Articles in this section
Category / Section

How to enable or disable data labels dynamically in Blazor Charts?

3 mins read

This article explains how to dynamically enable or disable the datalabels.

Enabling or disabling datalabels dynamically

Blazor Charts provides an option to enable or disable the chart series datalabels dynamically using OnDataLabelRender event and datalabel Visible property.

To disable all datalabels, you can specify Visible as false in button click and to disable only specific datalabels, you can specify datalabel text as empty string in OnDataLabelRender event.

The below code example demonstrates how to show or hide datalabels dynamically

Index.razor

@using Syncfusion.Blazor.Charts
<button class="btn btn-primary shadow-none" @onclick="DisableDataLabel">Disable DataLabels</button>
<SfChart Title="Hide all datalabels" Height="350px">
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
   <ChartPrimaryYAxis Title="Sales in Dollar"></ChartPrimaryYAxis>
   <ChartSeriesCollection>
       <ChartSeries DataSource="@Sales" XName="Month" YName="SalesValue" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
           <ChartMarker>
               <ChartDataLabel Position="Syncfusion.Blazor.Charts.LabelPosition.Top" Visible=@isVisible></ChartDataLabel>
           </ChartMarker>
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>
<SfChart Title="Condition based datalabel hide" Height="350px">
   <ChartEvents OnDataLabelRender="DatalabelRenderEvent"></ChartEvents>
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
   <ChartPrimaryYAxis Title="Sales in Dollar"></ChartPrimaryYAxis>
   <ChartSeriesCollection>
       <ChartSeries DataSource="@Sales" XName="Month" YName="SalesValue" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
           <ChartMarker>
               <ChartDataLabel Position="Syncfusion.Blazor.Charts.LabelPosition.Top" Visible="true"></ChartDataLabel>
           </ChartMarker>
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>
@code {
   bool isVisible = true;
   void DisableDataLabel()
   {
       isVisible = false;
   }
   public class SalesInfo
   {
       public string Month { get; set; }
       public double SalesValue { get; set; }
   }
   void DatalabelRenderEvent(Syncfusion.Blazor.Charts.TextRenderEventArgs eventArgs)
   {
       if (eventArgs.Point.Index % 2 == 0)
       {
           eventArgs.Text = "";
       }
   }
   public List<SalesInfo> Sales = new List<SalesInfo>
   {
       new SalesInfo { Month = "Jan", SalesValue = 35 },
       new SalesInfo { Month = "Feb", SalesValue = 28 },
       new SalesInfo { Month = "Mar", SalesValue = 34 },
       new SalesInfo { Month = "Apr", SalesValue = 32 },
       new SalesInfo { Month = "May", SalesValue = 40 },
       new SalesInfo { Month = "Jun", SalesValue = 32 },
       new SalesInfo { Month = "Jul", SalesValue = 35 }
   };
}

The following screenshot illustrates the output of the code snippet.

Output

image.png

Live Sample for Hiding Datalabels

Conclusion

I hope you enjoyed learning how to enable or disable datalabels dynamically in Blazor Chart Component.

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)
Please  to leave a comment
Access denied
Access denied