【问题标题】:Scale Y-Axis of a Chart depending on the Values within a section of X-Values for several Series根据多个系列的一部分 X 值中的值缩放图表的 Y 轴
【发布时间】:2014-06-01 16:00:04
【问题描述】:

我有一个这样的应用程序: 使用图表下方的文本框,用户可以设置图表 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 主持了这个洞示例项目。

【问题讨论】:

  • 那你有什么问题? “我的问题在于实施”并没有给我们太多的继续。
  • @ChrisRyding 如何找到给定部分中所有系列的 Y 最小值。

标签: c# winforms charts axis scaling


【解决方案1】:

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

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

【讨论】:

  • 我正在搜索 y 轴的自动缩放。这是完美的解决方案!辉煌
【解决方案2】:

对于从 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;

【讨论】:

    【解决方案3】:

    我用于项目的代码是:(基于@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;
    }
    

    上面的问题代码中调用了这个方法:

    changeYScala(chart1);
    

    别忘了包括:

    using System.Windows.Forms.DataVisualization.Charting;
    

    【讨论】:

    • 看起来不错。我喜欢地板和天花板的想法! +1
    • 转念一想,我仍然喜欢这个想法,但想写一句话警告:完全添加一点范围的方法取决于实际数字。对于您的大整数,它工作正常,但我的测试用例使用sincosine 绘图,并且需要完全不同的数字或完全不同的公式来完成将值范围扩大“位”的任务。
    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 2020-04-20
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多