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

Correctly handle stacked groups when not adjacent #4937

Merged
merged 2 commits into from Nov 14, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/controllers/controller.bar.js
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.
* @param {Number=} [last] - The dataset index
* @returns {Array} The stack list
* @private
*/
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.
* @param {Number=} [datasetIndex] - The dataset index
Copy link
Member

Choose a reason for hiding this comment

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

[nit] what the purpose of = in {Number=}?

Copy link
Contributor

Choose a reason for hiding this comment

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

Indicates that it's optional: http://usejsdoc.org/tags-type.html

Though I don't think we need both the equals and square brackets. That would seem redundant to me. I would think we should just use one or the other. I was more familiar with the = syntax

Copy link
Member

Choose a reason for hiding this comment

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

Didn't take time to look around, thinking it was a typo, thanks @benmccann! I agree, it's redundant, and I think we already adopted the other form {Number} [datasetIndex] - ..., so I would continue with that one.

* @param {String=} [name] - The stack name to find
* @returns {Number} The stack index
* @private
*/
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 Down