简体   繁体   中英

Scale Y-Axis of a Chart depending on the Values within a section of X-Values for several Series

I have got an aplication like this: 具有X轴缩放的图表 With the textboxes below the chart the user can set the min and max 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 to have the Y-axis scaled automatically. Y-min should be calulated as the min value of all series in the section of (X-min and X-max) and the Y-max as the maximum of all series in the selected section. My problem is the implementation.

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

I hosted the hole exampleproject here at GitHup .

This will scale the Y-Axis to the Minimum and Maximum values between the Minimum and Maximum values[0] of the X-Axis for all series from 0 to 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;

Edit: While testing I noticed that resetting the Min&Max Values is not quite obvious. Here is how:

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;

轴最小值自动设置为0,只需使用IsStartesFromZero属性:

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

The code I used for my project is: (based on @TaW's 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 question code above with:

changeYScala(chart1);

don't forget to include:

using System.Windows.Forms.DataVisualization.Charting;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM