Reputation: 226
I am just trying to plot some basic X and Y points on a line graph being gathered from some PHP.
On a form submit I do a ajax post to get the points for the graph. Then run a JS function which should create the graph. I keep getting "this.scale is undefined" and the graph shows nothing/crashes.
HTML
<canvas id="cv" height="50"></canvas>
EDIT:
This is the JSON being set in PHP being sent back from the post. The $dates is just the below dates same with $num_added is the below data points.
$ds['labels'] = $dates;
$ds['datasets'][]=array('label' => $tid,'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' => $num_added);
print json_encode($ds);
Ajax post which returns the JSON object of
{
"labels":[ "2016-01-10","2016-01-12","2016-01-13","2016-01-14","2016-01-15","2016-01-16","2016-01-17","2016-01-18","2016-01-19","2016-01-21","2016-01-22","2016-01-24","2016-01-26","2016-01-27","2016-01-29","2016-01-30","2016-01-31","2016-02-01","2016-02-02","2016-02-03","2016-02-04","2016-02-05","2016-02-06","2016-02-07","2016-02-08","2016-02-10"],
"datasets":[
{
"label":"XYZ",
"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":["1483","2044","834","818","1215","1534","823","1368","3048","4808","4486","6488","1340","4260","1863","5309","1259","858","4041","7444","4514","3994","3574","1419","1383","1825"]
}
]}
This is the same format of the example the give you online which is why I am stumped in figuring this out
Once I get the points I sent them to my js function line_graph("cv",data) to graph the line but this is where it crashes. Which doesnt make sense to me because I got it right from thr documentation.
function line_graph(holder, data){
Chart.defaults.global.animation = true;
Chart.defaults.global.animationSteps = 60;
Chart.defaults.global.animationEasing = "easeOutQuart";
Chart.defaults.global.showScale = true;
Chart.defaults.global.scaleOverride = false;
Chart.defaults.global.scaleLineColor ="rgba(0,0,0,.1)";
Chart.defaults.global.scaleLineWidth = 1;
Chart.defaults.global.scaleShowLabels = true;
Chart.defaults.global.scaleLabel = "<%=value%>";
Chart.defaults.global.scaleIntegersOnly = true;
Chart.defaults.global.scaleBeginAtZero = false;
Chart.defaults.global.scaleFontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
Chart.defaults.global.scaleFontSize = 12;
Chart.defaults.global.scaleFontStyle = "normal";
Chart.defaults.global.scaleFontColor = "#666";
Chart.defaults.global.responsive = true;
Chart.defaults.global.maintainAspectRatio = true;
Chart.defaults.global.showTooltips = true;
Chart.defaults.global.tooltipEvents = ["mousemove", "touchstart", "touchmove"];
Chart.defaults.global.tooltipFillColor = "rgba(0,0,0,0.8)";
Chart.defaults.global.tooltipFontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
Chart.defaults.global.tooltipFontSize = 14;
Chart.defaults.global.tooltipFontStyle = "normal";
Chart.defaults.global.tooltipFontColor = "#fff";
Chart.defaults.global.tooltipTitleFontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
Chart.defaults.global.tooltipTitleFontSize = 14;
Chart.defaults.global.tooltipTitleFontStyle = "bold";
Chart.defaults.global.tooltipTitleFontColor = "#fff";
Chart.defaults.global.tooltipYPadding = 6;
Chart.defaults.global.tooltipXPadding = 6;
Chart.defaults.global.tooltipCaretSize = 8;
Chart.defaults.global.tooltipCornerRadius = 6;
Chart.defaults.global.tooltipXOffset = 10;
Chart.defaults.global.tooltipTemplate = "<%if (label){%><%=label%>: <%}%><%= value %>";
Chart.defaults.global.multiTooltipTemplate = "<%= value %>";
var ctx = document.getElementById(holder).getContext("2d");
var myLineChart = new Chart(ctx).Line(data, {
scaleShowGridLines : true,
scaleGridLineColor : "rgba(0,0,0,.05)",
scaleGridLineWidth : 1,
scaleShowHorizontalLines: true,
scaleShowVerticalLines: true,
bezierCurve : true,
bezierCurveTension : 0.4,
pointDot : true,
pointDotRadius : 4,
pointDotStrokeWidth : 1,
pointHitDetectionRadius : 20,
datasetStroke : true,
datasetStrokeWidth : 2,
datasetFill : true,
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
});
}
The graph works if i just hard code the data into the function but obviously that's worthless. I have literally tried everything I can think of so I figure someone may have run into this problem. Its a simple ajax to get the data and then display the points which is annoying because its a simple process. I know I am just missing somthing this being my first time with chart.js
Upvotes: 1
Views: 3332
Reputation: 226
I figured it out after about 2 days. Its pretty annoying but I guess it was looking at the JSON as an array and not the object it needed. so i did a parseJson on the returned data and it worked perfectly into the graph....Any way thanks for your help you telling me to do that alert @potatopeelings helped me realize the JSON was not being read properly.
Upvotes: 3