I am using MVVM and SL4. I have a collection of VoteResults that are being bound to a ItemsControl. I want a PieChart in EACH VoteResult item. A VoteResult item object properties looks like this: VotedYes 10; VotedNo 4; Abstained 2; DidntVote 6;
How can I pass these VoteItem property values to build the pie chart's series ItemSource in XAML? So开发者_如何学Cmething like :
<charting:Chart>
<charting:Chart.Series>
<charting:PieSeries>
<charting:PieSeries.ItemsSource>
<controls:ObjectCollection>
</controls:ObjectCollection>
</charting:PieSeries.ItemsSource>
</charting:PieSeries>
</charting:Chart.Series>
</charting:Chart>
The charting controls are designed to present data that has been processeda and aggregated ready for presentation.
Sounds to me like you want a PieSeries to present a series that contains the Categories "VotedYes", "VotedNo", "Abstained" and "Did not vote" as its independent value and a vote count as the dependant value. In that case you need to pass to the ItemsSource a collection containing this set of data, a single object will not do.
You will need to pass the VoteResult object through a function that returns something like IEnumerable<KeyValuePair<String, Int32>>
.
public IEnumerable<KeyValuePair<String, Int32>> ProcessVoteResult(VoteResult vr)
{
yield return new KeyValuePair<String, In232>("Voted Yes", vr.VotedYes);
yield return new KeyValuePair<String, In232>("Voted No", vr.VotedNo);
yield return new KeyValuePair<String, In232>("Abstained", vr.Abstained);
yield return new KeyValuePair<String, In232>("Did Not Vote ", vr.DidntVote );
}
Now you will probably want to bind the VoteResult
since you appear to be using MVVM (from looking at your other questions). You you will need a value converter:-
public class VoteResultToEnumerableConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
return ProcessVoteResult((VoteResult)value);
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
You would then have this converter in a static resources somewhere above the chart:-
<local:VoteResultToEnumerableConverter x:Key="vrconv" />
Then your chart would look like:-
<toolkit:Chart>
<toolkit:PieSeries
ItemsSource="{Binding SomeVoteResult, Converter={StaticResource vrconv}}"
DependentValuePath="Key"
IndependendValuePath="Value"/>
</toolkit:Chart>
精彩评论