How to position the secondary axis to avoid intersection in WinForms Chart?
The location of the secondary axis in a WinForms Chart can be set manually using the Location property of the ChartAxis class. Before setting the location, ensure that the LocationType property is set to Set.
The following code snippets illustrate this:
C#
this.chartControl1.PrimaryXAxis.ValueType = ChartValueType.Category;
this.chartControl1.PrimaryYAxis.ValueType = ChartValueType.Double;
//Secondary Y axis
ChartAxis secYAxis = new ChartAxis();
secYAxis.Orientation = ChartOrientation.Vertical;
secYAxis.Range = new MinMaxInfo(0, 100, 20);
secYAxis.LocationType = ChartAxisLocationType.Set;
secYAxis.Location = new PointF(this.chartControl1.PrimaryXAxis.Location.X + 1295, this.chartControl1.PrimaryXAxis.Location.Y + 700);
this.chartControl1.Axes.Add(secYAxis);
//Series 1 represents primary Y Axis
ChartSeries chartSeries1 = new ChartSeries("Series 1");
chartSeries1.CategoryModel = new CategoryAxisDataBindModel(dataSource)
{
CategoryName = "Year",
YNames = new string[] { "Sales" }
};
this.chartControl1.Series.Add(chartSeries1);
//Series 2 represents secondary Y Axis
ChartSeries chartSeries2 = new ChartSeries("Series 2");
chartSeries2.CategoryModel = new CategoryAxisDataBindModel(dataSource)
{
CategoryName = "Year",
YNames = new string[] { "YValue" }
};
chartSeries2.YAxis = secYAxis;
this.chartControl1.Series.Add(chartSeries2);
VB
columnChart.PrimaryYAxis.ValueType = ChartValueType.Double
columnChart.PrimaryXAxis.ValueType = ChartValueType.Category
'Secondary Axis
Dim secYAxis = New ChartAxis()
secYAxis.Orientation = ChartOrientation.Vertical
secYAxis.Range = New MinMaxInfo(0, 100, 20)
secYAxis.LocationType = ChartAxisLocationType.Set
secYAxis.Location = New PointF(columnChart.PrimaryXAxis.Location.X + 1295, columnChart.PrimaryXAxis.Location.Y + 700)
columnChart.Axes.Add(secYAxis)
'Series 1 represents the primary Y axis
Dim ChartSeries1 = New ChartSeries("Series 1")
Dim categoryBindModel1 = New CategoryAxisDataBindModel(viewModel.PlantDetails)
categoryBindModel1.CategoryName = "Year"
categoryBindModel1.YNames = New String() {"Sales"}
ChartSeries1.CategoryModel = categoryBindModel1
columnChart.Series.Add(ChartSeries1)
'Series 2 represents the secondary Y axis
Dim ChartSeries2 = New ChartSeries("Series 2")
Dim categoryBindModel2 = New CategoryAxisDataBindModel(viewModel.PlantDetails)
categoryBindModel2.CategoryName = "Year"
categoryBindModel2.YNames = New String() {"YValue"}
ChartSeries2.CategoryModel = categoryBindModel2
ChartSeries2.YAxis = secYAxis
columnChart.Series.Add(ChartSeries2)
Conclusion
I hope you enjoyed learning about how to position the secondary axis to avoid intersection 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!