Search code examples
vb.netwinformsmschart

How does the AxisName enumeration work in Windows.Forms.Charts?


I'm using the windows forms charts in Visual Studio, and I want to write a function that, given a chart area, and an axis returns the maximum or minimum value (XValue or Yvalue, depending on the axis argument) from the points in that chart area. I want to use the AxisName enumeration for the second argument but, as per usual, the official documentation from msdn does not cover me. Does the enum name represent an index for the Axes() property of the ChartArea class or is it a direct link to an Axis object? Do i need to declare the enum in my class (that inherits DataVisualisation.Charts), or is it already known? pls help me

Public Function getAxisMinimum(ByVal area As AreaEnum, ByVal axe As AxisName) As Double
  Dim min As Double = Double.NaN
  For Each ser As Series In Series
     If ser.ChartArea = ChartAreas(area).Name And ser.Points.Count > 0 Then
        For Each p As DataPoint In ser.Points
           'compare X or Y values depending on the axe argument to set the min
        Next
     End If
  Next
  'If there are no points in any series in the area, it will return NaN
  Return min

End Function

AreaEnum is an integer enumeration that represents the index of the ChartArea() property that corresponds to each name.

I don't need a solution as to how to compare my points' values or how to return them, I just need an explanation on how to use the AxisName enumeration


Solution

  • Nevermind, I think I solved it. Visual Studio's auto-complete gave me the answer, because it recognized AxisName enumeration and corrected me in the select statement. I think this works:

    Public Function getAxisMinimum(ByVal area As AreaEnum, ByVal axe As AxisName) As Double
      Dim min As Double = Double.NaN
      For Each ser As Series In Series
         If ser.ChartArea = ChartAreas(area).Name AndAlso ser.Points.Count > 0 Then
            For Each p As DataPoint In ser.Points
               Select Case axe
                  Case AxisName.X
                     If Double.IsNaN(min) OrElse p.XValue < min Then
                        min = p.XValue
                     End If
                  Case AxisName.Y
                     For Each Yval As Double In p.YValues
                        If Double.IsNaN(min) OrElse Yval < min Then
                           min = Yval
                        End If
                     Next
                  Case Else
                     ' Not an acceptable AxisName
                     Return Double.NaN
               End Select
            Next
         End If
      Next
      'If there are no points in any series in the area, it will return NaN
      Return min
    

    End Function