|
1 | | -# How-to-add-a-custom-data-marker-in-WinUI-Chart-SfCartesianChart- |
| 1 | +# How to add a custom data marker in WinUI Chart (SfCartesianChart)? |
| 2 | + |
| 3 | +This example illustrates how to add a custom view as chart Data Label and customize the appearance based on its Y value in the [WinUI charts](https://www.syncfusion.com/winui-controls/charts). |
| 4 | + |
| 5 | +**Step 1:** By using the [ContentTemplate](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.Charts.ChartDataLabelSettings.html#Syncfusion_UI_Xaml_Charts_ChartDataLabelSettings_ContentTemplate) property of [CartesianDataLabelSettings](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.Charts.CartesianDataLabelSettings.html), we can add the data label with custom view. The following code example explains how to add a circle ring shape as a data label using the Border control. |
| 6 | + |
| 7 | +``` |
| 8 | +… |
| 9 | +<chart:SfCartesianChart.Resources> |
| 10 | + <DataTemplate x:Key="dataLabelTemplate"> |
| 11 | + <Border Height="10" Width="10" CornerRadius="20" |
| 12 | + BorderBrush="Red " BorderThickness="2" |
| 13 | + Background="WhiteSmoke" > |
| 14 | + </Border> |
| 15 | + </DataTemplate> |
| 16 | +</chart:SfCartesianChart.Resources> |
| 17 | +
|
| 18 | +<chart:SplineAreaSeries ItemsSource="{Binding Data}" |
| 19 | + XBindingPath="XValue" YBindingPath="YValue" |
| 20 | + ShowDataLabels="True"> |
| 21 | + <chart:SplineAreaSeries.DataLabelSettings> |
| 22 | + <chart:CartesianDataLabelSettings ContentTemplate="{StaticResource dataLabelTemplate}"/> |
| 23 | + </chart:SplineAreaSeries.DataLabelSettings> |
| 24 | +</chart:SplineAreaSeries> |
| 25 | +``` |
| 26 | + |
| 27 | +**Step 2:** Using IValueConverter, we can customize the appearance of the BorderBrush color based on the "Y" data point value for the custom data labels as shown in the following code example. |
| 28 | + |
| 29 | +``` |
| 30 | +… |
| 31 | +<chart:SfCartesianChart.Resources> |
| 32 | + <local:BorderColorConverter x:Key="borderColorConverter"/> |
| 33 | + <DataTemplate x:Key="dataLabelTemplate"> |
| 34 | + <Border Height="10" Width="10" CornerRadius="20" |
| 35 | + BorderBrush="{Binding Converter={StaticResource borderColorConverter}}" |
| 36 | + Background="WhiteSmoke" BorderThickness="2"> |
| 37 | + </Border> |
| 38 | + </DataTemplate> |
| 39 | + … |
| 40 | +</chart:SfCartesianChart.Resources> |
| 41 | +… |
| 42 | +<chart:SplineAreaSeries ItemsSource="{Binding Data}" |
| 43 | + XBindingPath="XValue" YBindingPath="YValue" |
| 44 | + ShowDataLabels="True" |
| 45 | + PaletteBrushes="{StaticResource customBrushes}"> |
| 46 | + <chart:SplineAreaSeries.DataLabelSettings> |
| 47 | + <chart:CartesianDataLabelSettings ContentTemplate="{StaticResource dataLabelTemplate}"/> |
| 48 | + </chart:SplineAreaSeries.DataLabelSettings> |
| 49 | +</chart:SplineAreaSeries> |
| 50 | +``` |
| 51 | + |
| 52 | +**BorderColorConverter.cs** |
| 53 | +``` |
| 54 | +public class BorderColorConverter : IValueConverter |
| 55 | +{ |
| 56 | + static double YValue = 0; |
| 57 | + public object Convert(object value, Type targetType, object parameter, string language) |
| 58 | + { |
| 59 | + if (value != null) |
| 60 | + { |
| 61 | + var yData = System.Convert.ToDouble(value); |
| 62 | + if (yData >= YValue) |
| 63 | + { |
| 64 | + //if y value increased |
| 65 | + YValue = yData; |
| 66 | + return new SolidColorBrush(Colors.Green); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + //if y value decreased |
| 71 | + YValue = yData; |
| 72 | + return new SolidColorBrush(Colors.Red); |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + return new SolidColorBrush(Colors.Transparent); |
| 77 | + } |
| 78 | +
|
| 79 | + public object ConvertBack(object value, Type targetType, object parameter, string language) |
| 80 | + { |
| 81 | + return value; |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Output |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +### See also |
| 91 | + |
| 92 | +[How to customize the data label in WinUI Chart (SfCartesianChart)?](https://help.syncfusion.com/winui/cartesian-charts/datalabels#template) |
| 93 | + |
| 94 | +[How to customize the appearance in WinUI Chart (SfCartesianChart)?](https://help.syncfusion.com/winui/cartesian-charts/appearance) |
0 commit comments