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

Min value is not well calculated #52

Closed
TheTisiboth opened this issue Nov 29, 2021 · 4 comments
Closed

Min value is not well calculated #52

TheTisiboth opened this issue Nov 29, 2021 · 4 comments

Comments

@TheTisiboth
Copy link

TheTisiboth commented Nov 29, 2021

Hello everyone, and first of all, thanks for the work, this plugin is really useful.

I am using it in a React project, and I managed to make it work with typescript as well (it took me some days haha)
But now, I observe that the minimum value of my X axis is "wrong:
Screenshot 2021-11-29 092349

Actually, it's not totally wrong, because this min value is the overall min value for the whole datasets. I can verify this by hiding the dataset packaging:
Screenshot 2021-11-29 092418

So is there a way to recompute the min value, everytime we show/hide a dataset, not to display the overall min, but to have the min according to the showed dataset? So in my example, the effective min in the first example should be -20%, and if I hide the dataset packaging, it should be recomputed, to be -100%
It would avoid to have the chart shown on just half of the screen, like on the first screenshot

Thanks for your help!

EDIT:
I can see that this -100% is actually maybe not linked to the values itself, but simply because I have negative values, as this comments suggests: #30
But anyway, I'm still interested for some hints about this !

EDIT 2: I also observed that if I update my code, by just adding a blank line (for instance), then save, It will update my react website, and then it will have the wanted behavior (with dynamic min value computed each time a dataset is hidden/shown).
So it is not a fix, but I imagine that having the wanted behavior by default should be easy

Here is a reproducible example (it's maybe a bit messy since this is under development, but If you need me to comment something, let me know)

@TheTisiboth
Copy link
Author

TheTisiboth commented Nov 29, 2021

I found a workaround, but it is rather temporary I would say, since it is not elegant at all.
Anyway, as I said, I get the expected behavior if my react component is refreshed (whenever I update the source code).
So I thought that I could programmatically achieve this, and I found this snippet on the web, which is doing it:

const [, updateState] = useState();
const forceUpdate = useCallback(() => updateState({}), []);

Then, I created a simple plugin that is forcing the update of my component once at the beginning, and that does the trick:

const updatePlugin: Plugin<"bar"> = {
        id: "updatePlugin",
        beforeInit: () => {
            // It will update my component once, just before the chart is initialized
            forceUpdate()
        }
    }

So this is maybe just a React related issue, I'm not sure. If you think that this issue is not related to your plugin, I can of course delete it, no problem!

@y-takey
Copy link
Owner

y-takey commented Nov 29, 2021

Hi @TheTisiboth , thanks for the issue and a reproducible example :)
I will look into it.

y-takey added a commit that referenced this issue Nov 30, 2021
@y-takey
Copy link
Owner

y-takey commented Nov 30, 2021

This is expected behavior, but made negative scale variable.
You can use fixNegativeScale option, after update this plugin to v1.1.0.

<Bar data={data} options={{ plugins: { stacked100: { enable: true, fixNegativeScale: false } } }} />

Thanks for your pointing :)

@TheTisiboth
Copy link
Author

TheTisiboth commented Nov 30, 2021

Oh great, that is exactly what I was expecting, thank you a lot for your quick fix, that was really fast!!!

@y-takey y-takey closed this as completed Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants