UNPKG

d3-force-cluster

Version:
1 lines 4.41 kB
{"version":3,"file":null,"sources":["../lib/forceCluster.js"],"sourcesContent":["/**\n * Pulls nodes toward a set of cluster center nodes / points.\n * Adapted from Mike Bostock's Clustered Force Layout III:\n * https://bl.ocks.org/mbostock/7881887\n */\nfunction cluster (centers) {\n\n let nodes,\n centerpoints = [],\n strength = 0.1,\n centerInertia = 0.0;\n\n // coerce centers accessor into a function\n if (typeof centers !== 'function') centers = () => centers;\n\n function force (alpha) {\n // scale + curve alpha value\n alpha *= strength * alpha;\n\n let c, x, y, l, r;\n nodes.forEach((d, i) => {\n c = centerpoints[i];\n if (!c || c === d) return;\n \n x = d.x - c.x,\n y = d.y - c.y,\n l = Math.sqrt(x * x + y * y),\n r = d.radius + (c.radius || 0);\n\n if (l && l != r) {\n l = (l - r) / l * alpha;\n d.x -= x *= l;\n d.y -= y *= l;\n c.x += (1 - centerInertia) * x;\n c.y += (1 - centerInertia) * y;\n }\n });\n }\n\n function initialize () {\n if (!nodes) return;\n\n // populate local `centerpoints` using `centers` accessor\n let i, n = nodes.length;\n centerpoints = new Array(n);\n for (i = 0; i < n; i++) centerpoints[i] = centers(nodes[i], i, nodes);\n }\n \n /**\n * Reinitialize the force with the specified nodes.\n */\n force.initialize = _ => {\n nodes = _;\n initialize();\n };\n\n /**\n * An array of objects representing the centerpoint of each cluster,\n * or a function that returns such an array.\n * Each object must have `x` and `y` values, and optionally `radius`.\n */\n force.centers = _ => {\n // return existing value if no value passed\n if (_ == null) return centers;\n\n // coerce centers accessor into a function\n centers = typeof _ === 'function' ? _ : (n, i) => _[i];\n\n // reinitialize\n initialize();\n\n // allow chaining\n return force;\n };\n\n /**\n * Strength of attraction to the cluster center node/position.\n */\n force.strength = _ => {\n return _ == null ? strength : (strength = +_, force);\n };\n\n /**\n * Inertia of cluster center nodes/positions.\n * Higher values mean the cluster center moves less;\n * lower values mean the cluster center is more easily\n * pulled around by other nodes in the cluster.\n * Typical values range from 0.0 (cluster centers move as much as all other nodes)\n * to 1.0 (cluster centers are not moved at all by the clustering force).\n */\n force.centerInertia = _ => {\n return _ == null ? centerInertia : (centerInertia = +_, force);\n };\n\n return force;\n\n}\n\nexport default cluster;"],"names":["cluster","centers","nodes","centerpoints","strength","centerInertia","force","alpha","c","x","y","l","r","forEach","d","i","Math","sqrt","radius","initialize","n","length","Array","_"],"mappings":"AAAA;;;;;AAKA,SAASA,OAAT,CAAkBC,QAAlB,EAA2B;;MAErBC,cAAJ;MACEC,eAAe,EADjB;MAEEC,WAAW,GAFb;MAGEC,gBAAgB,GAHlB;;;MAMI,OAAOJ,QAAP,KAAmB,UAAvB,EAAmCA,WAAU;WAAMA,QAAN;GAAV;;WAE1BK,KAAT,CAAgBC,KAAhB,EAAuB;;aAEZH,WAAWG,KAApB;;QAEIC,UAAJ;QAAOC,UAAP;QAAUC,UAAV;QAAaC,UAAb;QAAgBC,UAAhB;UACMC,OAAN,CAAc,UAACC,CAAD,EAAIC,CAAJ,EAAU;UAClBZ,aAAaY,CAAb,CAAJ;UACI,CAACP,CAAD,IAAMA,MAAMM,CAAhB,EAAmB;;UAEfA,EAAEL,CAAF,GAAMD,EAAEC,CAAZ,EACAC,IAAII,EAAEJ,CAAF,GAAMF,EAAEE,CADZ,EAEAC,IAAIK,KAAKC,IAAL,CAAUR,IAAIA,CAAJ,GAAQC,IAAIA,CAAtB,CAFJ,EAGAE,IAAIE,EAAEI,MAAF,IAAYV,EAAEU,MAAF,IAAY,CAAxB,CAHJ;;UAKIP,KAAKA,KAAKC,CAAd,EAAiB;YACX,CAACD,IAAIC,CAAL,IAAUD,CAAV,GAAcJ,KAAlB;UACEE,CAAF,IAAOA,KAAKE,CAAZ;UACED,CAAF,IAAOA,KAAKC,CAAZ;UACEF,CAAF,IAAO,CAAC,IAAIJ,aAAL,IAAsBI,CAA7B;UACEC,CAAF,IAAO,CAAC,IAAIL,aAAL,IAAsBK,CAA7B;;KAdJ;;;WAmBOS,UAAT,GAAuB;QACjB,CAACjB,KAA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