2011-11-29 159 views
0

從來就得到了下面的代碼:WPF餅圖填充

private void LoadPieChartData() 
{ 
    LinkedList<String> kostenstellen = rep.GetKostenstellen(); 

    for (int i = 0; i < kostenstellen.Count; i++) 
    { 
     ((ColumnSeries)PieChart.Series[i]).ItemsSource = new KeyValuePair<string, double>[]{ 
      new KeyValuePair<string,double>(kostenstellen.ElementAt(i), i+1) 
      }; 
     } 
    } 
} 

我的XAML看起來是這樣的:

<DVC:Chart HorizontalAlignment="Left" Margin="625,44,0,0" Name="PieChart" VerticalAlignment="Top" Height="276" Width="256" > 
    <DVC:Chart.Series> 
     <DVC:PieSeries Title="FirstPiece" ItemsSource="{Binding LoadPieChartData}" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}" /> 
     <DVC:PieSeries Title="SecondPiece" ItemsSource="{Binding LoadPieChartData}" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}" /> 
    </DVC:Chart.Series> 
</DVC:Chart> 

我的問題是,在我的for循環可籌集最多到大約20個。所以,我需要大約20個PieSeries,據我所知,創造動態是不可能的。
我該如何管理?

+0

你真的想要不同的系列,或者你只是想顯示一個最多20段的圖表? – madd0

回答

1

如果你正在嘗試做的僅僅是顯示與若干段餅圖什麼,你所要做的就是收集綁定到單個PieSeriesItemsSource屬性:

<DVC:Chart Name="PieChart"> 
    <DVC:Chart.Series> 
     <DVC:PieSeries ItemsSource="{Binding LoadPieChartData}" 
         IndependentValueBinding="{Binding Path=Key}" 
         DependentValueBinding="{Binding Path=Value}" /> 
    </DVC:Chart.Series> 
</DVC:Chart> 

我用下面的屬性:

public IEnumerable<KeyValuePair<string,int>> LoadPieChartData 
{ 
    get 
    { 
     for (int i = 0; i < 20; i++) 
     { 
      yield return new KeyValuePair<string, int>("Item " + i, i); 
     } 
    } 
} 

我也得到如下圖:

enter image description here

+0

太好了,謝謝! – user896692

+0

好的,現在我試了一下,但沒有奏效。 我把屬性放在代碼隱藏文件中,但是如果我運行應用程序,圖表不會顯示任何東西...... – user896692

+2

@ user896692你的DataContext可能設置不正確,所以你的程序不知道在哪裏尋找您綁定的財產。如果屬性是承載圖表的窗口的一部分,則可以嘗試在窗口的構造函數 – madd0