How to enable or disable data labels dynamically in Blazor Charts?
This article explains how to dynamically enable or disable datalabels in Blazor Charts.
Enabling or Disabling Datalabels Dynamically:
Blazor Charts provides an option to enable or disable chart series datalabels dynamically using the OnDataLabelRender event and datalabel Visible property.
To disable all datalabels, you can set the Visible property to false on button click. To disable only specific datalabels, you can set the datalabel text to an empty string in the OnDataLabelRender event.
The code example below 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:
Live Sample for Hiding Datalabels
Conclusion:
We hope you enjoyed learning how to enable or disable datalabels dynamically in the Blazor Chart component.
You can refer to our Blazor Chart feature tour page to learn about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.
For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor Charts and other Blazor components.
If you have any questions 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!