微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 根据几个系列的X值的一部分中的值,缩放图表的Y轴

我有一个这样的应用:

使用图表下方的文本框,用户可以设置图表的X轴的最小值和最大值.这是它的代码

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

现在我想让Y轴自动缩放.应将Y-min计算为(X-min和X-max)部分中所有系列的最小值,并将Y-max计算为所选部分中所有系列的最大值.我的问题是实施.

在此示例中,Y-min应更改为大约50.

我在GitHup处主持了这个漏洞示例项目.

解决方法

对于从0到1的所有系列,这会将Y轴缩放到X轴的最小值和最大值[0]之间的最小值和最大值:
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;

编辑:测试时我注意到重置最小值和最大值并不是很明显.方法如下:

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;

原文地址:https://www.jb51.cc/csharp/98671.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐