2013-04-03 43 views
1

我有一個包含不同項目的組合框。基於這個組合框的價值,我想要在圖表中呈現的這個項目上註冊的不同小時數。使用WPF圖表工具包根據選定的組合框創建圖表

因爲我已創建的圖表代碼:

    <chartingToolkit:Chart Height="262" HorizontalAlignment="Left" 
        Margin="33,0,0,620" Name="columnChart" 
        VerticalAlignment="Bottom" Width="360" BorderBrush="AntiqueWhite" BorderThickness="1"> 
        <chartingToolkit:ColumnSeries 
         DependentValuePath="Value" 
         IndependentValuePath="Key" 
         ItemsSource="{Binding}"/> 
       </chartingToolkit:Chart> 

認爲「應該」填充,將創建圖形列表中的代碼:

private void showColumnChart() 
    { 
     List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string,int(); 
     List<ProsjektTime> timeListe = new List<ProsjektTime>(); 

     foreach (ProsjektTime p in Master.getDC().ProsjektTimes) 
     { 
      if (p.ProsjektID_FK == prosjekt.ProsjektID) 
      { 
       timeListe.Add(p); 
      } 
     } 

     foreach(ProsjektTime p in timeListe) 
     { 
      valueList.Add(new KeyValuePair<string,int(p.TimeTyper.Type,p.AntallTimer)); 
     } 

     try 
     { 
      columnChart.DataContext = valueList; 
     } 
     catch (InvalidOperationException e) { MessageBox.Show(e+""); } 
    } 

不幸的是我得到的錯誤,「不能此時修改此節點的邏輯子項,因爲樹走「。

但是我發現了一些有趣的東西。如果我將ComboBox的selectedIndex設置爲已註冊一些小時的項目(並非所有項目都有小時註冊),那麼一切正常。在圖表中顯示小時的項目以及沒有任何小時註冊的項目也會顯示爲空白圖表。我錯過了什麼?

回答

0

找到了答案。由於擁有一個dataSourceList和一個chartingToolkit:ColumnSeries,我創建了兩個列表和兩個chartingToolkit:ColumnSeries。繼承人代碼:

 private void prosjektTimeGraf() 
    { 
     int estimerteTimer = 0; 
     int registrerteTimer = 0; 

     List<KeyValuePair<string, int>> estimerListe = new List<KeyValuePair<string, int>>(); 
     List<KeyValuePair<string, int>> registrertListe = new List<KeyValuePair<string, int>>(); 

     foreach (ProsjektTime p in Master.getDC().ProsjektTimes) 
     { 
      if (p.ProsjektID_FK == prosjekt.ProsjektID) 
      { 
       estimerteTimer += p.AntallTimer; 
       estimerListe.Add(new KeyValuePair<string, int>(p.TimeTyper.Type, p.AntallTimer)); 
      } 
     } 

     foreach (ProsjektTimeBruker ptb in Master.getDC().ProsjektTimeBrukers) 
     { 
      if (ptb.ProsjektID_FK == prosjekt.ProsjektID) 
      { 
       registrerteTimer += (int)ptb.AntallTimer; 
       registrertListe.Add(new KeyValuePair<string, int>(ptb.TimeTyper.Type, (int)ptb.AntallTimer)); 
      } 
     } 

     estimerListe.Insert(0, new KeyValuePair<string, int>("Totalt", estimerteTimer)); 
     registrertListe.Insert(0, new KeyValuePair<string, int>("Totalt", registrerteTimer)); 

     try 
     { 
      espTimer.DataContext = estimerListe; 
      regpTimer.DataContext = registrertListe; 
     } 
     catch (InvalidOperationException e) { MessageBox.Show(e+""); } 
    }