gpt4 book ai didi

javascript - Highcharts 的 JSON 数据格式

转载 作者:行者123 更新时间:2023-11-30 15:00:23 24 4
gpt4 key购买 nike

我有一个 json 对象,如下所示。我希望它根据特定属性进行分组,比如“SEX”并将其传递给 highcharts,这样我只有一个 xAxis 类别用于“TIME”,图表应如下所示:enter image description here

[{"TIME":"2017-09-15","SEX":"MALE","ARIZONA":483280000.0,"IDAHO":6624960.0,"RATE":98.6291686272},
{"TIME":"2017-09-15","SEX":"FEMALE","ARIZONA":1034350000.0,"IDAHO":32409500.0,"RATE":96.8666697274},
{"TIME":"2017-09-16","SEX":"MALE","ARIZONA":482379000.0,"IDAHO":9578100.0,"RATE":98.0144001036},
{"TIME":"2017-09-16","SEX":"FEMALE","ARIZONA":1052960000.0,"IDAHO":40686800.0,"RATE":96.1359744871},
{"TIME":"2017-09-17","SEX":"FEMALE","ARIZONA":1052530000.0,"IDAHO":41476900.0,"RATE":96.0593301937},
{"TIME":"2017-09-17","SEX":"MALE","ARIZONA":1052590000.0,"IDAHO":41479900.0,"RATE":96.0893301937},]

我们如何实现上面的JSON格式..??

最佳答案

首先,你应该使用filter方法以获得两个数组:一个用于male另一个是 female实体。

然后使用map方法以获得dates , femaleRatesmaleRates 数组

let array=[{"TIME":"2017-09-15","SEX":"MALE","ARIZONA":483280000.0,"IDAHO":6624960.0,"RATE":98.6291686272}, {"TIME":"2017-09-15","SEX":"FEMALE","ARIZONA":1034350000.0,"IDAHO":32409500.0,"RATE":96.8666697274}, {"TIME":"2017-09-16","SEX":"MALE","ARIZONA":482379000.0,"IDAHO":9578100.0,"RATE":98.0144001036}, {"TIME":"2017-09-16","SEX":"FEMALE","ARIZONA":1052960000.0,"IDAHO":40686800.0,"RATE":96.1359744871}, {"TIME":"2017-09-17","SEX":"FEMALE","ARIZONA":1052530000.0,"IDAHO":41476900.0,"RATE":96.0593301937}, {"TIME":"2017-09-17","SEX":"MALE","ARIZONA":1052590000.0,"IDAHO":41479900.0,"RATE":96.0893301937}];
let males=array.filter(a=>a.SEX=='MALE');
let females=array.filter(a=>a.SEX=='FEMALE');
let dates=males.map(a=>a.TIME);
let malesRates=males.map(a=>a.RATE);
let femaleRates=females.map(a=>a.RATE);
console.log(dates);
console.log(malesRates);
console.log(femaleRates);

最后一步是在您的图表中显示数据。

let array=[{"TIME":"2017-09-15","SEX":"MALE","ARIZONA":483280000.0,"IDAHO":6624960.0,"RATE":98.6291686272}, {"TIME":"2017-09-15","SEX":"FEMALE","ARIZONA":1034350000.0,"IDAHO":32409500.0,"RATE":96.8666697274}, {"TIME":"2017-09-16","SEX":"MALE","ARIZONA":482379000.0,"IDAHO":9578100.0,"RATE":98.0144001036}, {"TIME":"2017-09-16","SEX":"FEMALE","ARIZONA":1052960000.0,"IDAHO":40686800.0,"RATE":96.1359744871}, {"TIME":"2017-09-17","SEX":"FEMALE","ARIZONA":1052530000.0,"IDAHO":41476900.0,"RATE":96.0593301937}, {"TIME":"2017-09-17","SEX":"MALE","ARIZONA":1052590000.0,"IDAHO":41479900.0,"RATE":96.0893301937}];
let males=array.filter(a=>a.SEX=='MALE');
let females=array.filter(a=>a.SEX=='FEMALE');
let dates=males.map(a=>a.TIME);
let malesRates=males.map(a=>a.RATE);
let femaleRates=females.map(a=>a.RATE);
Highcharts.chart('container', {
xAxis: {
categories:dates
},
series: [{
name: 'Males',
data: malesRates
}, {
name: 'Females',
data:femaleRates
}],

responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom'
}
}
}]
}

});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container"></div>

关于javascript - Highcharts 的 JSON 数据格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46620237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com