javascript - Set a full background color to the canvas in chartjs 2.0 -
basically question similar 1 asked here how set full length background color each bar in chartjs bar
it has desired jsfiddle link http://jsfiddle.net/x73rhq2y/
but not work chartjs 2.* (i using chart.min.bundle.js v2.1.6)
i trying extend bar chart display multiple stacked bar chart. along need separate out bar chart background.
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; }, initialize: function (data) { debugger; var self = data; var originalbuildscale = self.buildscale; self.buildscale = function () { originalbuildscale.apply(this, arguments); debugger; var ctx = self.chart.ctx; var scale = self.scale; var originalscaledraw = self.scale.draw; var barareawidth = scale.calculatex(1) - scale.calculatex(0); var barareaheight = scale.endpoint - scale.startpoint; self.scale.draw = function () { originalscaledraw.call(this, arguments); scale.xlabels.foreach(function (xlabel, i) { ctx.fillstyle = data.labelbackgrounds[i]; ctx.fillrect( scale.calculatex(i - (scale.offsetgridlines ? 0.5 : 0)), scale.startpoint, barareawidth, barareaheight); ctx.fill(); }); }; }; chart.controllers.bar.prototype.initialize.apply(this, arguments); } });
but since documentation has been updated , not able find solution. data array follows:
var data = { labels: ["label1","label2","label2","label2",], labelbackgrounds: ["rgba(120,220,220,0.2)", "rgba(220,120,220,0.2)", "rgba(220,220,120,0.2)", "rgba(120,120,220,0.2)"], datasets: [ { label: "pass", backgroundcolor: "rgba(3,166,120,0.7)", data: [40,50,30,45], stack: 1 }, { label: "fail", backgroundcolor: "rgba(212,82,84,0.7)", data: [40,50,30,45], stack: 1 }, { label: "not run", backgroundcolor: "rgba(89,171,227,0.7)", data: [40,50,30,45], stack: 1 }, { label: "pass", backgroundcolor: "rgba(3,166,120,0.7)", data: [40,50,30,45], stack: 2 }, { label: "fail", backgroundcolor: "rgba(212,82,84,0.7)", data: [40,50,30,45], stack: 2 }, { label: "not run", backgroundcolor: "rgba(89,171,227,0.7)", data: [40,50,30,45], stack: 2 } ] };
p.s: had create new question reputation not allows me comment on https://stackoverflow.com/a/31833017/4787971
Comments
Post a Comment