在ChartJs的数据集字段中输入数据集数组

时间:2017-02-27 15:05:08

标签: chart.js

有没有办法可以创建数据集对象数组并将其直接传递到图表的构造中,而不是手动传入数据集对象,如下所示:

这有效:

 myChart[0] = new Chart(ctx, {
type: barbarblackbar,
data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [xdata, ydata]
    },
options: {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero:true
            }
        }]
    }
}
});

这不是:

var datData = { ydata, xdata };

myChart[0] = new Chart(ctx, {
type: barbarblackbar,
data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: datData
    },
options: {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero:true
            }
        }]
    }
}
});

我知道这可能是一个简单的修复,但我已经尝试了我能想到的一切

1 个答案:

答案 0 :(得分:0)

如果将datData的定义从对象更改为数组,则第二种方法可以正常工作:

var datData = [ ydata, xdata ];

myChart[0] = new Chart(ctx, {
type: barbarblackbar,
data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: datData
    },
options: {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero:true
            }
        }]
    }
}
});