Reputation: 140
I'm fairly new to C# development. I'm trying to create a simple Pie chart using Visual Studio, C# and Extended WPF Toolkit. The code that follows is a part of a dll i'm trying to build (plug in for Revit). I installed Extended WPF Toolkit using NuGet. I was not able to find any tutorial or examples, so i tried to put up the code from some pieces i found on differnt online sources. At the moment, i have
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
<Grid Grid.Row="1">
<xctk:Pie x:Name="foobar"
DataContext="{Binding PieCollection, UpdateSourceTrigger=PropertyChanged}" >
</xctk:Pie>
</Grid>
and
public class PiePoint
{
public string Name { get; set; }
public Int16 Share { get; set; }
}
public class CompareToMultiLODViewModel : INotifyPropertyChanged
{
private ObservableCollection<PiePoint> _pieCollection;
public ObservableCollection<PiePoint> PieCollection
{
get { return _pieCollection; }
set { _pieCollection = value; OnPropertyChanged("PieCollection"); }
}
public CompareToMultiLODViewModel()
{
CompareToMultiLODBtnCommand = new MRCommand(this);
PieCollection = new ObservableCollection<PiePoint>();
PieCollection.Add(new PiePoint { Name = "Mango", Share = 10 });
PieCollection.Add(new PiePoint { Name = "Banana", Share = 36 });
}
private PropertyChangedEventHandler _PropertyChanged;
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add
{
//((INotifyPropertyChanged)PieCollection).PropertyChanged += value;
_PropertyChanged += value;
}
remove
{
//((INotifyPropertyChanged)PieCollection).PropertyChanged -= value;
_PropertyChanged -= value;
}
}
private void OnPropertyChanged(string PropertyName)
{
_PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
The code behind xaml is
public partial class CompareToMultiLOD : Page
{
public CompareToMultiLOD()
{
InitializeComponent();
DataContext = new CompareToMultiLODViewModel();
}
I do not know if it's relevant, but when debugging i see that DataContext is assigned before the PieCollection is created, and then OnPropertyChanged is triggered once, on PieCollection initialization. PropertyChanged seems to be triggered once (which i do not understand, since i add two values).
I am not sure that ViewModel is the right place to store data that Pie chart uses, but i placed it there temporaly (since it's a mock class, obviously).
At the moment i would just like to make it work. Any help is appreciated!
Upvotes: 1
Views: 8482
Reputation: 169400
Pie
is not really a chart with data series but a shape that represents a single portion of an ellipse: https://github.com/xceedsoftware/wpftoolkit/wiki/PieChart.
If you want a pie chart you should take a look at this NuGet package and this answer for an example of how to use it to create a pie chart.
This should give you a pie chart:
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
...
<Grid Grid.Row="1">
<chartingToolkit:Chart Margin="0" Title="Chart Title" DataContext="{Binding PieCollection}">
<chartingToolkit:PieSeries ItemsSource="{Binding}"
DependentValuePath="Share"
IndependentValuePath="Name">
</chartingToolkit:PieSeries>
</chartingToolkit:Chart>
</Grid>
Upvotes: 3