user896692
user896692

Reputation: 2371

WPF PieChart filling

I´ve got the following code:

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)
           };
        }
    }
}

My Xaml looks like this:

<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>

My problem is, that the i in the for-loop can be raise up to about 20. So, I would need about 20 PieSeries, to create them dynamic isn´t possible as I know.
How can I manage that?

Upvotes: 0

Views: 2156

Answers (1)

madd0
madd0

Reputation: 9303

If what you are trying to do is simply display a pie chart with several segments, all you have to do is bind a collection to a single PieSeries's ItemsSource property:

<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>

I used the following property:

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

And I get the following chart:

enter image description here

Upvotes: 1

Related Questions