繁体   English   中英

如何使用canvasJs绘制图形?

[英]How to plot graph using canvasJs?

在这里,我尝试使用动态数据inputs array data绘制图形

但是我在控制台中遇到一些错误错误: 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> 

当我尝试使用dataPoints:plot1它不起作用

plotplot1具有相同的数据,但是为什么会发生呢? 我使用错误的方法吗? 提前致谢

您的变量plotplot1不相等。 plot是一个数组,但plot1是一个字符串。 CanvasJS dataPoints选项需要一个数组,因此如果给它一个字符串,它将不起作用。

我不知道您为什么决定在这里使用JSON.stringify() ,它将arr转换为字符串。 只需直接使用arr

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

暂无
暂无

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

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