Chart.js 在折线图上绘制两个 json 数据集
Chart.js plotting two json datasets on a line chart
我正在尝试找出如何使用 Chart.js
将标签绘制到正确的位置
var json = {
"competition one": [
{
"date": "2015-05-20",
"position": 37
},
{
"date": "2015-05-21",
"position": 22
}
],
"competition two": [
{
"date": "2015-05-20",
"position": 29
},
{
"date": "2015-05-21",
"position": 19
}
]
}
如何将标签绘制到正确的位置?日期会转到正确的标签上,这样就不会重复了吗?
具体来说,我正在努力将 "competition one"
放入 dataset
数组的 label
(label: "competition one"
)
我需要它类似于 Chart.js?
所需的以下数据结构
var data = {
labels: ["2015-05-20", "2015-05-21"],
datasets: [
{
label: "competition one",
data: [37, 22]
},
{
label: "Competition two",
data: [29, 19]
}
]
};
如评论中所述,您可以获得如下属性名称:
var json = {
"competition one": [
{
"date": "2015-05-20",
"position": 37
},
{
"date": "2015-05-21",
"position": 22
}
],
"competition two": [
{
"date": "2015-05-20",
"position": 29
},
{
"date": "2015-05-21",
"position": 19
}
]
}
var keys = Object.keys(json);
for (var i = 0; i < keys.length; i++)
{
var key = keys[i];
//"competition one", "competition two", etc
console.log(key);
}
Fiddle
然后您只需将这些值操作成您想要的对象结构。
var keys = Object.keys(json);
//set up our object containing an array called datasets
var data = {datasets:[]};
for (var i = 0; i < keys.length; i++)
{
var key = keys[i];
//push the key into the dataset array as an object {}
data.datasets.push({label:key, data:...});
}
我正在尝试找出如何使用 Chart.js
将标签绘制到正确的位置var json = {
"competition one": [
{
"date": "2015-05-20",
"position": 37
},
{
"date": "2015-05-21",
"position": 22
}
],
"competition two": [
{
"date": "2015-05-20",
"position": 29
},
{
"date": "2015-05-21",
"position": 19
}
]
}
如何将标签绘制到正确的位置?日期会转到正确的标签上,这样就不会重复了吗?
具体来说,我正在努力将 "competition one"
放入 dataset
数组的 label
(label: "competition one"
)
我需要它类似于 Chart.js?
所需的以下数据结构var data = {
labels: ["2015-05-20", "2015-05-21"],
datasets: [
{
label: "competition one",
data: [37, 22]
},
{
label: "Competition two",
data: [29, 19]
}
]
};
如评论中所述,您可以获得如下属性名称:
var json = {
"competition one": [
{
"date": "2015-05-20",
"position": 37
},
{
"date": "2015-05-21",
"position": 22
}
],
"competition two": [
{
"date": "2015-05-20",
"position": 29
},
{
"date": "2015-05-21",
"position": 19
}
]
}
var keys = Object.keys(json);
for (var i = 0; i < keys.length; i++)
{
var key = keys[i];
//"competition one", "competition two", etc
console.log(key);
}
Fiddle
然后您只需将这些值操作成您想要的对象结构。
var keys = Object.keys(json);
//set up our object containing an array called datasets
var data = {datasets:[]};
for (var i = 0; i < keys.length; i++)
{
var key = keys[i];
//push the key into the dataset array as an object {}
data.datasets.push({label:key, data:...});
}