[英]How to create a simple Pie chart using Extended WPF Toolkit
I'm fairly new to C# development. 我是C#开发的新手。 I'm trying to create a simple Pie chart using Visual Studio, C# and Extended WPF Toolkit. 我正在尝试使用Visual Studio,C#和Extended WPF Toolkit创建一个简单的饼图。 The code that follows is a part of a dll i'm trying to build (plug in for Revit). 以下代码是我尝试构建的dll的一部分(插入Revit)。 I installed Extended WPF Toolkit using NuGet. 我使用NuGet安装了Extended WPF Toolkit。 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 xaml背后的代码是
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. 我不知道它是否相关,但是在调试时我看到在创建PieCollection之前已分配DataContext,然后在PieCollection初始化时触发一次OnPropertyChanged。 PropertyChanged seems to be triggered once (which i do not understand, since i add two values). PropertyChanged似乎被触发一次(我不理解,因为我添加了两个值)。
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). 我不确定ViewModel是否是存储饼图使用的数据的正确位置,但我暂时将其放置在此处(因为它显然是模拟类)。
At the moment i would just like to make it work. 目前,我只想使其工作。 Any help is appreciated! 任何帮助表示赞赏!
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 . Pie
实际上并不是具有数据序列的图表,而是代表椭圆的单个部分的形状: 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. 如果要使用饼图,则应查看此NuGet软件包和此答案,以获取有关如何使用它创建饼图的示例。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.