简体   繁体   English

如何使用canvasJs绘制图形?

[英]How to plot graph using canvasJs?

Here I try to plot a graph using dynamic data inputs array data 在这里,我尝试使用动态数据inputs array data绘制图形

But I'm getting some error in the console Error: canvasjs.min.js:197 Uncaught TypeError: Cannot read property 'getTime' of undefined 但是我在控制台中遇到一些错误错误: canvasjs.min.js:197 Uncaught TypeError: Cannot read property 'getTime' of undefined

 window.onload = function () { function setObject(name) { this.y = name; } var inputs=[1,23,21,2,67,54] var arr = []; for (var i = 0; i < inputs.length; i++) { var cookieValue = inputs[i]; var setObj = new setObject(cookieValue); arr.push(setObj); } plot1 = JSON.stringify(arr, null, 2); var plot = [{ "y":1},{ "y":23},{ "y":21},{ "y":2},{ "y":67},{ "y":54}] var graph = [{ type: "line", //when i try to use plot1 instead of plot then graph not showing dataPoints:plot }] var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, theme: "light2", title:{ text: "Simple Line Chart" }, axisY:{ includeZero: false }, data: graph }); chart.render(); } 
 <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script> <div id="chartContainer" style="height: 300px; width: 100%;"></div> 

When I try to use dataPoints:plot1 then it's not working 当我尝试使用dataPoints:plot1它不起作用

plot and plot1 have same data but Why it's happening? plotplot1具有相同的数据,但是为什么会发生呢? am I using the wrong approach? 我使用错误的方法吗? Thanks In advance 提前致谢

Your variables plot and plot1 are not equivalent. 您的变量plotplot1不相等。 plot is an array, but plot1 is a string. plot是一个数组,但plot1是一个字符串。 CanvasJS dataPoints option expects an array, so if you give it a string, it doesn't work. CanvasJS dataPoints选项需要一个数组,因此如果给它一个字符串,它将不起作用。

I don't know why you decided to use JSON.stringify() here, which converts your arr to a string. 我不知道您为什么决定在这里使用JSON.stringify() ,它将arr转换为字符串。 Just use arr directly! 只需直接使用arr

var graph = [{
    type: "line",
    dataPoints: arr
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM