Articles in this section

How to change series visibility using a checkbox in Blazor Charts?

This article explains how to change series visibility using a checkbox in Blazor Charts.

Change Series Visibility Using a Checkbox

Blazor Charts provides an option to change series visibility using a checkbox.

This can be achieved by utilizing the Visible property of the ChartSeries to change the series visibility using a checkbox.

The below code example demonstrates how to change series visibility using a checkbox.

Index.razor

@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor.Buttons

<SfCheckBox Label="Vietnam" @bind-Checked="isChecked1" @onchange="onChange1"></SfCheckBox>
<SfCheckBox Label="Canada" @bind-Checked="isChecked2" @onchange="onChange2"></SfCheckBox>
<SfCheckBox Label="Malaysia" @bind-Checked="isChecked3" @onchange="onChange3"></SfCheckBox>

<SfChart Title="Crude Steel Production Annual Growth">
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category" />
   <ChartPrimaryYAxis Minimum="0" Maximum="20" Interval="4" />
   <ChartSeriesCollection>
       <ChartSeries DataSource="@ChartData" Visible="@Visibility1" XName="Period" Width="2"
                    Opacity="1" YName="Viet_Growth" Type="ChartSeriesType.Line">
           <ChartMarker Visible="true" Width="7" Height="7" IsFilled="true" Shape="ChartShape.Circle">
           </ChartMarker>
       </ChartSeries>
       <ChartSeries DataSource="@ChartData" Visible="@Visibility2" XName="Period" Width="2"
                    Opacity="1" YName="Can_Growth" Type="ChartSeriesType.Line">
           <ChartMarker Visible="true" Width="6" IsFilled="true" Height="6" Shape="ChartShape.Triangle">
           </ChartMarker>
       </ChartSeries>
       <ChartSeries DataSource="@ChartData" Visible="@Visibility3" XName="Period" Width="2"
                    Opacity="1" YName="Mal_Growth" Type="ChartSeriesType.Line">
           <ChartMarker Visible="true" Width="7" IsFilled="true" Height="7" Shape="ChartShape.Diamond">
           </ChartMarker>
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>

@code {
   private bool isChecked1 = true;
   private bool isChecked2 = true;
   private bool isChecked3 = true;

   public bool Visibility1 = true;
   public bool Visibility2 = true;
   public bool Visibility3 = true;

   private void onChange1(Microsoft.AspNetCore.Components.ChangeEventArgs args)
   {
       Visibility1 = args.Value.ToString() == "True" ? true : false;
   }

   private void onChange2(Microsoft.AspNetCore.Components.ChangeEventArgs args)
   {
       Visibility2 = args.Value.ToString() == "True" ? true : false;
   }

   private void onChange3(Microsoft.AspNetCore.Components.ChangeEventArgs args)
   {
       Visibility3 = args.Value.ToString() == "True" ? true : false;
   }

   public class LineChartData
   {
       public double Period { get; set; }
       public double Can_Growth { get; set; }
       public double Viet_Growth { get; set; }
       public double Mal_Growth { get; set; }
   }

   public List<LineChartData> ChartData = new List<LineChartData>
   {
       new LineChartData { Period = 2020, Can_Growth = 11.0, Viet_Growth = 19.5, Mal_Growth = 7.1 },
       new LineChartData { Period = 2019, Can_Growth = 12.9, Viet_Growth = 17.5, Mal_Growth = 6.8 },
       new LineChartData { Period = 2018, Can_Growth = 13.4, Viet_Growth = 15.5, Mal_Growth = 4.1 },
       new LineChartData { Period = 2017, Can_Growth = 13.7, Viet_Growth = 10.3, Mal_Growth = 2.8 },
       new LineChartData { Period = 2016, Can_Growth = 12.7, Viet_Growth = 7.8, Mal_Growth = 2.8 },
       new LineChartData { Period = 2015, Can_Growth = 12.5, Viet_Growth = 5.7, Mal_Growth = 3.8 },
       new LineChartData { Period = 2014, Can_Growth = 12.7, Viet_Growth = 5.9, Mal_Growth = 4.3 },
       new LineChartData { Period = 2013, Can_Growth = 12.4, Viet_Growth = 5.6, Mal_Growth = 4.7 },
       new LineChartData { Period = 2012, Can_Growth = 13.5, Viet_Growth = 5.3, Mal_Growth = 5.6 }
   };
}

The following screenshot illustrates the output of the code snippet.

msedge_JbjBk28ZEh.png

Live Sample for Changing Series Visibility Using a Checkbox

Conclusion

I hope you enjoyed learning how to change series visibility using a checkbox in Blazor Charts.

You can refer to our Blazor Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied