How to customize the default shape in WPF Chart?
You can define custom templates for series in WPF Chart (SfChart) using the CustomTemplate property. The DataTemplate defined in this property renders for each data point, where the corresponding segment is provided as the DataContext. For example, when defining a template for LineSeries, the LineSegment is used as the data context for that DataTemplate.
This CustomTemplate property supports only limited series types.
XAML
<Window.Resources> <local:LabelConverter x:Key="labelConverter"/> <DataTemplate x:Key="columnSegment"> <Canvas> <local:CustomControl Width="{Binding Width}" Height="{Binding Height}" Canvas.Left="{Binding RectX}" Canvas.Top="{Binding RectY}"/> </Canvas> </DataTemplate> </Window.Resources>
.
. <!--Assign the CustomTemplate of the series with the customized DataTemplate--> <syncfusion:ColumnSeries CustomTemplate="{StaticResource columnSegment}" ItemsSource="{Binding Computers}" XBindingPath="Computer" YBindingPath="Year2014" syncfusion:ChartSeriesBase.Spacing="0.5"> <!--Assign the LabelTemplate of the adornment with the customized DataTemplate--> <syncfusion:ColumnSeries.AdornmentsInfo> <syncfusion:ChartAdornmentInfo ShowLabel="True" SegmentLabelContent="YValue" AdornmentsPosition="Top"/> </syncfusion:ColumnSeries.AdornmentsInfo> </syncfusion:ColumnSeries>
C#
public class CustomControl : ContentControl { public PointCollection Data { get; set; } public CustomControl() { Data = new PointCollection(); this.Loaded += CustomControl_Loaded; } public void DrawPolygon() { Polygon columnSegment = new Polygon(); columnSegment.Fill = new SolidColorBrush(Colors.SkyBlue); double center = Width / 2; Data.Add(new Point(0, Height)); Data.Add(new Point(Width, Height)); Data.Add(new Point(Width, 35)); Data.Add(new Point(center, 0)); Data.Add(new Point(0, 35)); columnSegment.Points = Data; Content = columnSegment; } void CustomControl_Loaded(object sender, RoutedEventArgs e) { DrawPolygon(); } }
The following demo image illustrates how to customize the default shape in a WPF Chart using the CustomTemplate property.
Conclusion
I hope you enjoyed learning how to customize the default shape in WPF Chart.
You can refer to our WPF Chart feature tour page know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart Examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!