apexsankey 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # ApexSankey - Installation and Getting started
1
+ # ApexSankey
2
2
 
3
- The ApexSankey is a javascript library built on SVG that helps to create organizational or hierarchical charts.
3
+ A JavaScript library to create Sankey diagrams built on SVG
4
4
 
5
- <img width="811" alt="Screenshot 2023-12-17 at 10 28 04 PM" src="https://github.com/apexcharts/tree/assets/17950663/e09212ec-6322-4c68-ac12-9afc524d2abd">
5
+ <img width="752" alt="apex-sankey-chart" src="https://github.com/apexcharts/projects/assets/17950663/51e00ff0-79d1-45eb-8815-f8d11e2dd981">
6
6
 
7
7
 
8
8
  ## Installation
@@ -16024,7 +16024,6 @@ function neighbourPositions(G2, order, i, j, u, includeLoops = false) {
16024
16024
  thisRank.forEach((n, i2) => {
16025
16025
  if (G2.nodeEdges(n, u).length > 0) {
16026
16026
  positions.push(i2 + 0.5);
16027
- console.log(" ", n, i2);
16028
16027
  }
16029
16028
  });
16030
16029
  }
@@ -16543,10 +16542,8 @@ function sankey(options) {
16543
16542
  assignRanks(graph2, data2.rankSets || []);
16544
16543
  addDummyNodes(graph2);
16545
16544
  order = ordering(graph2);
16546
- console.log("order", order, data2.order);
16547
16545
  } else {
16548
16546
  order = data2.order;
16549
- console.log("order", order, data2.order);
16550
16547
  if (order.length > 0 && order[0].length > 0 && !lodashExports.isArray(order[0][0])) {
16551
16548
  order = order.map((rank) => [rank]);
16552
16549
  }
@@ -16680,28 +16677,34 @@ function drawRect({
16680
16677
  rect.stroke({ color: borderColor, width: 1 });
16681
16678
  return rect;
16682
16679
  }
16683
- function drawText(text = "", { x: x2, y: y2, fontColor = "#666767", textAnchor = "start" }) {
16680
+ function drawText(text = "", { x: x2, y: y2, fontColor = "#666767", textAnchor = "start", pointerEvents = "all" }) {
16684
16681
  const textSvg = new Text();
16685
16682
  textSvg.font({ fill: fontColor });
16686
16683
  textSvg.tspan(text);
16687
16684
  if (x2 !== void 0 && y2 !== void 0) {
16688
16685
  textSvg.move(x2, y2);
16689
16686
  }
16690
- textSvg.attr({ "text-anchor": textAnchor });
16687
+ textSvg.attr({ "text-anchor": textAnchor, "pointer-events": pointerEvents });
16691
16688
  return textSvg;
16692
16689
  }
16693
- function drawGroup(x2 = 0, y2 = 0, id, parent) {
16690
+ function drawGroup(x2, y2, id, parent) {
16694
16691
  const group = new G();
16695
- group.attr({ transform: `translate(${x2}, ${y2})`, "data-self": id, "data-parent": parent });
16692
+ group.attr({ transform: x2 && y2 ? `translate(${x2}, ${y2})` : void 0, "data-self": id, "data-parent": parent });
16696
16693
  return group;
16697
16694
  }
16698
- function drawPath(pathString, { id = "", edgeColor = "none", opacity = 1 } = {}) {
16695
+ function drawPath(pathString, { id = "", edgeColor, opacity = 1 } = {}) {
16699
16696
  const path = new Path({ d: pathString });
16700
16697
  path.id(id);
16701
16698
  path.fill(edgeColor).opacity(opacity);
16702
16699
  return path;
16703
16700
  }
16704
- const nodeTitle = (nodeData) => nodeData.title !== void 0 ? nodeData.title : nodeData.id;
16701
+ const nodeTitle = (nodeData, maxLength) => {
16702
+ const text = nodeData.title !== void 0 ? nodeData.title : nodeData.id;
16703
+ if (maxLength && text.length > maxLength) {
16704
+ return `${text.substr(0, 15)}...`;
16705
+ }
16706
+ return text;
16707
+ };
16705
16708
  function titlePosition(node) {
16706
16709
  let top = false;
16707
16710
  let right = false;
@@ -16719,51 +16722,55 @@ function titlePosition(node) {
16719
16722
  }
16720
16723
  return { top, right };
16721
16724
  }
16722
- function textTransform(d, position2) {
16723
- const y2 = position2.top ? -25 : d.dy / 2 - 10;
16725
+ function textTransform(node, position2, nodeWidth) {
16726
+ const y2 = position2.top ? -25 : node.dy / 2 - 10;
16724
16727
  let x2;
16725
16728
  if (position2.top) {
16726
16729
  x2 = position2.right ? 4 : -4;
16727
16730
  } else {
16728
- x2 = position2.right ? -10 : 10;
16731
+ x2 = position2.right ? nodeWidth : -nodeWidth;
16729
16732
  }
16730
- return { x: d.x + x2, y: d.y + y2 };
16733
+ return { x: node.x + x2, y: node.y + y2 };
16731
16734
  }
16732
- function renderNodeText(graphNode, fontColor) {
16735
+ function renderNodeText(graphNode, { fontColor, nodeWidth }) {
16733
16736
  const { data: data2 } = graphNode;
16734
- const titleLayoutData = titlePosition(graphNode);
16735
- const { x: x2, y: y2 } = textTransform(graphNode, titleLayoutData);
16736
- const text = drawText(nodeTitle(data2), {
16737
+ const position2 = titlePosition(graphNode);
16738
+ const { x: x2, y: y2 } = textTransform(graphNode, position2, nodeWidth);
16739
+ const text = drawText(nodeTitle(data2, position2.top ? 25 : 15), {
16737
16740
  x: x2,
16738
16741
  y: y2,
16739
- textAnchor: titleLayoutData.right ? "end" : "start",
16740
- fontColor
16742
+ textAnchor: !position2.top ? position2.right ? "start" : "end" : "middle",
16743
+ fontColor,
16744
+ pointerEvents: "none"
16741
16745
  });
16742
16746
  return text;
16743
16747
  }
16744
16748
  function renderNode(graphNode, options) {
16749
+ var _a, _b;
16745
16750
  const { x: x2, y: y2, dy: dy2 } = graphNode;
16746
16751
  const { nodeWidth, nodeBorderWidth, nodeBorderColor, onNodeClick, fontColor } = options;
16747
16752
  const group = new G({ class: "node" });
16748
- const body = drawRect({
16753
+ const nodeBody = drawRect({
16749
16754
  x: x2 - nodeWidth / 2,
16750
16755
  y: y2,
16751
16756
  width: nodeWidth,
16752
16757
  height: dy2
16753
16758
  });
16754
- body.fill(graphNode.data.color);
16755
- body.stroke({
16759
+ nodeBody.fill((_a = graphNode.data) == null ? void 0 : _a.color);
16760
+ nodeBody.stroke({
16756
16761
  width: nodeBorderWidth,
16757
- color: nodeBorderColor ?? graphNode.data.color
16762
+ color: nodeBorderColor ?? ((_b = graphNode.data) == null ? void 0 : _b.color)
16758
16763
  });
16759
- body.on("click", () => {
16764
+ nodeBody.on("click", () => {
16760
16765
  if (typeof onNodeClick === "function") {
16761
16766
  onNodeClick(graphNode);
16762
16767
  }
16763
16768
  });
16764
- const text = renderNodeText(graphNode, fontColor);
16765
- group.add(body);
16766
- group.add(text);
16769
+ group.add(nodeBody);
16770
+ if (graphNode.data) {
16771
+ const text = renderNodeText(graphNode, { fontColor, nodeWidth });
16772
+ group.add(text);
16773
+ }
16767
16774
  return group;
16768
16775
  }
16769
16776
  const DefaultPalette = [
@@ -16783,7 +16790,7 @@ const DefaultPalette = [
16783
16790
  "#14A14D",
16784
16791
  "#C5114D"
16785
16792
  ];
16786
- const getTooltip = (tooltipId = "apextree-tooltip-container") => {
16793
+ const getTooltip = (tooltipId = "apexsankey-tooltip-container") => {
16787
16794
  const tooltipElement = document.getElementById(tooltipId) || document.createElement("div");
16788
16795
  tooltipElement.id = tooltipId;
16789
16796
  return tooltipElement;
@@ -16846,9 +16853,7 @@ class SankeyGraph extends Paper {
16846
16853
  colorIndex
16847
16854
  };
16848
16855
  });
16849
- console.log("construct", data2.nodes, updatedNodes);
16850
16856
  const graph2 = layout(data2.edges, updatedNodes, data2.options);
16851
- console.log("graph", graph2);
16852
16857
  this.graph = graph2;
16853
16858
  }
16854
16859
  renderEdge(edgeObj, group) {
@@ -16858,33 +16863,44 @@ class SankeyGraph extends Paper {
16858
16863
  tooltipBGColor,
16859
16864
  tooltipBorderColor,
16860
16865
  edgeOpacity,
16866
+ edgeGradientFill,
16861
16867
  tooltipTemplate
16862
16868
  } = this.options;
16863
16869
  const edgeLabel = this.graph.edge(edgeObj.v, edgeObj.w, edgeObj.name);
16864
16870
  const sourceNode = this.graph.node(edgeObj.v);
16865
16871
  const targetNode = this.graph.node(edgeObj.w);
16866
16872
  const edge = generatePath(edgeLabel);
16873
+ const edgeColor = this.canvas.gradient("linear", function(add) {
16874
+ var _a, _b, _c;
16875
+ add.stop(0, (_a = sourceNode.data) == null ? void 0 : _a.color);
16876
+ add.stop(1, edgeGradientFill ? (_b = targetNode.data) == null ? void 0 : _b.color : (_c = sourceNode.data) == null ? void 0 : _c.color);
16877
+ });
16867
16878
  const path = drawPath(edge, {
16868
- edgeColor: sourceNode.data.color,
16879
+ id: `${edgeObj.v}-${edgeObj.w}`,
16880
+ edgeColor,
16869
16881
  opacity: edgeOpacity
16870
16882
  });
16871
- if (enableTooltip) {
16883
+ if (enableTooltip && sourceNode.data && targetNode.data) {
16872
16884
  const tooltipContent = tooltipTemplate ? tooltipTemplate({
16873
16885
  source: sourceNode.data,
16874
16886
  target: targetNode.data,
16875
16887
  value: edgeLabel.value
16876
16888
  }) : null;
16877
16889
  path.on("mousemove", function(e) {
16890
+ var _a;
16878
16891
  const styles = getTooltipStyles(
16879
16892
  e.pageX,
16880
16893
  e.pageY,
16881
16894
  tooltipBorderColor,
16882
16895
  tooltipBGColor
16883
16896
  );
16897
+ (_a = SVG(`[id="${e.target.id}"]`)) == null ? void 0 : _a.front().opacity(1);
16884
16898
  updateTooltip(tooltipId, styles.join(" "), tooltipContent);
16885
16899
  });
16886
- path.on("mouseout", function() {
16900
+ path.on("mouseout", function(e) {
16901
+ var _a;
16887
16902
  updateTooltip(tooltipId);
16903
+ (_a = SVG(`[id="${e.target.id}"]`)) == null ? void 0 : _a.front().opacity(edgeOpacity);
16888
16904
  });
16889
16905
  }
16890
16906
  group.add(path);
@@ -16915,9 +16931,12 @@ class SankeyGraph extends Paper {
16915
16931
  mainGroup.attr("style", globalStyle);
16916
16932
  const nodes = ((_a = this.graph) == null ? void 0 : _a.nodes()) || [];
16917
16933
  const edges = ((_b = this.graph) == null ? void 0 : _b.edges()) || [];
16934
+ const edgesGroup = drawGroup();
16918
16935
  edges.forEach((edgeObj) => {
16919
- this.renderEdge(edgeObj, mainGroup);
16936
+ this.renderEdge(edgeObj, edgesGroup);
16920
16937
  });
16938
+ mainGroup.add(edgesGroup);
16939
+ const nodesGroup = drawGroup();
16921
16940
  nodes.forEach((nodeId) => {
16922
16941
  const node = this.graph.node(nodeId);
16923
16942
  const nodeGroup = renderNode(node, {
@@ -16927,8 +16946,9 @@ class SankeyGraph extends Paper {
16927
16946
  onNodeClick,
16928
16947
  fontColor
16929
16948
  });
16930
- mainGroup.add(nodeGroup);
16949
+ nodesGroup.add(nodeGroup);
16931
16950
  });
16951
+ mainGroup.add(nodesGroup);
16932
16952
  this.add(mainGroup);
16933
16953
  if (keepOldPosition) {
16934
16954
  this.updateViewBox(
@@ -16955,6 +16975,7 @@ const DefaultOptions = {
16955
16975
  nodeBorderColor: null,
16956
16976
  onNodeClick: void 0,
16957
16977
  edgeOpacity: 0.4,
16978
+ edgeGradientFill: true,
16958
16979
  fontSize: "14px",
16959
16980
  fontFamily: "",
16960
16981
  fontWeight: 400,
@@ -16980,11 +17001,11 @@ class ApexSankey {
16980
17001
  constructor(element, options) {
16981
17002
  this.element = element;
16982
17003
  this.options = { ...DefaultOptions, ...options };
16983
- const treeWrapper = document.createElement("div");
16984
- treeWrapper.id = "apexTreeWrapper";
16985
- treeWrapper.style.position = "relative";
16986
- this.graph = new SankeyGraph(treeWrapper, this.options);
16987
- this.element.append(treeWrapper);
17004
+ const sankeyWrapper = document.createElement("div");
17005
+ sankeyWrapper.id = "apexSankeyWrapper";
17006
+ sankeyWrapper.style.position = "relative";
17007
+ this.graph = new SankeyGraph(sankeyWrapper, this.options);
17008
+ this.element.append(sankeyWrapper);
16988
17009
  }
16989
17010
  render(data2) {
16990
17011
  if (!this.element) {