簡體   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