Skip to content

Instantly share code, notes, and snippets.

@stnc
Created August 25, 2018 11:07
Show Gist options
  • Save stnc/c129fbf056c61732376545ba66946089 to your computer and use it in GitHub Desktop.
Save stnc/c129fbf056c61732376545ba66946089 to your computer and use it in GitHub Desktop.
Highcharts sankey problem fix (Demo http://jsfiddle.net/vh4792wy/39/ )
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.src.js"></script>
<div id="container"></div>
var StandartDatam=[
{ from: "F18 - Bella Italia", to: "F01-Administration", weight: 410 },
{ from: "F54 - Italian Village", to: "F01-Administration", weight: 327 },
{ from: "F42-Ferrari Store", to: "F01-Administration", weight: 325 },
{ from: "F02 - G-Force", to: "F01-Administration", weight: 294 },
{ from: "Unknown", to: "F01-Administration", weight: 290 },
{ from: "F01-Administration", to: "F54 - Italian Villag e", weight: 465 },
{ from: "F01-Administration", to: "F59 - Go Kart", weight: 462 },
{ from: "F01-Administration", to: "Unknown", weight: 451 },
{ from: "F01-Administration", to: "F18 - Bella Italia", weight: 422 },
{ from: "F01-Administration", to: "F02 - G-Force", weight: 335 },
/*
{from:"F01-Administration1", to: "Unknown ", weight: 451},
{from:"F01-Administration1", to: "F18 - Bella Italia ", weight: 422},
{from:"F01-Administration1", to: "F02 - G-Force ", weight: 335},
{from:"F01-Administration1", to: "Unknown", weight: 188},
{from:"F01-Administration1", to: "F18 - Bella Italia", weight: 422},
{from:"F01-Administration1", to: "F02 - G-Force", weight: 335},
*/
{ from: "F02 - G-Force1", to: "f45", weight: 335 },
{ from: "f45", to: "f46", weight: 335 },
{ from: "f46", to: "f47", weight: 335 },
{ from: "f47", to: "f46", weight: 335 },
];
var DataFixer = data => {
const ArrayGroup = data.reduce(function (r, a) {
r[a.to] = r[a.to] || [];
r[a.to].push(a);
return r;
}, Object.create(null));
//object to array
var myArr = Object.keys(ArrayGroup).map(function (x) { return ArrayGroup[x]; })
var array = [];
var arrayMerge = [];
let ArrayCount = myArr.length;
//root array
for (var i = 0; i < ArrayCount; i++) {
var merged = myArr[i];
var counter = merged.length;
if (counter > 1) {
if (merged[0].to == merged[1].to) {
//array subcategory
for (var ia = 0; ia < merged.length; ia++) {
arrayMerge.push({
from: merged[ia].from,
to: merged[ia].to + ' ',
weight: merged[ia].weight,
});
delete merged[ia]
}
}
}
Array.prototype.push.apply(array, merged);
}
Array.prototype.push.apply(array, arrayMerge);
var result = array.filter(function( element ) {
return element !== undefined;
});
return result;
};
var StandartData=DataFixer(StandartDatam)
Highcharts.chart('container', {
title: {
text: 'Highcharts Sankey Diagram'
},
series: [{
data:StandartData,
type: 'sankey',
name: 'Sankey demo series'
}]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment