以适合 Highcharts 的格式制作 JSON
Posted
技术标签:
【中文标题】以适合 Highcharts 的格式制作 JSON【英文标题】:Make JSON in the proper format for Highcharts 【发布时间】:2018-10-22 10:00:00 【问题描述】:以下是我的 Response 返回的 JSON 数据:
["id":1,"name":"AAA","value":5, "id":1,"name":"BBB","value":8, " id":1,"name":"CCC","value":9, "id":1,"name":"AAA","value":15, "id":1,"名称":"BBB","值":3, "id":1,"name":"CCC","value":5, "id":1,"name":"AAA" ,"value":3, , "id":1,"name":"BBB","value":10, "id":1,"name":"CCC","value": 11]
请问如何获得正确的格式来使用 jquery 或 javascript 绘制图表(Highcharts),如下所示:
series: [
name: 'AAA',
data: [5, 15, 3]
,
name: 'BBB',
data: [8, 3, 10]
,
name: 'CCC',
data: 9, 5, 11]
]
【问题讨论】:
请展示您尝试过的内容。 *** 不是help center 中所述的免费代码编写服务或“如何” 教程服务。目标是让您进行基础研究,其他人帮助您修复您的代码当它没有按预期工作时 【参考方案1】:首先创建一个临时对象,通过name
存储每个组,然后通过map()
将该对象转换为数组:
const data = ["id":1,"name":"AAA","value":5, "id":1,"name":"BBB","value":8, "id":1,"name":"CCC","value":9, "id":1,"name":"AAA","value":15, "id":1,"name":"BBB","value":3, "id":1,"name":"CCC","value":5, "id":1,"name":"AAA","value":3, , "id":1,"name":"BBB","value":10, "id":1,"name":"CCC","value":11];
const temp = ;
data.forEach(row =>
if (!temp[row.name])
temp[row.name] = name: row.name, data: [] ;
temp[row.name].data.push(row.value);
);
const series =
series: Object.keys(temp).map(name => ( name, data: temp[name].data )),
;
console.log(series);
【讨论】:
以上是关于以适合 Highcharts 的格式制作 JSON的主要内容,如果未能解决你的问题,请参考以下文章
使用 MySQL 为 HighCharts 每月格式化 JSON 数据