-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Use element options for removeHoverStyle(), even when inheriting #5194
Conversation
Thanks for tracking down what's happening in that issue. The core of the issue appears that candlestick subclasses the bar chart and overrides There's a lot of duplicated code right now between Now that I have more details, I think we should primarily fix this in the candlestick chart. However, it would be nice to fix the code duplication here that I mentioned. Would you be able to update this PR to create a method to apply the default styles to the rectangle model that's used by both |
b9df193
to
c98185e
Compare
I made changes to If a subclass has different default options, the new options can be used by either overriding Example for |
I was only suggesting just to create a method to update the styles. Not the entire rectangle. Setting |
So just |
Just the last few lines that edit the styles starting with |
src/controllers/controller.bar.js
Outdated
this.updateRectangle(rectangle); | ||
}, | ||
|
||
updateRectangle: function(rectangle, options) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about calling this method setDefaultStyles
or something that indicates how the rectangle is being updated
Neither caller of this function passes in the second parameter...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree updateRectangle
is not the best, but I would expect setDefaultStyles
to set all default styles. Is this the case here ? If not, it's a bit misleading.
As for the second parameter, it can be used by a subclass to supply different default options without having to reimplement the complete method.
Like this : loicbourgois/chartjs-chart-financial@b5eb159
It's just an idea, but maybe that's not the best way to do it.
Also, when trying to set a default value like (rectangle, options = null)
, the build fails.
Scriptable options are coming and will hopefully give more power on configuring our charts (I would love to see these I didn't test the following solution, but what about simply store the "previous" state: setHoverStyle: function(element) {
var dataset = this.chart.data.datasets[element._datasetIndex];
var index = element._index;
var custom = element.custom || {};
var model = element._model;
element.$previousStyle = {
backgroundColor: model.backgroundColor,
borderColor: model.borderColor,
borderWidth: model.borderWidth
};
model.backgroundColor = custom.hoverBackgroundColor ? custom.hoverBackgroundColor : helpers.valueAtIndexOrDefault(dataset.hoverBackgroundColor, index, helpers.getHoverColor(model.backgroundColor));
model.borderColor = custom.hoverBorderColor ? custom.hoverBorderColor : helpers.valueAtIndexOrDefault(dataset.hoverBorderColor, index, helpers.getHoverColor(model.borderColor));
model.borderWidth = custom.hoverBorderWidth ? custom.hoverBorderWidth : helpers.valueAtIndexOrDefault(dataset.hoverBorderWidth, index, model.borderWidth);
},
removeHoverStyle: function(element) {
helpers.merge(element._model, element.$previousStyle || {});
delete element.$previousStyle;
} Does that solve your issue with the financial chart? Note that |
Yes, that would work. What do you think about changing to the solution that Simon suggested @loicbourgois ? |
@benmccann you mentioned a bug with |
|
I'm fine with leaving out |
I agree, if someone overrides |
New jsfiddle |
It may be better to fix the failing unit test instead. For each use case (defaults, array options and custom style), it should:
|
src/controllers/controller.bar.js
Outdated
@@ -478,11 +484,13 @@ module.exports = function(Chart) { | |||
var index = rectangle._index; | |||
var custom = rectangle.custom || {}; | |||
var model = rectangle._model; | |||
var rectangleElementOptions = this.chart.options.elements.rectangle; | |||
var options = rectangle.$previousStyle ? rectangle.$previousStyle : this.chart.options.elements.rectangle; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you need to check if $previousStyle
is defined here? it would seem to me that there's no way for it to be undefined
src/controllers/controller.bar.js
Outdated
model.backgroundColor = custom.backgroundColor ? custom.backgroundColor : helpers.valueAtIndexOrDefault(dataset.backgroundColor, index, rectangleElementOptions.backgroundColor); | ||
model.borderColor = custom.borderColor ? custom.borderColor : helpers.valueAtIndexOrDefault(dataset.borderColor, index, rectangleElementOptions.borderColor); | ||
model.borderWidth = custom.borderWidth ? custom.borderWidth : helpers.valueAtIndexOrDefault(dataset.borderWidth, index, rectangleElementOptions.borderWidth); | ||
delete rectangle.$previousStyle; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line also seems like it would be unnecessary. It doesn't hurt to leave it I suppose, but I'm not sure it ever has any effect
This seems like a bug everywhere as soon as you will need to override other controllers? I think I prefer to keep track of these changes in the same PR, more consistent and easier to track later. Better to release 2.7.2 with the full fix anyway. |
If this goes into 2.7.2 I think it needs to be in a single PR. If we want to defer until after 2.7.2, then I'm more open to multiple PRs. |
I'll keep working in this PR then. |
d89c572
I've been able to improve some controllers for With
edit : inverted first and third link. |
@benmccann indeed, my bad. I inverted the 2 Travis logs. Fixed previous comment. |
@loicbourgois I'd love to see this PR get finished if you have some more time to spend on it! |
@loicbourgois will you be able to finish up this PR? |
@loicbourgois any chance you'll be able to finish up this PR? it may be closed as inactive if you're not going to come back to it |
Closing as inactive. Please feel free to update and reopen |
Found an issue with
removeHoverStyle()
while working on chartjs-chart-financialSee chartjs/chartjs-chart-financial#24
The element used in
Chart.controllers.bar.removeHoverStyle()
is hardcoded, so if a new class inherits the bar controller and change the default values, these new default values are not applied.I could not figure how to get the element type associated with a controller, so I added a new attribute :
elementType
.It is needed because for the
bar
controller, the element and the controller type are different.For a
candlestick
, controller and element have the same name, so it is not necessary to setelementType
.jsfiddle before : after a hover on the second chart, the border disappears.
jsfiddle after
The difference is in the external resource for Chart.js.