Escalar o eixo Y de um gráfico, dependendo dos valores dentro de uma secção de valores X para várias séries

Tenho uma petição destas.: Chart with scaling for the X-axis Com as caixas de texto abaixo do gráfico, o usuário pode definir o min e o max do eixo X do Gráfico. Este é o código para ele:

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;
}
Agora eu gostaria de ter o eixo Y escalado automaticamente. Y-min deve ser calulado como o valor mínimo de todas as séries na secção de (X-min e X-max) e Y-max como o máximo de todas as séries na secção seleccionada. O meu problema é a implementação.

Neste exemplo, Y-min deve ser mudou para cerca de 50.

fui eu que organizei o projecto hole examplepleproject aqui emGitHup .

Author: TaW, 2014-06-01

3 answers

Isto irá escalar o eixo Y até aos valores mínimo e máximo entre os valores mínimo e máximo[0] do eixo X para todas as séries de 0 a 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;

Editar: ao testar, reparei que a redefinição dos valores Min&Max não é muito óbvia. Eis como:

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;
 7
Author: TaW, 2014-06-02 19:53:33

O eixo mínimo é definido automaticamente para 0, basta usar a propriedade Istartesfromzero:

chart.ChartAreas[0].AxisY.IsStartedFromZero = false;
 6
Author: idanp, 2016-02-23 18:04:06

O código que usei para o meu projecto é: (baseado na resposta de @TaW)

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;
}

Este método é chamado no código de perguntas acima com:

changeYScala(chart1);
Não te esqueças de incluir:
using System.Windows.Forms.DataVisualization.Charting;
 3
Author: kimliv, 2014-06-03 07:41:36