"Axis Object - Auto interval does not have proper value" when setting Series Axis minimum and maximum C# winforms

Viewed 499

I've been debugging a piece of code but I'm getting the error "Axis Object - Auto interval does not have proper value" when I run this. After some research I see that setting the AxisY.Maximum and AxisY.Minimum incorrectly can lead to this error. The reason why I need to get a maximum and minimum is that my graph Y axis needs to range from a minimum through zero to a maximum value.

Here's the code:

private void plotDisplacementStatic(List<float> timeElapsedToPlot, List<float> displacement)
{
    if (InvokeRequired)
    {
        this.Invoke(new Action<List<float>, List<float>>(plotDisplacementStatic), new object[] { timeElapsedToPlot, displacement });
        return;
    }
    try
    {

        clearPoints("Displacement over Time");
        //this seems to be a problem when displacement is zero
        float overallMax = getOverallMax(displacement);
        float overallMin = getOverallMin(displacement);


        if (timeElapsedToPlot.Count > 0)
        {
            realtimePlot.ChartAreas["ChartArea2"].Visible = true;
            realtimePlot.ChartAreas["ChartArea3"].Visible = false;

            realtimePlot.ChartAreas["ChartArea2"].AxisX.Title = "Time[ms]";
            realtimePlot.ChartAreas["ChartArea2"].AxisY.Title = "Displacement[um]";

            //go from time 0 -> (count - 1)
            realtimePlot.ChartAreas["ChartArea2"].AxisX.Minimum = 0;
            realtimePlot.ChartAreas["ChartArea2"].AxisX.Maximum = timeElapsedToPlot[timeElapsedToPlot.Count - 1];
            
            //the code I think is throwing the error
            //I think setting AxisY.Maximum and AxisY.Minimum to be the same value is the problem
            realtimePlot.ChartAreas["ChartArea2"].AxisY.Maximum = overallMax;
            realtimePlot.ChartAreas["ChartArea2"].AxisY.Minimum = overallMin;

            /* Plot the Force vs Tme data */
            for (var i = 0; i < timeElapsedToPlot.Count; i++)
            {
                realtimePlot.Series["Displacement over Time"].Points.AddXY(timeElapsedToPlot[i], displacement[i]);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}
private float getOverallMax(List<float> values)
{
    
    float maxValue = float.MinValue;

    for (int i = 0; i < values.Count; i++)
    {
        float value = values[i];
        if (value > maxValue)
        {
            maxValue = value;
        }
    }
    return maxValue;
}

private float getOverallMin(List<float> values)
{

    float minValue = float.MaxValue;

    for (int i = 0; i < values.Count; i++)
    {
        float value = values[i];
        if (value < minValue)
        {
            minValue = value;
        }
    }
    return minValue;
}

I thought of saying:

if (overallMax == overallMin) {
    overallMax += 1;
    overallMin -= 1;

}

Before setting AxisY.Minimum and AxisY.Maximum but this seems rather hacky. Can somebody please help me with a better solution?

0 Answers
Related