We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Chart not updated when datasource (List) is updated and StateHasChanged() is called.

Created a razor component by customizing a SfChart. Defined a DataSource for the ChartSeries. When I modify an object in the List and then call StateHasChanged(), the chart does not update. If I clear the list, then rebuild the list and call StateHasChanged(), then the chart updates as expected.

public List<ChartData> StaticData = new List<ChartData>

    {

        new ChartData {X = "1", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "2", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "3", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "4", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "5", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "6", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "7", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "8", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "9", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "10", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "11", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "12", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "13", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "14", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0},

        new ChartData {X = "15", Nominal = 45, LCL = 44.7, UCL = 45.3, Y = 45.0}

    };

The chart series sets the DataSource to StaticData as defined above.

<SfChart @ref="Chart" Title="Shaft Diameter" Width="2000px" Height="1000px">

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

    <ChartPrimaryXAxis Name="SPC Data" ValueType="Syncfusion.Blazor.Charts.ValueType.Category" Minimum="0" Maximum="14" Title="Sample">

    </ChartPrimaryXAxis>


    <ChartPrimaryYAxis OpposedPosition=true Title="@yAxisCfg.title" LabelFormat="n3" Minimum="@yAxisCfg.minimum" Maximum="@yAxisCfg.maximum" Interval="@yAxisCfg.interval">

        <ChartAxisLineStyle Width="0" ></ChartAxisLineStyle>

        <ChartAxisMajorTickLines Width="0" ></ChartAxisMajorTickLines>

    </ChartPrimaryYAxis>

    <ChartTooltipSettings Enable="true"></ChartTooltipSettings>

    <ChartSeriesCollection>

        <ChartSeries Name="SPC Data" DataSource="@StaticData" XName="X" YName="Y" Type="ChartSeriesType.Line" Width="3" >

            <ChartMarker Visible="true" Width="7" Height="7" Shape="ChartShape.Circle">

                <ChartDataLabel Visible="false"/>

            </ChartMarker>

        </ChartSeries>

        <ChartSeries Name="UCL" DataSource="@StaticData" XName="X" YName="UCL" Type="ChartSeriesType.Line "Width="3" DashArray="10,10">

            <ChartMarker Visible="false" Width="7" Height="7" Shape="ChartShape.Pentagon">

            </ChartMarker>

        </ChartSeries>

        <ChartSeries Name="Nominal" DataSource="@StaticData" XName="X" YName="Nominal" Type="ChartSeriesType.Line" Width="3" DashArray="5,5">

            <ChartMarker Visible="false" Width="7" Height="7" Shape="ChartShape.Diamond">

            </ChartMarker>

        </ChartSeries>

        <ChartSeries Name="LCL" DataSource="@StaticData" XName="X" YName="LCL" Type="ChartSeriesType.Line" Width="3" DashArray="10, 10">

            <ChartMarker Visible="false" Width="7" Height="7" Shape="ChartShape.Pentagon">

            </ChartMarker>

        </ChartSeries>

    </ChartSeriesCollection>

    <ChartLegendSettings Visible="true" Position=LegendPosition.Custom ></ChartLegendSettings>

</SfChart>


Calling ModifyList() changes the value of StaticData, but StateHasChanged() does not update the chart.


    public void ModifyList()

    {

        StaticData[5].Y = nomValue + (5 * sigma);

        StateHasChanged();

    }



Attachment: ChartListDemo_d88bf5de.zip

3 Replies

DG Durga Gopalakrishnan Syncfusion Team October 11, 2022 02:33 PM UTC

Hi Wayne,


Greetings from Syncfusion.


We suggest you to use ObservableCollection as datasource and INotifyPropertyChanged interface to update the data in a datasource. We have prepared sample based on your requirement.


public class LineChartData : INotifyPropertyChanged

    {

       double eng_InflationRate { get; set; }

        public double ENG_InflationRate { get { return eng_InflationRate; }

            set

            {

                this.eng_InflationRate = value;

                NotifyPropertyChanged("ENG_InflationRate");

            }

        }

       public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)

        {

            if (PropertyChanged != null)

            {

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

        }

    }

    public void ChangeData()

    {

        this.ChartData[3].ENG_InflationRate = 70;

    }


Before Update



After Update


Sample : https://www.syncfusion.com/downloads/support/directtrac/general/ze/UpdateData1013559496.zip


UG : https://blazor.syncfusion.com/documentation/common/data-binding/data-updates#inotifypropertychanged


Please revert us if you have any concerns.


Regards,

Durga Gopalakrishnan.



WA Wayne October 12, 2022 12:06 PM UTC

Thanks. I modified my code to use ObservableCollection. That works as you described.




DG Durga Gopalakrishnan Syncfusion Team October 13, 2022 11:09 AM UTC

Hi Wayne,


Most welcome. Please get back to us if you need any further assistance. We are always happy in assisting you.


Regards,

Durga Gopalakrishnan


Loader.
Live Chat Icon For mobile
Up arrow icon