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 and refactor bar controllers #4044

Merged
merged 1 commit into from
Apr 8, 2017
Merged

Conversation

simonbrunel
Copy link
Member

@simonbrunel simonbrunel commented Mar 19, 2017

Merge most of the horizontalBar controller into the bar one and fix stack groups and bar positioning when scales are stacked but also when a min and/or max tick values are explicitly defined. In addition to simplify the whole bar chart logic (which should make codeclimate happier), it decreases the minified size of about 3.7KB.

Important: this is a breaking change for derived controllers that rely on the following removed methods:

// Vertical
calculateBarWidth() -> ruler.barSize;
calculateBarBase() -> return this.calculateBarValuePixels(datasetIndex, index).base;
calculateBarX() -> return this.calculateBarIndexPixels(datasetIndex, index, ruler).center;
calculateBarY() -> return this.calculateBarValuePixels(datasetIndex, index).head;

// Horizontal
calculateBarHeight() -> return ruler.barSize;
calculateBarBase() -> return this.calculateBarValuePixels(datasetIndex, index).base;
calculateBarX() -> return this.calculateBarValuePixels(datasetIndex, index).head;
calculateBarY() -> return this.calculateBarIndexPixels(datasetIndex, index, ruler).base;

@simonbrunel
Copy link
Member Author

@potatopeelings you may be interested to review these changes since it impacts the stacking logic.

* @deprecated
* @private
*/
calculateBarWidth: function(ruler) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@etimberg I'm not sure I would keep these internal deprecated calculateBar* methods: they are not called anymore by the new implementation and most of the time people might have overridden them (which will obviously not work). The new implementation is more optimized and trying to keep compatibility will be slower (and dirty), so I don't think it worth the hack for (a few?) custom implementations using private code (e.g #2726, #3858).

What do you think?

/cc @daviddevore11 @yoerivdm

Copy link
Member

Choose a reason for hiding this comment

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

Right, if they are overridden they won't be called anymore. I think the recommended way to achieve the old behaviour would be to override updateElement to set the properties as expect. I am fine to remove these since they won't do anything and overridding them won't work. We should wait to do anything until we see what users say

borderSkipped: custom.borderSkipped ? custom.borderSkipped : rectangleOptions.borderSkipped,
backgroundColor: custom.backgroundColor ? custom.backgroundColor : helpers.getValueAtIndexOrDefault(dataset.backgroundColor, index, rectangleOptions.backgroundColor),
borderColor: custom.borderColor ? custom.borderColor : helpers.getValueAtIndexOrDefault(dataset.borderColor, index, rectangleOptions.borderColor),
borderWidth: custom.borderWidth ? custom.borderWidth : helpers.getValueAtIndexOrDefault(dataset.borderWidth, index, rectangleOptions.borderWidth)
Copy link
Member

Choose a reason for hiding this comment

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

This check would have failed in the old code for custom.borderWidth === 0? Are we ok with keeping it now? Should we fix it in another PR?

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 would go for another PR, maybe the one supporting functors for these data options.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, sounds good

* @deprecated
* @private
*/
calculateBarWidth: function(ruler) {
Copy link
Member

Choose a reason for hiding this comment

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

Right, if they are overridden they won't be called anymore. I think the recommended way to achieve the old behaviour would be to override updateElement to set the properties as expect. I am fine to remove these since they won't do anything and overridding them won't work. We should wait to do anything until we see what users say

{b: 290, w: 83, x: 202, y: 419},
{b: 290, w: 83, x: 318, y: 161},
{b: 290, w: 83, x: 434, y: 419}
{b: 290, w: 83 / 2, x: 63, y: 161},
Copy link
Member

Choose a reason for hiding this comment

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

previously these bars were the same full width?

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 that was a bug introduced by #3563, it should be half the width because the x scale is not stacked, right?

Copy link
Member

Choose a reason for hiding this comment

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

Yup, just confirming the change 😄

Merge most of the horizontalBar controller into the bar one but also fix stack groups and bar positioning when scales are stacked or when a min and/or max tick values are explicitly defined. Note that this is a breaking change for derived controllers that rely on the following removed methods: `calculateBarBase`, `calculateBarX`, `calculateBarY`, `calculateBarWidth` and `calculateBarHeight`.
@simonbrunel
Copy link
Member Author

simonbrunel commented Mar 25, 2017

@etimberg PR rebased, I also removed the deprecated methods and updated the PR description. May be safer that you checkout and test my branch to confirm that it's good to be merged.

Final minified size improvement: 3.7KB :)

@potatopeelings
Copy link
Contributor

@simonbrunel - looks great! Love the way stackIndex and stackCount methods were merged :-)

var vscale = me.getValueScale();
var base = vscale.getBasePixel();
var horizontal = vscale.isHorizontal();
var ruler = me._ruler || me.getRuler();
Copy link
Contributor

@potatopeelings potatopeelings Mar 25, 2017

Choose a reason for hiding this comment

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

I assume this || me.getRuler() is there because of addElementAndReset calling updateElement - would moving line 94 to updateElement and injecting ruler into updateElementGeometry instead make that clearer?

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 didn't even notice that update() wasn't called in certain cases, good point! Your suggestion was my first implementation but I did this || me.getRuler() in updateElementGeometry in case update() or updateElement() has been overridden, in which case it will still build the ruler (having it built only once per update is a great optimization). Though, if update() has been overridden, the ruler will be created but never updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Um... if updateElementGeometry is private, the two ways it can get called if updateElement is overridden are

  1. Someone uses the private updateElementGeometry method in a plugin or custom override (not expecting it to work across version changes)
  2. One of the existing bar methods uses it in future (none do right now)

So, we want to handle one / both of these possibilities?

And thinking back on my comment, I'm ok with its current position at line 94 - we can easily change it in future easily (if needed) since the method is private :-)

@etimberg
Copy link
Member

@simonbrunel I tested it out and didn't notice anything :)

@simonbrunel simonbrunel mentioned this pull request Apr 7, 2017
@etimberg etimberg merged commit 50a80da into chartjs:master Apr 8, 2017
@simonbrunel simonbrunel deleted the bar-refactor branch April 14, 2017 07:52
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.

3 participants