score:60
Accepted answer
your approach on constructing the chart is completely inappropriate. here is the proper way, that you should follow :
var jsonfile = {
"jsonarray": [{
"name": "joe",
"age": 12
}, {
"name": "tom",
"age": 14
}]
};
var labels = jsonfile.jsonarray.map(function(e) {
return e.name;
});
var data = jsonfile.jsonarray.map(function(e) {
return e.age;
});;
var ctx = canvas.getcontext('2d');
var config = {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'graph line',
data: data,
backgroundcolor: 'rgba(0, 119, 204, 0.3)'
}]
}
};
var chart = new chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.6.0/chart.min.js"></script>
<canvas id="canvas"></canvas>
score:0
i found the solution proposed by bear grizzly xi quite helpful. makes use of the for.. loop.
let's suppose you have a json response from your api as follows:
var markschart = document.getelementbyid("markschart");
{
"labels": "maths,geography,physics,chemistry,english,biology,music",
"datasets": [
{
"label": "student 3",
"data": "120, 90, 45, 90, 14, 100, 88",
"spangaps": false
},
{
"label": "student 2",
"data": "150, 80, 99, 100, 90, 110, 97",
"spangaps": false
},
{
"label": "student 1",
"data": "140, 100, 89, 134, 120, 78, 56",
"spangaps": false
}
]
}
in javascript you may handle the response as follows (response contains the above json packet):
var mydatasets = [];
var colorslist = ["blue","orange","magenta","green","syrup","navy","bumblebee","turkish","army","ferrari"];
for(var j = 0; j < response.datasets.length; j++) {
mydatasets.push({label: response.datasets[j].label, bodercolor: colorslist[j], data: response.datasets[j].data.splits(','), spangraphs: true});
}
var subjectsdata = {
labels: response.labels.split(','),
datasets: mydatasets
}
var options = {
scales: {
yaxes: [{
ticks: {
beginatzero: true
},
scalelabel: {
display: true,
labelstring: 'subject perfomance',
fontsize: 14
}
}]
}
};
var studentsmarksperformance = new chart(markschart, {
type: "line",
data: subjectsdata ,
options: options
});
the above is not a complete solution but may help with implementing for..each loop in creating a line chart using chart.js
Source: stackoverflow.com
Related Query
- Displaying JSON data in Chartjs
- Displaying JSON data with Chartjs
- Chartjs Data via json request not populating
- ChartJs Not displaying data
- Chart not displaying from JSON data
- Chartjs animate x-axis displaying data incorrectly in mac device
- ChartJS not displaying time data using Moment.js
- How to display chart using Chartjs with JSON data in Laravel
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- Chartjs not displaying timed data
- Passing JSON data from PHP array into ChartJS
- Dynamically pass the JSON data in chartjs
- Read google sheet json data into chartjs
- ChartJs - displaying data dynamically from back-end
- passing json data to chartjs
- load external json data file in to chartjs in the angular component
- Rendering Rails json data in chartjs
- Chartjs in Vue integrate parsed Json Data
- ChartJS have xAxes labels match data source
- JSON cyclic object value when posting data in ChartJS
- How to use JSON data as chartjs data?
- Displaying data from simple html form to display in chartjs
- Transform JSON data for ChartJS
- ionic display chartjs data from json http request
- Json data visualization in Javascript with chartjs
- ChartJs error in displaying my array data
- Calling data from outside of Chartjs code
- ChartJS - format datetime X axis when displaying data from MySql table
- Data not displaying in Chart JS from PHP JSON
- How can I reload data from chartjs after JSON file was modified in a HTTP POST
More Query from same tag
- How to set 3 axis in google chart (V-Axis Left as Qty, V-Axis Right as Series Column and H-Axis as TimeOrder)?
- Can you set seperate colors for different axes in Chart.js?
- graph data from a angular form to ChartJs
- Chart.js (3.7) - Why do the tooltips not appear?
- How to label x-Axis in Chart.js by days?
- Fix ChartJS Values not Well Displayed
- ChartJS set styling on the last point only
- Q: Chart.js v2 - x-axis time scale configuration
- Why is this "not defined"
- How to convert Data table to Json in Javascript
- Chartjs line graph dataset with offset
- How to change the angle of x axis ticks dynamically based on size of name?
- ChartJS and Radar Chart animation
- ChartJS timeline graph with events
- Chartjs v2.0: stacked bar chart
- How call a function when my array is filled
- gradient background on radar chartjs
- data from api not showing in chart.js (angular)
- Visual Studio 2013 IIS web site - problems with JS files
- Category scale on Y-axis and time on x-axis in bubble chart in Chartjs
- Values above points - chart.js
- Chartjs: Is it possible to hide the data labels on the axis but show up on the graph on hover?
- Pie chart with Chart.js and mySQL
- Problem with primeNG chart: Module not found: Error: Can't resolve 'chart.js/auto'
- Chart disappears just after finishing animation
- Multiple Graphs on one page
- Add mouseout event handler to legends in Chart.js
- Chartjs bar order adjustment after bar chart is drawn
- Why is my data on chartjs not starting from the data that it should
- attempting to change height and width of canvas for chart