I have a piechart that I have defined as below:
<chartingToolkit:Chart DataContext="1,10 2,20 3,30 4,40"
HorizontalAlignment="Left" Margin="334,238,0,0"
Name="chart1" VerticalAlignment="Top" Height="177"
Width="218" BorderBrush="#00000000">
<chartingToolkit:PieSeries
ItemsSource="{Binding PieCollection}"
IndependentValueBinding="{Binding Path=Name}"
DependentValueBinding="{Binding Path=Share}" />
</chartingToolkit:Chart>
And my xaml is also referenced here:
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
So no problem there. I have a dataset I declare in my ViewModel:
private ObservableCollection<PiePoint> _pieCollection;
public ObservableCollection<PiePoint> PieCollection { get { return _pieCollection; } set { _pieCollection = value; OnPropertyChanged("PieCollection"); } }
Where PiePoint is an object I define like so:
public class PiePoint
{
public string Name { get; set; }
public Int16 Share { get; set; }
}
When I start up my ViewModel in my constructor, I added some test data points here to see if I could get some data on my chart:
PieCollection = new ObservableCollection<PiePoint>();
PieCollection.Add(new PiePoint { Name = "Mango", Share = 10 });
PieCollection.Add(new PiePoint { Name = "Banana", Share = 36 });
But nothing shows up on the chart. The breakpoint hits, I see the chart's background, and I know that the items source is bound to the right collection, but I can't seem to get it to work. Any suggestions?
Here's what I got using your data:
XAML:
Result:
Notice that I'm creating the
PieCollection
within XAML, so it is associated with theDataContext
for the chart. In your case you're creating yourPieCollection
in code behind, so you probably need to do something likeEDIT: creating
PieCollection
dynamically:Result: