Search code examples
vb.netmschart

How to add/remove custom labels from the axis


I am trying to remove custom labels from the axis. However, I am unable to do so. Currently, I am using the below mentioned code to add custom labels.

For i As Double = Chart1.ChartAreas("ChartArea1").AxisY2.Minimum To Chart1.ChartAreas("ChartArea1").AxisY2.Maximum Step Chart1.ChartAreas("ChartArea1").AxisY2.Interval
    If i = Chart1.ChartAreas("ChartArea1").AxisY2.Minimum Then
        If i = 0 Then
            Chart1.ChartAreas("ChartArea1").AxisY2.CustomLabels.Add(i, i + 0.01, CStr(i))
        Else
            Chart1.ChartAreas("ChartArea1").AxisY2.CustomLabels.Add(i, i + 0.01, CStr(i) + " (" + CStr(Math.Round(20 * Math.Log10(i / 100), 1)) + " in dB)")
        End If
    End If
    Chart1.ChartAreas("ChartArea1").AxisY2.CustomLabels.Add(i, i - 0.01, CStr(i) + " (" + CStr(Math.Round(20 * Math.Log10(i / 100), 1)) + " in dB)")
Next

This is the only code that I know how to add custom labels, as I need to add a certain value along with the axis values. But I can't seem to remove it when I try to plot another file, even after disabling the axis. It reappears from enabling the axis again. The new values would not even appear.

Kindly help me in removing the already added custom labels and add new ones. If there is any better way of adding and changing custom axis labels, please do let me know as well.


Solution

  • You can remove the labels you have added by calling the code below:

    Chart1.ChartAreas("ChartArea1").AxisY2.CustomLabels.Clear()