|
1 | 1 | # How-to-collapse-the-visibility-of-specific-data-label-in-.NET-MAUI-Cartesian-chart |
2 | 2 | This article in the Syncfusion Knowledge Base explains how to collapse the visibility of specific data label in .NET MAUI Cartesian chart |
| 3 | + |
| 4 | +Collapsing the visibility of specific data labels in [Cartesian charts](https://www.syncfusion.com/maui-controls/maui-cartesian-charts) can be a useful way to improve your visualisation and focus on the most important data points. |
| 5 | +This article will explain the step to hide or collapse specific data labels in a Cartesian chart. |
| 6 | + |
| 7 | +**Step 1:** Define a data label template for the series. |
| 8 | + |
| 9 | +**[XAML]** |
| 10 | +``` |
| 11 | +<DataTemplate x:Key="labelTemplate"> |
| 12 | + <HorizontalStackLayout HorizontalOptions="Center"> |
| 13 | + <Label Text="{Binding Item.Value}" VerticalOptions="Center"/> |
| 14 | + <Label Text="%" VerticalOptions="Center"/> |
| 15 | + <Image Source="greenarrow.png" HeightRequest="15" WidthRequest="15" /> |
| 16 | + </HorizontalStackLayout> |
| 17 | +</DataTemplate> |
| 18 | +``` |
| 19 | +**Step 2:** Create a value to visibility converter to control the visibility of the data label based on the Y-value. For example, in the following converter, we have collapsed the visibility of the data label for values less than 50. |
| 20 | + |
| 21 | +**[C#]** |
| 22 | +``` |
| 23 | + public class VisibilityConverter : IValueConverter |
| 24 | + { |
| 25 | + public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 26 | + { |
| 27 | + if (!(value is double)) |
| 28 | + return null; |
| 29 | +
|
| 30 | + var labelValue = (double)value; |
| 31 | + if (labelValue < 50) |
| 32 | + return false; |
| 33 | + return true; |
| 34 | + } |
| 35 | +
|
| 36 | + public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 37 | + { |
| 38 | + return value; |
| 39 | + } |
| 40 | + } |
| 41 | +``` |
| 42 | +**Step 3:** To control the visibility, bind the converter to the DataTemplate layout IsVisible property |
| 43 | + |
| 44 | +**[XAML]** |
| 45 | +``` |
| 46 | + <chart:SfCartesianChart.Resources> |
| 47 | + <model:VisibilityConverter x:Key="visibilityConverter"/> |
| 48 | + <DataTemplate x:Key="labelTemplate"> |
| 49 | + <VerticalStackLayout IsVisible="{Binding Item.Value, Converter={StaticResource visibilityConverter}}" Spacing="5" WidthRequest="100"> |
| 50 | + <HorizontalStackLayout HorizontalOptions="Center" IsVisible="{Binding Item.Value}"> |
| 51 | + <Label Text="{Binding Item.Value}" VerticalOptions="Center"/> |
| 52 | + <Label Text="%" VerticalOptions="Center"/> |
| 53 | + <Image Source="greenarrow.png" HeightRequest="15" WidthRequest="15" /> |
| 54 | + </HorizontalStackLayout> |
| 55 | + </VerticalStackLayout> |
| 56 | + </DataTemplate> |
| 57 | +
|
| 58 | + </chart:SfCartesianChart.Resources> |
| 59 | +``` |
| 60 | +**Step 4:** Set the defined DataTemplate to the [LabelTemplate](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ChartSeries.html#Syncfusion_Maui_Charts_ChartSeries_LabelTemplate) property of [ColumnSeries](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ColumnSeries.html). |
| 61 | + |
| 62 | +**[XAML]** |
| 63 | +``` |
| 64 | +<chart:SfCartesianChart x:Name="Chart"> |
| 65 | +. . . |
| 66 | + <chart:SfCartesianChart.Series> |
| 67 | + <chart:ColumnSeries ItemsSource="{Binding ElectricityProductionData}" |
| 68 | + LabelTemplate="{StaticResource labelTemplate}" |
| 69 | + ShowDataLabels="True" |
| 70 | + XBindingPath="Year" |
| 71 | + YBindingPath="Value"> |
| 72 | + <chart:ColumnSeries.DataLabelSettings> |
| 73 | + <chart:CartesianDataLabelSettings LabelPlacement="Outer"/> |
| 74 | + </chart:ColumnSeries.DataLabelSettings> |
| 75 | + </chart:ColumnSeries> |
| 76 | + |
| 77 | + </chart:SfCartesianChart.Series> |
| 78 | +</chart:SfCartesianChart> |
| 79 | +``` |
| 80 | +**Output** |
| 81 | +  |
0 commit comments