gpt4 book ai didi

javascript - CanvasJS 图表 axisX 未定义

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:18 24 4
gpt4 key购买 nike

我有一个 CanvasJS 图表,我想动态设置它的 stripLine。

chart = new CanvasJS.Chart("chart",
{
backgroundColor: "rgba(0,0,0,0)",
legend: {
fontColor: "#FFF"
},
axisX: [{
title: "Test axisX title",
stripLines: [
{
value: 250,
color: "#FF0000"
}
],
}],
data: [] //Later add datapoints
});

设置stripLine位置的函数:

var setVerticalLine = function (xPos) {
if (typeof chart !== 'undefined') {
console.log(JSON.stringify(chart)); //debug info
console.log("chart.axisX type: " + typeof chart.axisX); //debug info
chart.axisX[0].stripLines[0].set("value", xPos);
}};

渲染后我调用该函数,但设置不成功。
错误:

Uncaught TypeError: Cannot read property '0' of undefined..."
The debug info: "chart.axisX type: undefined". The dumped chart object contains the axisX: "..."axisX":[{"title":"Test axisX title","stripLines":[{"value":250,"color":"#FF0000"}]}]...

当我在渲染的图表上看到带有标题和带状线的 axisX 时,它怎么会未定义?为什么我无法访问和修改?有人可以帮我解决这个问题吗?

最佳答案

根据documentation , '在使用 set 方法之前应先渲染图表'。在调用 setVerticalLine 之前渲染图表应该可以正常工作。

这是工作代码:

var chart = new CanvasJS.Chart("chart", {	
backgroundColor: "rgba(0,0,0,0)",
legend: {
fontColor: "#FFF"
},
axisX: [{
title: "Test axisX title",
stripLines: [
{
value: 250,
color: "#FF0000"
}
],
}],
data: [] //Later add datapoints
});

chart.options.data.push({type: "line", dataPoints: [{x: 200, y: 100}, {x: 300, y: 50}]}); //Dummy datapoints
chart.render();// Render Chart before using set method

var setVerticalLine = function (xPos) {
if (typeof chart !== 'undefined') {
chart.axisX[0].stripLines[0].set("value", xPos);
}
};

setVerticalLine(220); //Update Stripline value to 220
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chart" style="height: 260px; width: 100%;"></div>

关于javascript - CanvasJS 图表 axisX 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52346222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com