Scaling the Y-axis of the chart depending on the values ​​in the X-values ​​section for several series

I have an application like this: Chart with scaling for the X-axis With the text fields below the chart, the user can set the minimum and maximum value of the X axis of the chart. This is the code for it:

private void textBoxXaxisMin_TextChanged(object sender, EventArgs e)
{
    double x;
    //checks if the input is a double and smaller than the max value
    //if (Double.TryParse(this.textBoxXaxisMin.Text, out x) && x < chart1.ChartAreas[0].AxisX.Maximum)    
    if (Double.TryParse(this.textBoxXaxisMin.Text, out x))
    {
        this.textBoxXaxisMin.BackColor = Color.White;
        chart1.ChartAreas[0].AxisX.Minimum = Convert.ToDouble(this.textBoxXaxisMin.Text);
        //changeYScalaMin(chartCharacteristicCurvesThermoelemts, Convert.ToDouble(this.textBoxCharacteristicCurvesThermoelementXmin.Text), Convert.ToDouble(this.textBoxCharacteristicCurvesThermoelementXmax.Text));     

        //method to scale y axis

    }
    else
        //if the textbox is not highlighted 
        this.textBoxXaxisMin.BackColor = Color.Orange;
    //calls the Max Function to update the chart if the Max-value is now valid

    double y;
    //checks if the input is a double and greater than the min value
    if (Double.TryParse(this.textBoxXaxisMax.Text, out y) && y > chart1.ChartAreas[0].AxisX.Minimum)
    {
        this.textBoxXaxisMax.BackColor = Color.White;
        chart1.ChartAreas[0].AxisX.Maximum = Convert.ToDouble(this.textBoxXaxisMax.Text);


        //method to scale y axis

    }
    else
        //if the textbox is not  highlighted 
        this.textBoxXaxisMax.BackColor = Color.Orange;
}

Now I would like the Y axis to be scaled automatically. Y-min should be calculated as the minimum value of all series in the section (X-min and X-max), and Y-max should be calculated as the maximum of all series in the selected section. My problem is implementation.

In this example, Y-min should be changed to about 50.

I posted here an example of an example of holes in GitHup .

+4
3

Y- [0] X 0 1:

double max = Double.MinValue; 
double min = Double.MaxValue; 

double leftLimit  = chart1.ChartAreas[0].AxisX.Minimum;
double rightLimit = chart1.ChartAreas[0].AxisX.Maximum;

for (int s = 0; s <= 1; s++)
{
    foreach (DataPoint dp in chart1.Series[s].Points)
    {
        if (dp.XValue >= leftLimit && dp.XValue <= rightLimit)
        {
            min = Math.Min(min, dp.YValues[0]);
            max = Math.Max(max, dp.YValues[0]);
        }
    }
}

chart1.ChartAreas[0].AxisY.Maximum = max;
chart1.ChartAreas[0].AxisY.Minimum = min;

: , Min & Max . :

chart1.ChartAreas[0].AxisY.Minimum = Double.NaN;
chart1.ChartAreas[0].AxisY.Maximum = Double.NaN;
chart1.ChartAreas[0].AxisX.Minimum = Double.NaN;
chart1.ChartAreas[0].AxisX.Maximum = Double.NaN;
+6

0, IsStartesFromZero:

chart.ChartAreas[0].AxisY.IsStartedFromZero = false;
+4

Code I used for my project: (based on @TaW answer)

private void changeYScala(object chart)
{
    double max = Double.MinValue;
    double min = Double.MaxValue;

    Chart tmpChart = (Chart)chart;

    double leftLimit = tmpChart.ChartAreas[0].AxisX.Minimum;
    double rightLimit = tmpChart.ChartAreas[0].AxisX.Maximum;

    for (int s = 0; s < tmpChart.Series.Count(); s++)
    {
        foreach (DataPoint dp in tmpChart.Series[s].Points)
        {
            if (dp.XValue >= leftLimit && dp.XValue <= rightLimit)
            {
                min = Math.Min(min, dp.YValues[0]);
                max = Math.Max(max, dp.YValues[0]);
            }
        }
    }
    //tmpChart.ChartAreas[0].AxisY.Maximum = max;
    tmpChart.ChartAreas[0].AxisY.Maximum = (Math.Ceiling((max / 10)) * 10);
    tmpChart.ChartAreas[0].AxisY.Minimum = (Math.Floor((min / 10)) * 10);
    //tmpChart.ChartAreas[0].AxisY.Minimum = min;
}

This method is called in the interrogation code above:

changeYScala(chart1);

do not forget to include:

using System.Windows.Forms.DataVisualization.Charting;
+2
source

Source: https://habr.com/ru/post/1542839/


All Articles