I created a test chart and it works fine. I would like to fetch my data via ajax rather than embedding in the web page. I figure that I need to format my dataPoints arrays ( { x: new Date(2009, 0), y: 15 },
) so that they can be used in an external JSON file. In my example the format of the data that populates my chart is:
{
name: "File Sharing",
showInLegend: true,
type: "stackedColumn100",
color: "#4192D9 ",
indexLabel: "{y}",
dataPoints: [
{ x: new Date(2009, 0), y: 15 },
{ x: new Date(2010, 0), y: 15 },
{ x: new Date(2011, 0), y: 12 },
{ x: new Date(2012, 0), y: 10 },
{ x: new Date(2013, 0), y: 12 },
{ x: new Date(2014, 0), y: 10 }
]
},
I looked on the Canvas JS site and they used the following Ajax call example:
var dataPoints = [];
$.getJSON("https://canvasjs.com/services/data/datapoints.php?xstart=1&ystart=10&length=100&type=json", function(data) {
$.each(data, function(key, value){
dataPoints.push({x: value[0], y: parseInt(value[1])});
});
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text:"Rendering Chart with dataPoints from External JSON"
},
data: [{
type: "line",
dataPoints : dataPoints,
}]
});
chart.render();
});
Not sure if this makes a difference but the format of the JSON data that is used in the AJAX call above is:
[[1,12],[2,7],[3,6],[4,6],[5,9],[6,13],[7,12],[8,15],[9,14],[10,18]]
EDIT
https://jsfiddle.net/uvac6rdz/2/
with edited data format to match OP's preferred axisX.
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Composition of Internet Traffic in North America",
horizontalAlign: "right"
},
axisX: {
tickThickness: 0,
interval: 1,
intervalType: "year"
},
animationEnabled: true,
toolTip: {
shared: true
},
axisY: {
lineThickness: 0,
tickThickness: 0,
interval: 20
},
legend: {
verticalAlign: "center",
horizontalAlign: "left"
},
data: [
{
name: "Real-Time",
showInLegend: true,
type: "column",
color: "#004B8D ",
indexLabel: "{y}",
dataPoints: [
{ x: new Date(2009,0), y: 12 },
{ x: new Date(2010,0), y: 7 },
{ x: new Date(2011,0), y: 6},
{ x: new Date(2012,0), y: 6},
{ x: new Date(2013,0), y: 9},
{ x: new Date(2014,0), y: 13},
{ x: new Date(2015,0), y: 12},
{ x: new Date(2016,0), y: 15},
{ x: new Date(2017,0), y: 14}
]
},
]
});
chart.render();
I changed the following:
the type on your axisX.intervalType
from "year"
to "number"
(due to your sample data).
formatted your sample data from [[1,12],[2,7]...
to follow
[{x: 1, y: 12}, {x: 2, y: 7},...
chart type from stackedColumn100
to column
.
Looking at the documentation for canvasJS, the sample requires you to format the data to be following:
{x: valueX, y: valueY}