score:14
You're on the right track, you'll have to iterate over your json and convert it into an structure chart.js will understand.
You should start with an empty structure containing all the static information:
var chartData = {
labels: [], // currently empty will contain all the labels for the data points
datasets: [
{
label: "Total Bills",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [] // currently empty will contain all the data points for bills
},
{
label: "Total Amount",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [] // currently empty will contain all the data points for bills
}
]
};
So far this will not print your chart, but it contains all the necessary information, like line labels and colors.
Now iterate over your array:
$.each(data.billDetails, function(position, billDetail) {
// first use the invoiceDate as a label
if (billDetail.invoiceDate) {
// You should probably format that
chartData.labels.push(billDetail.invoiceDate);
} else {
// your last data entry doesn't have an invoiceDate
chartData.labels.push(''); // blank or think of something creative
}
// add the totalBills and totalAmount to the dataset
chartData.datasets[0].data.push(billDetail.totalBills);
chartData.datasets[1].data.push(billDetail.totalAmount);
});
And now you can let chart.js render the chartData
.
score:0
Your JSON results should be same structure as chardata, and then you make this
var charData =
{
labels = [\\months]
datasets: data.datasets
}
in case that your response (in my case data.datasets) has sam structure like is hardcoded in those examples.
Source: stackoverflow.com
Related Query
- Drawing line chart in chart.js with json response
- Drawing line chart in chart.js with json data
- drawing line chart with chartjs
- Plotting multiple JSON subobjects in Chart.js Line chart ( with Time as x-axis)
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- Html5 web page with chartjs: loading an external json file and create a line chart
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- line chart with {x, y} point data displays only 2 values
- Display line chart with connected dots using chartJS
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Chart.js - line chart with two yAxis: "TypeError: yScale is undefined"
- How to display Line Chart dataset point labels with Chart.js?
- Chart.js Mixed Bar and Line chart with different scales
- chart.js Line chart with different background colors for each section
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js: Line chart with partial dashed line
- Obtain max value of y axis of line chart rendered with Chart.js
- ChartJS - Line Chart with different size datasets
- Line chart with large number of labels on X axis
- ChartJS - Line chart issue with only 1 point
- ChartJs line chart - display permanent icon above some data points with text on hover
- Is it possible to revert x-axe values in line chart with chart.js
- Drawing line chart in chart.js placing dots only when value changes
- Chart.js line chart with area range
- Extending Line Chart with custom line
- Can we draw a Line Chart with both solid and dotted line in it?
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Chart.js plotting two json datasets on a line chart
More Query from same tag
- Dates not displaying on X Axis using Chart.Js on MVC 5
- How do I access chart.js options dynamically?
- Chart is not populating when getting the data from AJAX call
- Chart.js pie chart not showing in Google Chrome canvas
- How to display chart using Chartjs with JSON data in Laravel
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- Chartjs v2.8 showAllTooltips does not exist
- Chart.js timescale: set min and max on xAxes
- Draw two plots using chartjs over one another with transparency
- How to get click event in Chart.js
- Order the Time Units on Y-axis + Chart Js
- the graph in the view is not displayed - Chart.js Angularjs
- ChartJS - TypeError: Cannot read property '_model' of null - Angular
- ChartJS Label Names Not Showing, Just ID
- How to fix chart Legends width-height with overflow scroll in ChartJS
- How I can add the predict data and the actual data in the same graph with ChartJS?
- Modify the legend of a double doughnut with chart js
- how to pass the value of input to my bar chart
- Firefox is not responding when loading 800+ data sets in Bar Chart with Charts.Js
- chart labels didn't shows in chartjs
- Angular 4: Different color for bar in bar chart dynamically using ChartJS
- Callback after line chart rendered
- Charts .js update when array changes
- chartjs: trying to rotate the y-Axis label
- How could I put a string for the points on the x-axis?
- Cannot read property 'map' of undefined when looking at an array with no index
- Get hash value Rails / Chart JS / Google Chart
- How to set gradient in legend box color in Chart,js
- Adding additional data to chart
- Display two line graphs with data starting at different points