canvasJS图表无法正确呈现

时间:2015-09-13 18:10:32

标签: javascript jquery charts canvasjs

我正在使用render尝试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 and maxPrice的重置消失了。

enter image description here

PS 如果我将dataPoints: dataPoints更改为dataPoints: min/maxPrice,则无论如何都无法正常工作。

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,我发布的内容对其他人有帮助。

我怀疑如果您尝试动态呈现图表,那么对于y-axis,您需要使用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