Unb*_*ble 2 javascript charts graph backbone.js chart.js
到目前为止,我已经使用 ChartJS 成功创建了一个折线图。但我想把它做成堆叠图。
以下是我到目前为止所拥有的:
define(['backbone'], function(Backbone)
{
var fifthSubViewModel = Backbone.View.extend(
{
template: _.template($('#myChart5-template').html()),
render: function(){
$(this.el).html(this.template());
var ctx = this.$el.find('#lineChart5')[0];
var lineChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [{
label: "Unavailable Unit",
backgroundColor: "rgba(38, 185, 154, 0.31)",
borderColor: "rgba(38, 185, 154, 0.7)",
pointBorderColor: "rgba(38, 185, 154, 0.7)",
pointBackgroundColor: "rgba(38, 185, 154, 0.7)",
pointHoverBackgroundColor: "#fff",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointBorderWidth: 1,
data: this.model.attributes.unavailThisYear
}, {
label: "Vacant Unit",
backgroundColor: "rgba(3, 88, 106, 0.3)",
borderColor: "rgba(3, 88, 106, 0.70)",
pointBorderColor: "rgba(3, 88, 106, 0.70)",
pointBackgroundColor: "rgba(3, 88, 106, 0.70)",
pointHoverBackgroundColor: "#fff",
pointHoverBorderColor: "rgba(151,187,205,1)",
pointBorderWidth: 1,
data: this.model.attributes.vacThisYear
}]
},
options: {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Income in $'
}
}]
}
}
});
},
initialize: function(){
console.log("In the initialize function");
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
this.render();
}
});
return fifthSubViewModel;
});
Run Code Online (Sandbox Code Playgroud)
目前,我在一个图表上得到两个折线图。但我不知何故想让它们堆叠起来。我的意思是第二个折线图的起点应该是第一个折线图的起点。因此,该区域不应重叠。有什么方法可以告诉我的折线图从另一个折线图结束的值开始,以获得堆叠图。
当前截图未堆叠:
根据最新chartjs版本的文档,您需要在要堆叠它们的轴上将stacked属性设置为true。所以在你的情况下,它将是:
options: {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Income in $'
},
stacked: true
}]
}
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问ChartJs 文档
| 归档时间: |
|
| 查看次数: |
4661 次 |
| 最近记录: |