score:4
Accepted answer
Couple of Issues :
- since
$.getJSON()
method is asynchronous, you should construct the chart inside it's callback function. - you are looping through the response data incorrectly. could be as simple as :
var labels = data.customers[0].amounts.map(function(e) {
return e[0];
});
var data = data.customers[0].amounts.map(function(e) {
return e[1];
});
- you are adding
labels
array to your dataset, while it belogns to thedata
object.
Here is the revised version of your code :
$.getJSON("https://jsonblob.com/api/jsonBlob/26078b70-6b6f-11e7-a38a-bf689f57642c", function(data) {
var labels = data.customers[0].amounts.map(function(e) {
return e[0];
});
var data = data.customers[0].amounts.map(function(e) {
return e[1];
});
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
backgroundColor: 'rgb(129, 198, 2228)',
borderColor: 'rgb(0, 150, 215)',
data: data
}]
},
options: {
responsive: 'true',
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="myChart"></canvas>
Source: stackoverflow.com
Related Query
- Use JSON file with chart.js
- How to use JSON data in creating a chart with chartjs?
- Chart js with json file from url
- Html5 web page with chartjs: loading an external json file and create a line chart
- JS import updated JSON file for use with ChartJS
- Live Update Callback -> afterTitle with Array via JSON file
- Drawing line chart in chart.js with json response
- Multiple lines / data series from JSON file with Chart.js
- Use transparent stroke color with chartjs pie chart
- How do I use my chart.js line chart with handlebars?
- How to display chart using Chartjs with JSON data in Laravel
- Drawing line chart in chart.js with json data
- Use fetch to get JSON Data for Chart JS
- Parsing values from external JSON file with Chart.js
- Problem with script src for Chart.js. The CDN for Chart.js funtions fine, so my code is ok. Somehow I'm not linking the file correctly
- Chart.JS Multiline Chart with unknown numbers of Lines from JSON
- How to use chart.js to plot line chart with x axis as time stamp in seconds
- Use data from SQL database in .js file with PHP
- what is wrong with my code ? java script chart position
- Use multiple datasets with Javascript Chart
- How to fetch data from Json file on charts(bar-chart/pie chart etc) in angular?
- Chart.js - creating time series freqency chart with JSON data
- Can't use a chart module with angularjs
- Angular chart js how to use formatter with Doughnut chart
- How do I make a pie chart showing number of people in an age group with JSON and ChartJS?
- I want to use an object to help create a chart with Chartjs
- loading json file to create chart
- opening json file to create chart in chart.js
- Plotting multiple JSON subobjects in Chart.js Line chart ( with Time as x-axis)
- How to use computed property with data from JSON in data object for time axis in Vue using Chart.js
More Query from same tag
- Chart.js how to show cursor pointer for labels & legends in line chart
- Working with multiple date axes in ChartJS
- Chart.js 2.0 - vertical lines
- Django and Chartjs template conflict
- ChartJS and Flask, how to pass data?
- How to use output of JSON.parse() as a input of HTML charts' data field?
- Svelte , pass data to chartjs from API
- Angular 5, Chart.js displaying multiple charts, one on each tab
- chartJS or/and php print_r not working together
- Extend chart.js in PHP heredoc causes parse error
- How to show label for second y-axis in charts.js?
- django chart.js - Query to get count of all distinct values for particular column
- Changing x axis labels in Chart.js line chart
- Getting nice chart.js animation when adding data with unshift()?
- Chart.js Types of property 'type' are incompatible. Type 'string' is not assignable to type '"line" | "bar" | "scatter"
- Mapping data (from getJSON response) in Chart.js
- What's the most effective way to implement a radar plot with 50 points at arbitrary locations using chart.js
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- How can I highlight/format a specific date label (e.g. today) on a time axis using chart.js?
- Display php array data in chart.js javascript
- How to install Chart.js without a library?
- Chart.js legend alignment left side
- Looping through afterDraw in ChartJS
- Angular canvas is undefined
- how to access class variables inside chart.js custom tooltip interface
- chart.js how to display max value?
- How can I pass the data into the chartjs label?
- Hide labels on x-axis ChartJS
- how to set color for each datapoint in chartjs-chart-treemap
- Cannot use ng2-charts with Ionic 3 (ionic-angular 3.9.4)