javascript - Chart.js stacked and grouped bar chart -
is there way stacked , grouped bar chart chart.js library?
it should http://www.highcharts.com/demo/column-stacked-and-grouped
ok, found solution. it's described in github issue , solution in this jsfiddle
chart.defaults.groupablebar = chart.helpers.clone(chart.defaults.bar); var helpers = chart.helpers; chart.controllers.groupablebar = chart.controllers.bar.extend({ calculatebarx: function (index, datasetindex) { // position bars based on stack index var stackindex = this.getmeta().stackindex; return chart.controllers.bar.prototype.calculatebarx.apply(this, [index, stackindex]); }, hideotherstacks: function (datasetindex) { var meta = this.getmeta(); var stackindex = meta.stackindex; this.hiddens = []; (var = 0; < datasetindex; i++) { var dsmeta = this.chart.getdatasetmeta(i); if (dsmeta.stackindex !== stackindex) { this.hiddens.push(dsmeta.hidden); dsmeta.hidden = true; } } }, unhideotherstacks: function (datasetindex) { var meta = this.getmeta(); var stackindex = meta.stackindex; (var = 0; < datasetindex; i++) { var dsmeta = this.chart.getdatasetmeta(i); if (dsmeta.stackindex !== stackindex) { dsmeta.hidden = this.hiddens.unshift(); } } }, calculatebary: function (index, datasetindex) { this.hideotherstacks(datasetindex); var bary = chart.controllers.bar.prototype.calculatebary.apply(this, [index, datasetindex]); this.unhideotherstacks(datasetindex); return bary; }, calculatebarbase: function (datasetindex, index) { this.hideotherstacks(datasetindex); var barbase = chart.controllers.bar.prototype.calculatebarbase.apply(this, [datasetindex, index]); this.unhideotherstacks(datasetindex); return barbase; }, getbarcount: function () { var stacks = []; // put stack index in dataset meta chart.helpers.each(this.chart.data.datasets, function (dataset, datasetindex) { var meta = this.chart.getdatasetmeta(datasetindex); if (meta.bar && this.chart.isdatasetvisible(datasetindex)) { var stackindex = stacks.indexof(dataset.stack); if (stackindex === -1) { stackindex = stacks.length; stacks.push(dataset.stack); } meta.stackindex = stackindex; } }, this); this.getmeta().stacks = stacks; return stacks.length; }, }); var data = { labels: ["january", "february", "march"], datasets: [ { label: "apples", backgroundcolor: "rgba(99,255,132,0.2)", data: [20, 10, 30], stack: 1 }, { label: "bananas", backgroundcolor: "rgba(99,132,255,0.2)", data: [40, 50, 20], stack: 1 }, { label: "cookies", backgroundcolor: "rgba(255,99,132,0.2)", data: [60, 20, 20], stack: 1 }, { label: "apples", backgroundcolor: "rgba(99,255,132,0.2)", data: [20, 10, 30], stack: 2 }, { label: "bananas", backgroundcolor: "rgba(99,132,255,0.2)", data: [40, 50, 20], stack: 2 }, { label: "cookies", backgroundcolor: "rgba(255,99,132,0.2)", data: [60, 20, 20], stack: 2 }, { label: "apples", backgroundcolor: "rgba(99,255,132,0.2)", data: [20, 10, 30], stack: 3 }, { label: "bananas", backgroundcolor: "rgba(99,132,255,0.2)", data: [40, 50, 20], stack: 3 }, { label: "cookies", backgroundcolor: "rgba(255,99,132,0.2)", data: [60, 20, 20], stack: 3 }, ] }; var ctx = document.getelementbyid("mychart").getcontext("2d"); new chart(ctx, { type: 'groupablebar', data: data, options: { legend: { labels: { generatelabels: function(chart) { return chart.defaults.global.legend.labels.generatelabels.apply(this, [chart]).filter(function(item, i){ return <= 2; }); } } }, scales: { yaxes: [{ ticks: { max: 160, }, stacked: true, }] } } });
Comments
Post a Comment