gpt4 book ai didi

javascript - canvasJS 图表无法正确渲染

转载 作者:行者123 更新时间:2023-11-28 06:56:12 24 4
gpt4 key购买 nike

我正在尝试使用 canvasJS 渲染多线图,但它没有按预期工作。

这是我的drawChart函数

function drawChart(obj, placeholder){
var dataPoints = [];
var maxPrice = [];
var minPrice = [];
//console.info(obj);
for (var i = obj.length - 1; i >= 0; i--) {
dataPoints.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:Math.round(obj[i].avgPrice * 1000) / 1000});
minPrice.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:obj[i].minPrice});
maxPrice.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:obj[i].maxPrice});
}
console.log(dataPoints);
console.log(maxPrice);
console.log(minPrice);
var chart = new CanvasJS.Chart("chartContainer",
{
animationEnabled: true,
//theme: "theme1",
zoomEnabled: true,
title:{
text: placeholder
},
data: [
{
type: "spline", //change type to bar, line, area, pie, etc
showInLegend: true,
legendText: "Average Price",
dataPoints: dataPoints //this line I tried to change
},
{
type: "spline",
showInLegend: true,
legendText: "Min Price",
dataPoints: minPrice
},
{
type: "spline",
showInLegend: true,
legendText: "Max Price",
dataPoints: maxPrice
}
],
legend: {
cursor: "pointer",
itemclick: function (e) {
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
} else {
e.dataSeries.visible = true;
}
chart.render();
}
}
});

chart.render();

}

var dataPoints, maxPrice, minPrice 具有相同的布局/数据。

看这里。 enter image description here

它显示的图表是这样的,两行minPrice和maxPrice的重置消失了。

enter image description here

PS如果我将dataPoints: dataPoints更改为dataPoints: min/maxPrice,那么这种方式也不起作用。

最佳答案

我找到了解决方案,我将其发布,以便对其他人有所帮助。

我怀疑,如果您尝试动态渲染图表,那么对于 y 轴,您需要使用 Math 函数才能使其工作。

所以我改变了这些

dataPoints.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:Math.round(obj[i].avgPrice * 1000) / 1000});
minPrice.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:obj[i].minPrice});
maxPrice.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:obj[i].maxPrice});

dataPoints.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:Math.round(obj[i].avgPrice * 1000) / 1000});
minPrice.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:Math.round(obj[i].minPrice * 1000) / 1000});
maxPrice.push({label:obj[i].year+"-"+obj[i].month+"-"+obj[i].day,y:Math.round(obj[i].maxPrice * 1000) / 1000});

现在它工作正常,这可能是 canvasjs 中的一个错误,或者可能是我在某个地方搞砸了。

看这里enter image description here

关于javascript - canvasJS 图表无法正确渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32552906/

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