我已经找到了解决方案。对于Chart.js 2.1.x及以上版本(在Chart.js 1.x.x中不起作用),您可以按照下面所示的自定义选项应用数据集:
var options = {
scales:{
xAxes: [{ stacked: true }],
yAxes: [{ stacked: true }]
}
};
var myBarChart = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});
我正在使用来自此源的chart js 2.2.1 gem。第3到35行的帮助方法提供了创建堆叠条形图的见解。但是它没有选项来输入给定方法中的数据。因此,让我们覆盖帮助方法:
def chart2_bar id=nil, size=nil, data={}, options={}
html = "<canvas id=\"myChart_bar\" width=\"#{size[:width]}px\" height=\"#{size[:height]}px\"></canvas>".html_safe
script = javascript_tag do
<<-END.html_safe
var ctx = document.getElementById("myChart_bar");
var data = "#{data.to_json}";
var options = "#{options.to_json}";
var myBarChart = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});
END
end
return html + script
end