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

Fix for issues #4897, #4907 #4911

Closed
wants to merge 1 commit 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: 28 additions & 7 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ module.exports = function(Chart) {
},

/**
* Returns the effective number of stacks based on groups and bar visibility.
* Returns the stacks based on groups and bar visibility.
* @private
* @param {number=} [last] - The dataset index
* @returns {Array} [stacks] - The stack list
*/
getStackCount: function(last) {
getStacks: function(last) {
var me = this;
var chart = me.chart;
var scale = me.getIndexScale();
Expand All @@ -223,15 +225,33 @@ module.exports = function(Chart) {
}
}

return stacks.length;
return stacks;
},

/**
* Returns the effective number of stacks based on groups and bar visibility.
* @private
*/
getStackCount: function() {
return this.getStacks().length;
},

/**
* Returns the stack index for the given dataset based on groups and bar visibility.
* @private
* @param {number=} [datasetIndex] - The dataset index
* @param {String=} [name] - The stack name to find
* @returns {number} [index] - The stack index
*/
getStackIndex: function(datasetIndex) {
return this.getStackCount(datasetIndex) - 1;
getStackIndex: function(datasetIndex, name) {
var stacks = this.getStacks(datasetIndex);
var index = (name !== undefined)
? stacks.indexOf(name)
: -1; // indexOf returns -1 if element is not present

return (index === -1)
? stacks.length - 1
: index;
},

/**
Expand Down Expand Up @@ -312,7 +332,8 @@ module.exports = function(Chart) {
calculateBarIndexPixels: function(datasetIndex, index, ruler) {
var me = this;
var options = ruler.scale.options;
var stackIndex = me.getStackIndex(datasetIndex);
var meta = me.getMeta();
var stackIndex = me.getStackIndex(datasetIndex, meta.stack);
var pixels = ruler.pixels;
var base = pixels[index];
var length = pixels.length;
Expand All @@ -322,7 +343,7 @@ module.exports = function(Chart) {

if (length === 1) {
leftSampleSize = base > start ? base - start : end - base;
rightSampleSize = base < end ? end - base : base - start;
rightSampleSize = leftSampleSize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, which part of the issue does this fix?

Copy link
Contributor Author

@jcopperfield jcopperfield Nov 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fixes: #4907: Bar stretched over multiple labels for single value dataset.

This code is part of a special case for length === 1, in the other cases where index === 0 or index === length - 1, which is always the case, both sample sizes are set to the same value.

} else {
if (index > 0) {
leftSampleSize = (base - pixels[index - 1]) / 2;
Expand Down