2015-05-07 55 views
0

我正在嘗試使用字典中的值創建Chart,但是我的Chart不顯示任何內容。我的代碼運行良好,沒有錯誤,我在表單上有一個圖表。圖表不會顯示數據點

我從來沒有在C#中創建圖表,所以我不知道我在做什麼。

這是我填寫它的代碼。

如果我刪除StatChart.update()它會顯示項目在Dictionary圖表上的總人數,但僅此而已

private void FillStatChart(Dictionary<string, int> dictionary) 
    { 
     int dicLength = dictionary.Count; 
     StatChart.Series.Clear(); 
     StatChart.Series.Add("Occurences"); 
     foreach (System.Windows.Forms.DataVisualization.Charting.DataPoint point in StatChart.Series["Occurences"].Points) 
     { 
      foreach (KeyValuePair<string, int> pair in dictionary) 
      { 
       for (int idx = 0; idx < dicLength; idx++) 
       { 
        point.SetValueXY(idx, pair.Value); 

        StatChart.Update(); 
       } 
      } 
     } 
    } 
+0

您是否正在更新現有點? –

回答

1

您需要將新DataPointsAddPoints集合。

更改此:

foreach (System.Windows.Forms.DataVisualization.Charting.DataPoint point in StatChart.Series["Occurences"].Points) 
    { 
     foreach (KeyValuePair<string, int> pair in dictionary) 
     { 
      for (int idx = 0; idx < dicLength; idx++) 
      { 
       point.SetValueXY(idx, pair.Value); 

       StatChart.Update(); 
      } 
     } 
    } 

這樣:

Series S = StatChart.Series["Occurences"]; 
    for (int idx = 0; idx < dicLength; idx++) 
    { 
     KeyValuePair<string, int> pair = dictionary.ElementAt(idx); 
     S.Points.AddXY(idx, pair.Value); 
     S.Points[idx].ToolTip = pair.Key; // optional tooltip 
    } 

SetValueXY僅供改變一個DataPoint的值更高版本。

+0

謝謝您將所有項目顯示爲點,但它們都具有相同的y值,即使我知道它們並不完全相同。任何想法爲什麼這可能是? –

+0

哎呀,你的三重環已經讓我絆倒了。我想你只需要一個循環..如果你喜歡你可以在工具提示中添加鍵字符串... – TaW

+0

是的,它現在工作的很棒。但是有沒有辦法在x軸下面朝向數據顯示密鑰字符串? –