Home > Back-end >  Customize secondary y axes value
Customize secondary y axes value

Time:05-31

I'm quite new with graph in vb.net. I think my problem is very simple but I seem can't find a way to figure it out. I have a graph of y against x. I managed to include some interval between the primary axes. I would to know if I want to add the secondary y axes, it is possible for me to customize the axes so it is always fixed with my custom value ? This secondary y axes should not be dependent on the both primary x and y axes.

Refer to pic below

enter image description here

Using this code:

'Add some dummy data.
Dim series1 = Chart1.Series(0)
series1.ChartType = SeriesChartType.Line
series1.Points.AddXY(1, 2)
series1.Points.AddXY(2, 3)
series1.Points.AddXY(3, 4)
series1.Points.AddXY(4, 1)
series1.Points.AddXY(5, 2)

'Enable and modify the second Y-axis.
Dim axY2 = Chart1.ChartAreas(0).AxisY2
axY2.Enabled = AxisEnabled.True
axY2.Minimum = -3
axY2.Maximum = 3
axY2.Interval = 1
axY2.LabelStyle.Format = " 0.0;-0.0;0.0"
axY2.MajorGrid.LineColor = Color.Orange

Am I correct in assuming this is not quite what you are looking for; you want the gridlines of the second Y-axis to coincide with those of the primary Y-axis?

CodePudding user response:

Thank you for the reply. I've managed to figure out how to do it. It has to do with the custom label. I created another secondary y axes on the series and set the minimum to 0 and max to 20 respectively. Then I set my interval 2.5. From here I know I already have 8 tick of interval from my graph(2.5 each). Finally we can add everything together to get the answers.

 Dim ax As New Axis
        ax = Chart1.ChartAreas("ChartArea1").AxisY2
        ax.Enabled = AxisEnabled.True
        ax.Interval = 2.5
        ax.Maximum = 20
        ax.Minimum = 0
        ax.CustomLabels.Add(0, 5, "- 3.0")
        ax.CustomLabels.Add(0, 10, "- 2.0")
        ax.CustomLabels.Add(0, 15, "- 1.0")
        ax.CustomLabels.Add(0, 20, "0.0")
        ax.CustomLabels.Add(0, 25, "  1.0")
        ax.CustomLabels.Add(0, 30, "  2.0")
        ax.CustomLabels.Add(0, 35, "  3.0")
        ax.Title = "SD"

Final graph

This might not be the best solution but it does works for me. Thanks again sir!

  • Related