How to set custom points in WinForms Chart?
You can define custom points by plotting them on the chart area, even if they do not belong to any series. These custom points are stored in the ChartControl's CustomPoints collection and can be used for annotating interesting data on WinForms Charts. The ChartCustomPoint class provides properties to set text, background, borders, or symbols at a specific point on the chart area.
Custom points can be categorized into the following four types:
- PointFollow - This type of custom point will track the regular points of any series to which it is assigned.
- ChartCoordinates - This allows you to render a point type at any specific location in the chart.
- Percent - The coordinates are specified as a percentage of the chart area.
- Pixel - The coordinates are specified in pixels relative to the chart area.
C#
//Assign the X and Y axes
this.chartControl1.PrimaryXAxis.ValueType = ChartValueType.Category;
this.chartControl1.PrimaryYAxis.ValueType = ChartValueType.Double;
//Configure the chart series
CategoryAxisDataBindModel dataSeriesModel = new CategoryAxisDataBindModel(dataSource);
dataSeriesModel.CategoryName = "Year";
dataSeriesModel.YNames = new string[] { "Sales" };
ChartSeries chartSeries = new ChartSeries("Sales");
chartSeries.Type = ChartSeriesType.Column;
chartSeries.CategoryModel = dataSeriesModel;
// Add all custom points to the chart
ChartCustomPoint cp1 = new ChartCustomPoint();
cp1.PointIndex = 0;
cp1.SeriesIndex = 0;
cp1.CustomType = ChartCustomPointType.PointFollow;
cp1.Text = "Start (1999)";
cp1.Font.FontStyle = FontStyle.Bold;
cp1.Alignment = ChartTextOrientation.UpRight;
cp1.Symbol.Shape = ChartSymbolShape.Diamond;
cp1.Symbol.Size = new Size(18, 18);
cp1.Symbol.Color = Color.Green;
ChartCustomPoint cp2 = new ChartCustomPoint();
cp2.PointIndex = 4;
cp2.SeriesIndex = 0;
cp2.CustomType = ChartCustomPointType.PointFollow;
cp2.Text = "Milestone (2003)";
cp2.Font.FontStyle = FontStyle.Bold;
cp2.Alignment = ChartTextOrientation.UpLeft;
cp2.Symbol.Shape = ChartSymbolShape.Circle;
cp2.Symbol.Size = new Size(18, 18);
cp2.Symbol.Color = Color.Blue;
ChartCustomPoint cp3 = new ChartCustomPoint();
cp3.PointIndex = 8;
cp3.SeriesIndex = 0;
cp3.CustomType = ChartCustomPointType.PointFollow;
cp3.Text = "Peak (2007)";
cp3.Font.FontStyle = FontStyle.Bold;
cp3.Alignment = ChartTextOrientation.UpLeft;
cp3.Symbol.Shape = ChartSymbolShape.Triangle;
cp3.Symbol.Size = new Size(18, 18);
cp3.Symbol.Color = Color.Red;
this.chartControl1.CustomPoints.Add(cp1);
this.chartControl1.CustomPoints.Add(cp2);
this.chartControl1.CustomPoints.Add(cp3);
VB
'Assign the X and Y axes
columnChart.PrimaryXAxis.ValueType = ChartValueType.Category
columnChart.PrimaryYAxis.ValueType = ChartValueType.Double
'Configure the chart series
Dim dataSeriesModel = New CategoryAxisDataBindModel(viewModel.PlantDetails)
dataSeriesModel.CategoryName = "Year"
dataSeriesModel.YNames = New String() {"Sales"}
ChartSeries1.Type = ChartSeriesType.Column
ChartSeries1.CategoryModel = dataSeriesModel
columnChart.Series.Add(ChartSeries1)
'Add the custom points to the series
Dim cp1 = New ChartCustomPoint()
cp1.PointIndex = 0
cp1.SeriesIndex = 0
cp1.CustomType = ChartCustomPointType.PointFollow
cp1.Text = "Start (1999)"
cp1.Font.FontStyle = FontStyle.Bold
cp1.Alignment = ChartTextOrientation.UpRight
cp1.Symbol.Shape = ChartSymbolShape.Diamond
cp1.Symbol.Size = New Size(18, 18)
cp1.Symbol.Color = Color.Green
Dim cp2 = New ChartCustomPoint()
cp2.PointIndex = 4
cp2.SeriesIndex = 0
cp2.CustomType = ChartCustomPointType.PointFollow
cp2.Text = "Milestone (2003)"
cp2.Font.FontStyle = FontStyle.Bold
cp2.Alignment = ChartTextOrientation.UpLeft
cp2.Symbol.Shape = ChartSymbolShape.Circle
cp2.Symbol.Size = New Size(18, 18)
cp2.Symbol.Color = Color.Blue
Dim cp3 = New ChartCustomPoint()
cp3.PointIndex = 8
cp3.SeriesIndex = 0
cp3.CustomType = ChartCustomPointType.PointFollow
cp3.Text = "Peak (2007)"
cp3.Font.FontStyle = FontStyle.Bold
cp3.Alignment = ChartTextOrientation.UpLeft
cp3.Symbol.Shape = ChartSymbolShape.Triangle
cp3.Symbol.Size = New Size(18, 18)
cp3.Symbol.Color = Color.Red
columnChart.CustomPoints.Add(cp1)
columnChart.CustomPoints.Add(cp2)
columnChart.CustomPoints.Add(cp3)
Output:
Conclusion
I hope you enjoyed learning about how to set custom points in WinForms Chart.
You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms 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!