簡體   English   中英

如何在chart.js中包含許多數據點

[英]How to include many datapoints to plot in chart.js

我試圖使用chart.js繪圖,在這里我基本上想要實現散點線圖這里是從chart.js文檔中取得的示例代碼

var scatterChart = new Chart(ctx, {
type: 'line',
data: {
    datasets: [{
        label: 'Scatter Dataset',
        data: [{
            x: -10,
            y: 0
        }, {
            x: 0,
            y: 10
        }, {
            x: 10,
            y: 5
        }]
    }]
},
options: {
    scales: {
        xAxes: [{
            type: 'linear',
            position: 'bottom'
        }]
    }
}
});

在這個例子中只有3個數據點,但在我的例子中,我有大約100個數據點存儲在2個數組中,即xArray和yArray。 我嘗試在函數中使用for循環,但它無法正常工作。 有人可以幫忙嗎?


編輯:這對我有用。 我創建了一個新數組並將對象(x坐標和y坐標)添加到此數組中。 這是代碼片段。

for(var i=0;i<100;i++)
{
    var obj = {x:xFinal[i],y:yFinal[i]};
    c.push(obj);
}

// Note that here c is a variable declared as var c = [];

嘗試連接數組,如下所示:

//var xArray = [{x: ?,y: ?}, {x: ?,y: ?}];
//var yArray = [{x: ?,y: ?}, {x: ?,y: ?}];
var concatenatedArray = xArray.concat(yArray);

然后,您可以在調用圖表時將其傳遞給:

var scatterChart = new Chart(ctx, {
type: 'line',
data: {
    datasets: concatenatedArray
},
options: {
    scales: {
        xAxes: [{
            type: 'linear',
            position: 'bottom'
        }]
    }
}
});

我希望這有幫助 - 讓我發布!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM