Skip to content

Commit 3598c8c

Browse files
author
Devakumar D
committed
Added example sample.
1 parent 6a66589 commit 3598c8c

23 files changed

+570
-1
lines changed

README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,94 @@
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)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!-- Copyright (c) Microsoft Corporation. All rights reserved. -->
2+
<!-- Licensed under the MIT License. See LICENSE in the project root for license information. -->
3+
4+
<Application
5+
x:Class="CustomDataLabel.App"
6+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
7+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
8+
xmlns:local="using:CustomDataLabel">
9+
<Application.Resources>
10+
<ResourceDictionary>
11+
<ResourceDictionary.MergedDictionaries>
12+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
13+
<!-- Other merged dictionaries here -->
14+
</ResourceDictionary.MergedDictionaries>
15+
<!-- Other app resources here -->
16+
</ResourceDictionary>
17+
</Application.Resources>
18+
</Application>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using Microsoft.UI.Xaml;
5+
using Microsoft.UI.Xaml.Controls;
6+
using Microsoft.UI.Xaml.Controls.Primitives;
7+
using Microsoft.UI.Xaml.Data;
8+
using Microsoft.UI.Xaml.Input;
9+
using Microsoft.UI.Xaml.Media;
10+
using Microsoft.UI.Xaml.Navigation;
11+
using Microsoft.UI.Xaml.Shapes;
12+
using System;
13+
using System.Collections.Generic;
14+
using System.IO;
15+
using System.Linq;
16+
using System.Runtime.InteropServices.WindowsRuntime;
17+
using Windows.ApplicationModel;
18+
using Windows.ApplicationModel.Activation;
19+
using Windows.Foundation;
20+
using Windows.Foundation.Collections;
21+
22+
// To learn more about WinUI, the WinUI project structure,
23+
// and more about our project templates, see: http://aka.ms/winui-project-info.
24+
25+
namespace CustomDataLabel
26+
{
27+
/// <summary>
28+
/// Provides application-specific behavior to supplement the default Application class.
29+
/// </summary>
30+
public partial class App : Application
31+
{
32+
/// <summary>
33+
/// Initializes the singleton application object. This is the first line of authored code
34+
/// executed, and as such is the logical equivalent of main() or WinMain().
35+
/// </summary>
36+
public App()
37+
{
38+
this.InitializeComponent();
39+
}
40+
41+
/// <summary>
42+
/// Invoked when the application is launched normally by the end user. Other entry points
43+
/// will be used such as when the application is launched to open a specific file.
44+
/// </summary>
45+
/// <param name="args">Details about the launch request and process.</param>
46+
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
47+
{
48+
m_window = new MainWindow();
49+
m_window.Activate();
50+
}
51+
52+
private Window m_window;
53+
}
54+
}
432 Bytes
Loading
5.25 KB
Loading
1.71 KB
Loading
637 Bytes
Loading
283 Bytes
Loading
456 Bytes
Loading
2.05 KB
Loading

0 commit comments

Comments
 (0)