2013-07-10 47 views
2

因此,我在.NET中使用了一個Chart控件,它使用Y軸的內部自動縮放算法。這一切都很好,但我現在試圖獲取Y軸的最大顯示值作爲一個雙精度值存儲用於進一步格式化。如何從自動縮放圖表控件獲取最大Y軸值

不幸的是,使用ChartControl.ChartAreas [0] .AxisY.Maximum將返回雙倍的NaN,因爲我使用自動縮放。

當使用自動調整軸時,可以獲得Y軸的最大顯示值嗎?

編輯 在我執行操作是建立基本的格式爲條形圖,添加使用AddXY(數據點),最後試圖讓所顯示的Y軸的最大值的順序。即使在添加大量數據點後,使用ChartControl.ChartAreas [0] .AxisY.Maximum仍會返回NaN。

+0

我試圖用自動縮放。如果我沒有添加數據點,我會得到NaN,但是如果我的系列中有任何數據點,我會得到正確的值。你能否添加更多關於你如何設置你的控件的細節? –

+0

更新了一些關於我的圖表控件的更多信息。你的方法仍然爲我返回NaN。 – SRTie4k

回答

3

,直到顯示圖表它不計算最大的值,因此下面的代碼顯示楠:

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表事件和兩個數據系列:

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 
} 
+0

'n'系列有什麼? – Brad

+0

它返回所有系列中Y的最大計算值。 –

+0

OP沒有真正說清楚,所以也許它並不重要,但現在這將返回只有系列[0]的最大值。 – Brad

5

呼叫chart.ChartAreas[0].RecalculateAxesScale();

然後chart1.ChartAreas[0].AxisY.MaximumMinimum將正確設置。

+0

這應該是公認的答案! – TaW

1

我需要在我的RecalculateAxesScale調用之前設置它們,因爲我在顯示同一圖表控件上的另一個數據集時設置了它們。

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

編輯:爲了澄清,我被重用相同的圖表控制來顯示取決於用戶選擇不同的數據集的圖表。其中一個選項將ScaleView.Size設置爲默認值(NaN)以外的值。所以我需要將它重新設置爲默認值,以允許RecalculateAxesScale像它應該那樣工作。

相關問題