Chart Legend resets visibility on refresh

Hello,

I have a chart that refreshes every minute to get new data, however I'm running into a problem when the chart refreshes it resets the legend visibilities. 

For example I have this code that set the visibility property to false on Silver and Bronze chart series. If I click on the legend icon, the chart will be visible. However after the chart refreshes with 

_chartObj.RefreshAsync();

 then the chart gets rendered with visibility property to false. Is there a way to keep track of the visibility property and save the chart series visibility even when chart is rerendered?

<SfChart Title="Olympic Medals">
    <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"/>    

    <ChartSeriesCollection>
        <ChartSeries DataSource="@MedalDetails" Name="Gold" XName="Country" Width="2" Opacity="1" YName="Gold" Type="ChartSeriesType.Column"/>      
        <ChartSeries DataSource="@MedalDetails" Name="Silver" XName="Country" Width="2" Opacity="1" YName="Silver" Type="ChartSeriesType.Column" Visible="false"/>     
        <ChartSeries DataSource="@MedalDetails" Name="Bronze" XName="Country" Width="2" Opacity="1" YName="Bronze" Type="ChartSeriesType.Column" Visible="false" />      
    </ChartSeriesCollection>

    <ChartLegendSettings Visible="true"/>
</SfChart>

3 Replies 1 reply marked as answer

DG Durga Gopalakrishnan Syncfusion Team June 17, 2022 03:31 PM UTC

Hi Pavel,

 

Greetings from Syncfusion.

 

We suggest you to update the required chart properties using chart refresh method as per below snippet. We have prepared sample based on your requirement. In an attached sample, we have changed the visibility of chart series to achieve your required scenario.

 

<SfChart @ref="chartInstance">   

    <ChartSeriesCollection>

         //...

        <ChartSeries Visible="@series2"/>

    </ChartSeriesCollection>

</SfChart>
@code{
    public bool series2 = false;
    SfChart chartInstance;
    public async Task OnChartRefresh(MouseEventArgs args)

    {

        series2 = true;

        await chartInstance.Refresh();

    }

}

 

 


 

Kindly revert us if you have any concerns.

 

Regards,

Durga Gopalakrishnan.



Attachment: SeriesVisibility_c7475f17.zip


PA Pavel June 20, 2022 08:24 PM UTC

Is there a way to track clicks on the legend?sfChartRefresh.JPG



DG Durga Gopalakrishnan Syncfusion Team June 21, 2022 02:26 PM UTC

Hi Pavel,

 

You can get the clicked legend item using OnLegendClick event. Please check with the below snippet and documentation link.

 

<SfChart>

       <ChartEvents OnLegendClick="LegendClickEvent"></ChartEvents>

</SfChart>

@code {

    public void LegendClickEvent(LegendClickEventArgs args)

    {   }

}

 

 

Please revert us if you have any concerns.

 

Regards,

Durga Gopalakrishnan.



Marked as answer
Loader.
Up arrow icon