I am trying to use Chart JS to create a table with dynamically generated data points coming from my JSON file. The logic of my code looks like so:
var datapart;
for (i = 0; i < jsonfile.jsonarray.length; i++){
datapart += {
label: jsonfile.jsonarray[i].name,
data: [jsonfile.jsonarray[i].age]
};
}
var config = {
type: 'line',
data: {
labels: ["Graph Line"],
datasets: [datapart]
}
}
My JSON file meanwhile looks something like so:
{
"jsonarray": [
{
"name": "Joe",
"age": 12
},
{
"name": "Tom",
"age": 14
}
]
}
The config
variable houses the configuration settings for ChartJS, including setting datapoints. When loaded into ChartJS, config
provides information needed to display my chart.
Anyhow, my thinking was to use the variable datapart
as a means of appending the datasets using my for loop. Unfortunately the code produces no results. I understand that my method for appending variables is faulty, but am unsure how to proceed.
How might I go about adding these JSON values to Chart.js?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…