将行数据转换为Highcharts库的旭日图数据。

huangapple go评论86阅读模式
英文:

Convert row-data to sunburst chart data for Highcharts library

问题

以下是翻译好的内容:

从我的MongoDB数据库中获取的原始数据。现在,我想将以下数据转换为旭日图数据。有人可以帮助我吗?

> 这是我的输入数据数组。

[
{
    "globalId": "全局图表",
    "国家": "印度",
    "州": "古吉拉特邦",
    "城市": "瓦多达拉",
    "模式": "交通",
    "数值": 2.9
},
{
    "globalId": "全局图表",
    "国家": "印度",
    "州": "拉贾斯坦邦",
    "城市": "斋普尔",
    "模式": "交通",
    "数值": 2.9
},
{
    "globalId": "全局图表",
    "国家": "印度",
    "州": "德里",
    "城市": "德里",
    "模式": "交通",
    "数值": 100
},
{
    "globalId": "全局图表",
    "国家": "印度",
    "州": "德里",
    "城市": "德里",
    "模式": "人口",
    "数值": 2000
},
{
    "globalId": "全局图表",
    "国家": "印度",
    "州": "德里",
    "城市": "德里",
    "模式": "动物",
    "数值": 5
},
{
    "globalId": "全局图表",
    "国家": "印度",
    "州": "德里",
    "城市": "德里",
    "模式": "鸟类",
    "数值": 0
},
{
    "globalId": "全局图表",
    "国家": "印度",
    "州": "德里",
    "城市": "德里",
    "模式": "树木",
    "数值": 0
}
]

我想要用于Highcharts库的旭日图数据。

有人有解决方案吗?

英文:

My Row-Data which I got from my MongoDB database. Now, I want to convert the below data to sunburst chart data. Can anyone help me?

> These are my Input data array.

[
{
    "globalId": "Chart Global",
    "country": "India",
    "state": "Gujarat",
    "city": "Vadodara",
    "mode": "traffic",
    "value": 2.9
},
{
    "globalId": "Chart Global",
    "first": "India",
    "state": "Rajsthan",
    "city": "Jaipur",
    "mode": "traffic",
    "value": 2.9
},
{
    "globalId": "Chart Global",
    "first": "India",
    "state": "Delhi",
    "city": "Delhi",
    "mode": "traffic",
    "value": 100
},
{
    "globalId": "Chart Global",
    "first": "India",
    "state": "Delhi",
    "city": "Delhi",
    "mode": "population",
    "value": 2000
},
{
    "globalId": "Chart Global",
    "first": "India",
    "state": "Delhi",
    "city": "Delhi",
    "mode": "animals",
    "value": 5
},
{
    "globalId": "Chart Global",
    "first": "India",
    "state": "Delhi",
    "city": "Delhi",
    "mode": "birds",
    "value": 0
},
{
    "globalId": "Chart Global",
    "first": "India",
    "state": "Delhi",
    "city": "Delhi",
    "mode": "trees",
    "value": 0
}
]

I want data for the sunburst Chart for highcharts library

Does anyone have solutions?

答案1

得分: 0

以下是您要翻译的代码部分:

function seqToBurstData(originalData, circleKeys){
    const sunBurstData = [];
    const valuePath2pointId = {}; // auxiliary data used to identify the parent id of a point

    // get all the values for {dataPoint} from top key to {level} key, joined by '_'
    // used to index nameLevel2pointId data, to ensure each point has a unique index value
    const valuePath = (dataPoint, level) =>
        Array.from({length: level}, (_, i) => dataPoint[circleKeys[i]]).join('_');

    circleKeys.forEach(function(key, index){
        const level = index + 1;
        let currentValuePath = null, nValues = 0, sunBurstPoint;
        for(const o of originalData){
            const thisValuePath = valuePath(o, level);
            if(thisValuePath !== currentValuePath){
                currentValuePath = thisValuePath;
                sunBurstPoint = {
                    id: level + '.' + nValues, // scheme similar to Highcharts examples for sunburst
                    parent: level === 1 ? null : valuePath2pointId[valuePath(o, level - 1)],
                    name: o[key],
                    level, // level not required but useful
                    ...(level === circleKeys.length ? {value: o.value} : {}) // only for the final level
                };
                if(level < circleKeys.length){
                    valuePath2pointId[currentValuePath] = sunBurstPoint.id;
                }
                sunBurstData.push(sunBurstPoint);
                nValues++;
            }
            else if(level === circleKeys.length){
                sunBurstPoint.value += o.value;
            }
        }
    });

    return sunBurstData;
}

const originalData = [
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Gujarat",
        "city": "Vadodara",
        "mode": "traffic",
        "value": 59
    },
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Rajasthan",
        "city": "Jaipur",
        "mode": "traffic",
        "value": 59
    },
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Rajasthan",
        "city": "Ranthambore",
        "mode": "tigers",
        "value": 40
    },
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Delhi",
        "city": "Delhi",
        "mode": "traffic",
        "value": 100
    },
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Delhi",
        "city": "Delhi",
        "mode": "population",
        "value": 200
    },
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Delhi",
        "city": "Delhi",
        "mode": "animals",
        "value": 50
    },
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Delhi",
        "city": "Delhi",
        "mode": "birds",
        "value": 5
    },
    {
        "globalId": "Chart Global",
        "first": "India",
        "state": "Delhi",
        "city": "Delhi",
        "mode": "trees",
        "value": 5
    }
];


const sunBurstData = seqToBurstData(originalData, ['first', 'state', 'city', 'mode']).
    // add the "intro" key to change the point title according to the level
    map(o=>({...o, intro: o.level === 4 ? 'The value of' : 'Total for'}));

Highcharts.chart('chart', {

    chart: {
        height: '100%'
    },

    title: {
        text: ''
    },

    series: [{
        type: 'sunburst',
        data: sunBurstData,
        name: sunBurstData[0].name,
        allowDrillToNode: true,
        cursor: 'pointer',
        dataLabels: {
            format: '{point.name}'
        },
        levels: [{
            level: 1,
            color: 'transparent'
        }, {
            level: 2,
            colorByPoint: true
        }, {
            level: 3,
            colorVariation: {
                key: 'brightness',
                to: 0.3
            }
        }, {
            level: 4,
            colorVariation: {
                key: 'brightness',
                to: -0.3
            }
        }]
    }],

    tooltip: {
        headerFormat: '',
        pointFormat: '{point.intro} <b>{point.name}</b> is <b>{point.value}</b>'
    }
});

如果您需要更多帮助,请随时告诉我。

英文:

As a start point you can use this function (I changed a little bit the data to see more effects):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function seqToBurstData(originalData, circleKeys){
const sunBurstData = [];
const valuePath2pointId = {}; // auxiliary data used to identify the parent id of a point
// get all the values for {dataPoint} from top key to {level} key, joined by &#39;_&#39;
// used to index nameLevel2pointId data, to ensure each point has a unique index value
const valuePath = (dataPoint, level) =&gt;
Array.from({length: level}, (_, i) =&gt; dataPoint[circleKeys[i]]).join(&#39;_&#39;);
circleKeys.forEach(function(key, index){
const level = index + 1;
let currentValuePath = null, nValues = 0, sunBurstPoint;
for(const o of originalData){
const thisValuePath = valuePath(o, level);
if(thisValuePath !== currentValuePath){
currentValuePath = thisValuePath;
sunBurstPoint = {
id: level + &#39;.&#39; + nValues, // scheme similar to Highcharts examples for sunburst
parent: level === 1 ? null : valuePath2pointId[valuePath(o, level - 1)],
name: o[key],
level, // level not required but useful
...(level === circleKeys.length ? {value: o.value} : {}) // only for the final level
};
if(level &lt; circleKeys.length){
valuePath2pointId[currentValuePath] = sunBurstPoint.id;
}
sunBurstData.push(sunBurstPoint);
nValues++;
}
else if(level === circleKeys.length){
sunBurstPoint.value += o.value;
}
}
});
return sunBurstData;
}
const originalData = [
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Gujarat&quot;,
&quot;city&quot;: &quot;Vadodara&quot;,
&quot;mode&quot;: &quot;traffic&quot;,
&quot;value&quot;: 59
},
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Rajasthan&quot;,
&quot;city&quot;: &quot;Jaipur&quot;,
&quot;mode&quot;: &quot;traffic&quot;,
&quot;value&quot;: 59
},
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Rajasthan&quot;,
&quot;city&quot;: &quot;Ranthambore&quot;,
&quot;mode&quot;: &quot;tigers&quot;,
&quot;value&quot;: 40
},
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Delhi&quot;,
&quot;city&quot;: &quot;Delhi&quot;,
&quot;mode&quot;: &quot;traffic&quot;,
&quot;value&quot;: 100
},
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Delhi&quot;,
&quot;city&quot;: &quot;Delhi&quot;,
&quot;mode&quot;: &quot;population&quot;,
&quot;value&quot;: 200
},
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Delhi&quot;,
&quot;city&quot;: &quot;Delhi&quot;,
&quot;mode&quot;: &quot;animals&quot;,
&quot;value&quot;: 50
},
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Delhi&quot;,
&quot;city&quot;: &quot;Delhi&quot;,
&quot;mode&quot;: &quot;birds&quot;,
&quot;value&quot;: 5
},
{
&quot;globalId&quot;: &quot;Chart Global&quot;,
&quot;first&quot;: &quot;India&quot;,
&quot;state&quot;: &quot;Delhi&quot;,
&quot;city&quot;: &quot;Delhi&quot;,
&quot;mode&quot;: &quot;trees&quot;,
&quot;value&quot;: 5
}
];
const sunBurstData = seqToBurstData(originalData, [&#39;first&#39;, &#39;state&#39;, &#39;city&#39;, &#39;mode&#39;]).
// add the &quot;intro&quot; key to change the point title according to the level
map(o=&gt;({...o, intro: o.level === 4 ? &#39;The value of&#39; : &#39;Total for&#39;}));
Highcharts.chart(&#39;chart&#39;, {
chart: {
height: &#39;100%&#39;
},
title: {
text: &#39;&#39;
},
series: [{
type: &#39;sunburst&#39;,
data: sunBurstData,
name: sunBurstData[0].name,
allowDrillToNode: true,
cursor: &#39;pointer&#39;,
dataLabels: {
format: &#39;{point.name}&#39;
},
levels: [{
level: 1,
color: &#39;transparent&#39;
}, {
level: 2,
colorByPoint: true
}, {
level: 3,
colorVariation: {
key: &#39;brightness&#39;,
to: 0.3
}
}, {
level: 4,
colorVariation: {
key: &#39;brightness&#39;,
to: -0.3
}
}]
}],
tooltip: {
headerFormat: &#39;&#39;,
pointFormat: &#39;{point.intro} &lt;b&gt;{point.name}&lt;/b&gt; is &lt;b&gt;{point.value}&lt;/b&gt;&#39;
}
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/highcharts/10.3.2/highcharts.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://code.highcharts.com/modules/sunburst.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://code.highcharts.com/modules/accessibility.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;chart&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 15:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定