【问题标题】:How to get maximum Y-axis value from autoscaling chart control如何从自动缩放图表控件中获取最大 Y 轴值
【发布时间】:2013-07-10 19:45:15
【问题描述】:

所以我在 .NET 中使用图表控件,它使用 Y 轴的内部自动缩放算法。这一切都很好,但我现在正在尝试获取 Y 轴的最大显示值以存储为双精度以用于进一步格式化。

不幸的是,使用 ChartControl.ChartAreas[0].AxisY.Maximum 返回双 NaN,因为我使用的是自动缩放。

在使用自动缩放轴时,是否可以获得 Y 轴的最大显示值?

编辑 我执行操作的顺序是建立条形图的基本格式,使用 AddXY() 添加数据点,然后最后尝试获取显示的 Y 轴的最大值。即使添加了许多数据点,使用 ChartControl.ChartAreas[0].AxisY.Maximum 仍然返回 NaN。

【问题讨论】:

  • 我尝试了自动缩放。如果我不添加任何数据点,我会得到 NaN,但如果我的系列中有任何数据点,我会得到正确的值。您能否添加更多关于您如何设置控件的详细信息?
  • 更新了一些关于我的图表控件的更多信息。你的方法仍然为我返回 NaN。

标签: c# .net winforms visual-studio-2010


【解决方案1】:

致电chart.ChartAreas[0].RecalculateAxesScale();

那么chart1.ChartAreas[0].AxisY.Maximum和Minimum就会被正确设置。

【讨论】:

    【解决方案2】:

    在图表显示之前它不会计算最大值,因此以下代码显示 NaN:

    public Form1()
    {
        InitializeComponent();
        this.chart1.Series.Clear();
        this.chart1.Series.Add("My Data");
        this.chart1.Series[0].Points.AddXY(1, 1);
        this.chart1.Series[0].Points.AddXY(2, 2);
        this.chart1.Series[0].Points.AddXY(3, 6);
        MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns NaN
    }
    

    但在图表显示后检查会给出正确的值:

    public Form1()
    {
        InitializeComponent();
        this.chart1.Series.Clear();
        this.chart1.Series.Add("My Data");
        this.chart1.Series[0].Points.AddXY(1, 1);
        this.chart1.Series[0].Points.AddXY(2, 2);
        this.chart1.Series[0].Points.AddXY(3, 6);
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns 8
    }
    

    或者,您可以在设置数据后立即执行更新(但这在表单构造函数中不起作用,因为图表尚未显示):

    private void button1_Click(object sender, EventArgs e)
    {
        this.chart1.Series.Clear();
        this.chart1.Series.Add("My Data");
        this.chart1.Series[0].Points.AddXY(1, 1);
        this.chart1.Series[0].Points.AddXY(2, 2);
        this.chart1.Series[0].Points.AddXY(3, 6);
        this.chart1.Update();
        MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns 8
    }
    

    这是使用 OnShown Form 事件和两个数据系列的另一种方法:

    public Form1()
    {
        InitializeComponent();
        this.chart1.Series.Clear();
        this.chart1.Series.Add("My Data");
        this.chart1.Series[0].Points.AddXY(1, 1);
        this.chart1.Series[0].Points.AddXY(2, 2);
        this.chart1.Series[0].Points.AddXY(3, 6);
        this.chart1.Series.Add("My Data2");
        this.chart1.Series[1].Points.AddXY(1, 1);
        this.chart1.Series[1].Points.AddXY(2, 9);
    }
    
    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        this.chart1.Update();
        MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns 10
    }
    

    【讨论】:

    • n 系列有什么?
    • 它返回为 Y 在所有系列中计算的最大值。
    • OP 并没有真正说清楚,所以也许没关系,但现在这将返回唯一系列 [0] 的最大值。
    • 我尝试了两个数据系列并得到了正确的值,考虑到我们在 Y 轴上取最大值并且两个系列都在同一个图表区域中,这是有道理的。 (见我上面的最后一个例子。)
    • 明天我会在我的办公桌前试一试 Update() 函数并回来报告。
    【解决方案3】:

    我需要在调用 RecalculateAxesScale 之前设置这些,因为我在同一图表控件上显示另一个数据集时设置了它们。

        chart.ChartAreas[0].AxisY.ScaleView.Size = double.NaN;
        chart.ChartAreas[0].AxisY2.ScaleView.Size = double.NaN;
    

    编辑:为了澄清,我根据用户选择重复使用相同的图表控件来显示不同数据集的图表。其中一项选择将 ScaleView.Size 设置为默认值 (NaN) 以外的值。所以我需要将其设置回默认值,以允许 RecalculateAxesScale 正常工作。

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2011-10-20
      相关资源
      最近更新 更多