Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bar chart option to start bars at the origin, instead of the bottom. #669

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});

Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -2064,19 +2067,32 @@

this.buildScale(data.labels);

this.BarClass.prototype.base = this.scale.endPoint;
this.BarClass.prototype.base = this.getBase();

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);

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.
Expand Down Expand Up @@ -2135,6 +2151,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(
Expand Down Expand Up @@ -2177,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
}));
Expand All @@ -2199,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,
Expand All @@ -2220,7 +2237,7 @@
helpers.each(this.datasets,function(dataset,datasetIndex){
helpers.each(dataset.bars,function(bar,index){
if (bar.hasValue()){
bar.base = this.scale.endPoint;
bar.base = this.getBase();
//Transition then draw
bar.transition({
x : this.scale.calculateBarX(this.datasets.length, datasetIndex, index),
Expand Down Expand Up @@ -3377,3 +3394,5 @@


}).call(this);

// vim: set noet ts=8 sw=8: