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

If hidden, charts never come back #163

Open
Gabriel-RABHI opened this issue Nov 15, 2020 · 4 comments
Open

If hidden, charts never come back #163

Gabriel-RABHI opened this issue Nov 15, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@Gabriel-RABHI
Copy link

Hi,

The chart is well displayed the first time. If not displayed and displayed again, an empty small html canvas remain on the page and the chart will never be visible again. To reproduce this bug i have modified the "Basic.cs" sample to add a toogle button :

@page "/charts/line/basic"
@using ChartJs.Blazor.LineChart
@layout SampleLayout


<button @onclick="SwitchShow">Toogle</button>
@if (_show)
{
<Chart Config="_config" @ref="_chart"></Chart>
}

<button @onclick="RandomizeData">Randomize Data</button>
<button @onclick="AddDataset">Add Dataset</button>
<button @onclick="RemoveDataset">Remove Dataset</button>
<button @onclick="AddData">Add Data</button>
<button @onclick="RemoveData">Remove Data</button>

@code {
    private bool _show = true;
    public void SwitchShow()
    {
        _show = !_show;
        _chart.
    }

I've seen that another dev have had the problem, but it seems the bug is here again !

Thank you !

@Gabriel-RABHI Gabriel-RABHI added the bug Something isn't working label Nov 15, 2020
@larschristensen20
Copy link

Did you try calling StateHasChanged() in your

public void SwitchShow()
    {
        _show = !_show;
        _chart.
    }

method after _show = !_show;

It tells Blazor to re-render the UI.

@Gabriel-RABHI
Copy link
Author

Gabriel-RABHI commented Nov 17, 2020

Something like that do not work :

public void SwitchShow()
{
_show = !_show;
_chart.Update();
StateHasChanged();
}

But I've found a hack : rebuilding the entire Config to force the component to call the JS Interop again. By specifying a new Config object, the component seems to track a change that induce a new JS Interop call and then the charts is rebuilded client side.

Add a toogle button in any sample and you'll see the problem. It is really disturbing the first time, but it seems to be a classic drawback of JS based components in blazor...

(a guess that's why real native blazor components are better in terme of efficiency, productivity and reliability)

@larschristensen20
Copy link

Yeah I just tried in my own project, and there is definitely something amiss here, my chart does not re-render just like you confirmed. A possible work-around might be to create a targeted JS interop to the method needed to show the chart (if you can find it, that is).

Sorry I can't help much more than that :D

@Joelius300
Copy link
Contributor

Hey @Gabriel-RABHI, thanks for the issue :)

You've observed correctly, I can easily reproduce the problem. The issue is that blazor will completely remove the canvas from the dom when it's hidden (AFAIK).

I think we would have to detect when the chart gets removed from the dom and re-initialize the chart when that happens (potentially without playing animations since it isn't the first render).

Does anyone have an idea how to do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants