Sankey Diagram with custom node widths and positions
This demo shows how you can use propertyFields and “adapters” to individually apply width and position for each Sankey Node.
Open in:New tab
/** * --------------------------------------- * This demo was created using amCharts 4. * * For more information visit: * https://www.amcharts.com/ * * Documentation is available at: * https://www.amcharts.com/docs/v4/ * --------------------------------------- */
// Themes beginam4core.useTheme(am4themes_animated);// Themes end
var chart = am4core.create("chartdiv", am4charts.SankeyDiagram);chart.hiddenState.properties.opacity = 0; // this creates initial fade-in
chart.data = [ { from: "D", width: 30, offset: -30 }, { from: "H", width: 50, offset: 15 }, { from: "C", width: 40, offset: 40 }, { from: "A", to: "D", value: 10 }, { from: "B", to: "D", value: 8 }, { from: "B", to: "E", value: 4 }, { from: "C", to: "E", value: 3 }, { from: "D", to: "G", value: 5 }, { from: "D", to: "I", value: 2 }, { from: "D", to: "H", value: 3 }, { from: "E", to: "H", value: 6 }, { from: "G", to: "J", value: 5 }, { from: "I", to: "J", value: 1 }, { from: "H", to: "J", value: 9 }];
let hoverState = chart.links.template.states.create("hover");hoverState.properties.fillOpacity = 0.6;
chart.dataFields.fromName = "from";chart.dataFields.toName = "to";chart.dataFields.value = "value";
// for right-most label to fitchart.paddingRight = 30;
// make nodes draggablevar nodeTemplate = chart.nodes.template;nodeTemplate.width = 20;
nodeTemplate.propertyFields.width = "width";
nodeTemplate.adapter.add("dx", function (dx, target) { return target.dataItem ? target.dataItem.dataContext.offset || dx : dx;});<div id="chartdiv"></div>body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";}
#chartdiv { width: 100%; height: 500px;}