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

A better fix for stacked bar charts with log axes #4010

Merged
merged 2 commits into from
Mar 21, 2017

Conversation

etimberg
Copy link
Member

Still resolves the original issue, #3585 while also fixing the issue identified for normal stacked charts when the Y axis has a user defined minimum value

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

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

Looks good, but I don't understand how this change fixes both issues! Also, why the existing tests needed to be updated?

@etimberg
Copy link
Member Author

In terms of the axes I think something about the range changed and as a result, the axis has a different max and min value.

It fixes both because Chart.scale.getBasePixel doesn't consider data values so for the first dataset we don't have to worry about the 0 for log axes. I think there actually might be one case where this solution still fails. If the y axis is log, and it is stacked, and the value in the first dataset is 0, the second dataset might still have this problem. I will check this case, but if I am right this PR won't actually work 😢

@simonbrunel
Copy link
Member

Shouldn't this issue be fixed on the logarithmic scale side instead? Controllers should not be aware of the scale internal logic so it's a bit weird to try to "prevent" the scale to fail in the bar controller with some special cases (how it's handled by other chart types?). Maybe the best way to fix that issue is to consider any value "valid" from the controller perspective and handle special cases (such as 0 for log) in scales?

@etimberg
Copy link
Member Author

I think that is the correct way to deal with this so I will close this PR and find a solution that works without changing the bar controller.

@etimberg etimberg closed this Mar 18, 2017
@etimberg etimberg reopened this Mar 18, 2017
@simonbrunel
Copy link
Member

We can keep working on that PR though :) Also, I think the controller need to be fixed because of the original value which should not be added at every iterations.

@etimberg
Copy link
Member Author

Yup, I realized that after closing :P Will look into the changes now :)

…e axis has a user defined minimum value.
@etimberg etimberg force-pushed the fix/stacked-logarithmic-part2 branch from 69b763a to 807b723 Compare March 18, 2017 15:32
@etimberg
Copy link
Member Author

The 2 commits that I pushed fix the issue without breaking the stacked linear axes and without adding log specific code to the bar controller.

In terms of how 0s are handled in the log axis, I think we should brainstorm on what the best solution is. Right now we have https://github.com/chartjs/Chart.js/blob/master/src/scales/scale.logarithmic.js#L201-L219 which adds a '0' tick mark below the minimum value but maybe we need to consider something more general

@@ -726,7 +726,7 @@ describe('Logarithmic Scale tests', function() {
expect(yScale.getPixelForValue(80, 0, 0)).toBeCloseToPixel(32); // top + paddingTop
expect(yScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(484); // bottom - paddingBottom
expect(yScale.getPixelForValue(10, 0, 0)).toBeCloseToPixel(246); // halfway
expect(yScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(32); // 0 is invalid. force it on top
expect(yScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(484); // 0 is invalid. force it on bottom
Copy link
Member

Choose a reason for hiding this comment

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

Is it a breaking change?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the old behaviour (0 at the top) was wrong for log Y axes

@@ -112,10 +112,9 @@ module.exports = function(Chart) {
var me = this;
var meta = me.getMeta();
var yScale = me.getScaleForId(meta.yAxisID);
var base = yScale.getBaseValue();
var original = base;
var base = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Not sure but I would tend to think that base still need to be initialized to the scale base value because when not stacked this method returns getBasePixel() which is getPixelForValue(getBaseValue()).

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, I will fix that. Good catch!

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 refactoring the bar controller and after a few tests, var base = 0; may be the correct way to calculate the bar positions! You can try it by setting a min on the scale. But that also means return scale.getBasePixel() is wrong.

Seems 2.5 is totally broken when stacked bar and a scale min value defined.

Copy link
Member Author

Choose a reason for hiding this comment

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

getBasePixel() will always clip to the screen, so if the min is 50 and the max is 100, it will return 50 for the base pixel even though the bar actually starts as 0. Before we had clipping implemented this was needed because otherwise the bar drew over the axis.

@simonbrunel simonbrunel added this to the Version 2.6 milestone Mar 19, 2017
@etimberg etimberg merged commit 36ccf40 into master Mar 21, 2017
@etimberg etimberg deleted the fix/stacked-logarithmic-part2 branch March 21, 2017 00:38
roicos pushed a commit to roicos/Chart.js that referenced this pull request Aug 21, 2017
* Undo fix for chartjs#3585 since it has broken the stacked bar charts when the axis has a user defined minimum value.

* When a value of 0 is requested for a vertical logarithmic axis, return the bottom point
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
* Undo fix for chartjs#3585 since it has broken the stacked bar charts when the axis has a user defined minimum value.

* When a value of 0 is requested for a vertical logarithmic axis, return the bottom point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants