我有以下JSON数据 .
[
{
"date": "2016-05-01T00:00:00",
"productInformation": [
{
"productName": "Apple",
"totalWeight": 200
}
]
},
{
"date": "2016-09-01T00:00:00",
"productInformation": [
{
"productName": "Apple",
"totalWeight": 632
},
{
"productName": "Mango",
"totalWeight": 856
},
{
"productName": "Spinach",
"totalWeight": 545
},
{
"productName": "Grapes",
"totalWeight": 338
}
]
},
{
"date": "2017-01-01T00:00:00",
"productInformation": [
{
"productName": "Mango",
"totalWeight": 500
}
]
}
]
在X轴上我想显示月份和年份,在Y轴上我想显示产品信息的堆积条 . 例如在2016-05,只有苹果,所以它只会显示苹果 . 2016-09有4种产品,因此根据4种产品及其总重量将显示4个放样条 . 我已经阅读了chart.js文档 . 根据文档,我必须在数据集中提供Y轴值 . 如何从数据集中提取Y轴值以根据给定的JSON数据创建堆积条?如果我想从上面给出的JSON数据手动创建图表,那么它就是这样的 .
var barChartData = {
labels: ["May 2016", "September 2016", "January 2017"],
datasets: [{
label: "Apple",
data: [200, 632, 0],
backgroundColor: "#3c8dbc"
},
{
label: "Mango",
data: [0,856,500],
backgroundColor: "#3c8dbc"
},
{
label: "Spinach",
data: [0,545,0],
backgroundColor: "#3c8dbc"
},
{
label: "Grapes",
data: [0,338,0],
backgroundColor: "#3c8dbc"
}]
};
我需要一种方法从给定的JSON数据中提取 data
数据集的一部分 .
1 回答
此代码段应解决您问题中最困难的部分(使用ES6语法):