Skip to content

Commit cc1573e

Browse files
docs(chart): example for hiding labels conditionally
1 parent b3e4f99 commit cc1573e

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
22.3 KB
Loading

components/chart/labels-template-and-format.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,74 @@ In a **legend item label template**, you can use the following fields:
188188
* `series` - the data series
189189
* `value` - the point value (only for donut and pie charts)
190190
* `percentage` - the point value represented as a percentage value. Available only for donut, pie and 100% stacked charts
191+
192+
193+
### Hide Label Conditionally
194+
195+
In some cases, you want the series to have labels, but certain data points must not have a label. For example, in [stacked series]({%slug components/chart/stack%}) where a certain value is `0`.
196+
197+
To do that, you need to:
198+
199+
* add conditional logic in the template that renders the desired content when your condition is met, and returns nothing when it is not.
200+
* ensure the labels background is transparent so there are no leftover spots on the chart.
201+
202+
>caption Hide labels with zero values
203+
204+
````CSHTML
205+
@using Telerik.Blazor
206+
@using Telerik.Blazor.Components.Chart
207+
208+
<TelerikChart>
209+
<TelerikChartSeriesItems>
210+
<TelerikChartSeries Type="ChartSeriesType.Column" Data="@chartData" Name="Product 1 Sales"
211+
Field="@nameof(MyChartDataModel.Value1)" CategoryField="@nameof(MyChartDataModel.TheCategory)">
212+
<TelerikChartSeriesLabels Visible="true" Template="@MySeriesTemplate" Background="transparent"></TelerikChartSeriesLabels>
213+
<TelerikChartSeriesStack Enabled="true"></TelerikChartSeriesStack>
214+
</TelerikChartSeries>
215+
<TelerikChartSeries Type="ChartSeriesType.Column" Data="@chartData" Name="Product 2 Sales"
216+
Field="@nameof(MyChartDataModel.Value2)" CategoryField="@nameof(MyChartDataModel.TheCategory)">
217+
<TelerikChartSeriesLabels Visible="true" Template="@MySeriesTemplate" Background="transparent"></TelerikChartSeriesLabels>
218+
</TelerikChartSeries>
219+
</TelerikChartSeriesItems>
220+
</TelerikChart>
221+
222+
@code {
223+
public string MySeriesTemplate = "# if (value != 0) { # #=value# #}#";
224+
225+
public class MyChartDataModel
226+
{
227+
public double Value1 { get; set; }
228+
public double Value2 { get; set; }
229+
public string TheCategory { get; set; }
230+
}
231+
232+
public List<MyChartDataModel> chartData = new List<MyChartDataModel>
233+
{
234+
new MyChartDataModel
235+
{
236+
Value1 = 3,
237+
Value2 = 2,
238+
TheCategory = "Q1"
239+
},
240+
new MyChartDataModel
241+
{
242+
Value1 = 1,
243+
Value2 = 0,
244+
TheCategory = "Q2"
245+
},
246+
new MyChartDataModel
247+
{
248+
Value1 = 0,
249+
Value2 = 0,
250+
TheCategory = "Q3"
251+
}
252+
};
253+
}
254+
````
255+
256+
>caption The result from the code above:
257+
258+
![](images/hide-label-conditionally.png)
191259

192260

193261
## See Also

0 commit comments

Comments
 (0)