From 75e9a64cd74656c37ae2e9044ebdeb9f5a566712 Mon Sep 17 00:00:00 2001 From: Alexander Huynh Date: Tue, 30 Sep 2014 17:22:28 -0700 Subject: [PATCH 1/4] Add bar chart option to start bars at the origin, instead of the bottom. --- Chart.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Chart.js b/Chart.js index 2c2b608dc3f..37775efb0c4 100644 --- a/Chart.js +++ b/Chart.js @@ -1235,7 +1235,7 @@ return this.base - this.y; }, inRange : function(chartX,chartY){ - return (chartX >= this.x - this.width/2 && chartX <= this.x + this.width/2) && (chartY >= this.y && chartY <= this.base); + return (chartX >= this.x - this.width/2 && chartX <= this.x + this.width/2) && (chartY >= Math.min(this.y, this.base) && chartY <= Math.max(this.y, this.base)); } }); @@ -1963,6 +1963,9 @@ //Number - Width of the grid lines scaleGridLineWidth : 1, + //Boolean - Whether the bars should start at the origin, or the bottom of the scale. + barBeginAtOrigin: true, + //Boolean - If there is a stroke on each bar barShowStroke : true, @@ -2064,13 +2067,21 @@ this.buildScale(data.labels); - this.BarClass.prototype.base = this.scale.endPoint; + if (this.options.barBeginAtOrigin && this.scale.min < 0) { + this.BarClass.prototype.base = (-1 * parseFloat(this.scale.min) / + ((this.scale.max - this.scale.min) * 1.00) * + (this.scale.endPoint - this.scale.startPoint) + + this.scale.startPoint); + } + else { + this.BarClass.prototype.base = this.scale.endPoint; + } this.eachBars(function(bar, index, datasetIndex){ helpers.extend(bar, { width : this.scale.calculateBarWidth(this.datasets.length), x: this.scale.calculateBarX(this.datasets.length, datasetIndex, index), - y: this.scale.endPoint + y: bar.base }); bar.save(); }, this); @@ -2135,6 +2146,7 @@ fontFamily : this.options.scaleFontFamily, valuesCount : labels.length, beginAtZero : this.options.scaleBeginAtZero, + beginAtOrigin : this.options.barBeginAtOrigin, integersOnly : this.options.scaleIntegersOnly, calculateYRange: function(currentHeight){ var updatedRanges = helpers.calculateScaleRange( @@ -2220,7 +2232,12 @@ helpers.each(this.datasets,function(dataset,datasetIndex){ helpers.each(dataset.bars,function(bar,index){ if (bar.hasValue()){ - bar.base = this.scale.endPoint; + if (this.options.barBeginAtOrigin && this.scale.min < 0) { + helpers.noop(); + } + else { + bar.base = this.scale.endPoint; + } //Transition then draw bar.transition({ x : this.scale.calculateBarX(this.datasets.length, datasetIndex, index), From ad71ae7323aa879edb5cc71bec2473461b45c158 Mon Sep 17 00:00:00 2001 From: Alexander Huynh Date: Tue, 30 Sep 2014 19:21:40 -0700 Subject: [PATCH 2/4] Fix mirrored origin --- Chart.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Chart.js b/Chart.js index 37775efb0c4..feffa134b4e 100644 --- a/Chart.js +++ b/Chart.js @@ -2068,10 +2068,10 @@ this.buildScale(data.labels); if (this.options.barBeginAtOrigin && this.scale.min < 0) { - this.BarClass.prototype.base = (-1 * parseFloat(this.scale.min) / + this.BarClass.prototype.base = (this.scale.endPoint - + (-1 * parseFloat(this.scale.min)) / ((this.scale.max - this.scale.min) * 1.00) * - (this.scale.endPoint - this.scale.startPoint) + - this.scale.startPoint); + (this.scale.endPoint - this.scale.startPoint)); } else { this.BarClass.prototype.base = this.scale.endPoint; From ca04f7490510e55b777b2816ce83361f57823c73 Mon Sep 17 00:00:00 2001 From: Alexander Huynh Date: Thu, 13 Nov 2014 16:28:22 -0800 Subject: [PATCH 3/4] Wrap all the base recalculation routines in a method --- Chart.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Chart.js b/Chart.js index feffa134b4e..fcf3ce910a6 100644 --- a/Chart.js +++ b/Chart.js @@ -2067,15 +2067,7 @@ this.buildScale(data.labels); - if (this.options.barBeginAtOrigin && this.scale.min < 0) { - this.BarClass.prototype.base = (this.scale.endPoint - - (-1 * parseFloat(this.scale.min)) / - ((this.scale.max - this.scale.min) * 1.00) * - (this.scale.endPoint - this.scale.startPoint)); - } - else { - this.BarClass.prototype.base = this.scale.endPoint; - } + this.BarClass.prototype.base = this.getBase(); this.eachBars(function(bar, index, datasetIndex){ helpers.extend(bar, { @@ -2088,6 +2080,19 @@ this.render(); }, + getBase : function () { + if (this.options.barBeginAtOrigin && this.scale.min < 0) { + return ( + this.scale.endPoint - + (-1 * parseFloat(this.scale.min)) / + ((this.scale.max - this.scale.min) * 1.00) * + (this.scale.endPoint - this.scale.startPoint) + ); + } + else { + return this.scale.endPoint; + } + }, update : function(){ this.scale.update(); // Reset any highlight colours before updating. @@ -2189,9 +2194,9 @@ value : value, label : label, x: this.scale.calculateBarX(this.datasets.length, datasetIndex, this.scale.valuesCount+1), - y: this.scale.endPoint, + y: this.getBase(), width : this.scale.calculateBarWidth(this.datasets.length), - base : this.scale.endPoint, + base : this.getBase(), strokeColor : this.datasets[datasetIndex].strokeColor, fillColor : this.datasets[datasetIndex].fillColor })); @@ -2211,8 +2216,8 @@ }, reflow : function(){ helpers.extend(this.BarClass.prototype,{ - y: this.scale.endPoint, - base : this.scale.endPoint + y: this.getBase(), + base : this.getBase() }); var newScaleProps = helpers.extend({ height : this.chart.height, @@ -2232,12 +2237,7 @@ helpers.each(this.datasets,function(dataset,datasetIndex){ helpers.each(dataset.bars,function(bar,index){ if (bar.hasValue()){ - if (this.options.barBeginAtOrigin && this.scale.min < 0) { - helpers.noop(); - } - else { - bar.base = this.scale.endPoint; - } + bar.base = this.getBase(); //Transition then draw bar.transition({ x : this.scale.calculateBarX(this.datasets.length, datasetIndex, index), From b57b4907485c5d84396bbc66f99481458a455c69 Mon Sep 17 00:00:00 2001 From: Alexander Huynh Date: Thu, 13 Nov 2014 16:28:37 -0800 Subject: [PATCH 4/4] Add vim defaults --- Chart.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Chart.js b/Chart.js index fcf3ce910a6..16a1dd0bac6 100644 --- a/Chart.js +++ b/Chart.js @@ -3394,3 +3394,5 @@ }).call(this); + +// vim: set noet ts=8 sw=8: