c# - 如何使用扩展 WPF 工具包创建简单的饼图
问题描述
我对 C# 开发相当陌生。我正在尝试使用 Visual Studio、C# 和扩展 WPF 工具包创建一个简单的饼图。下面的代码是我正在尝试构建的 dll 的一部分(Revit 插件)。我使用 NuGet 安装了扩展的 WPF 工具包。我找不到任何教程或示例,所以我尝试从我在不同的在线资源上找到的一些片段中放置代码。目前,我有
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>
和
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));
}
xaml 背后的代码是
public partial class CompareToMultiLOD : Page
{
public CompareToMultiLOD()
{
InitializeComponent();
DataContext = new CompareToMultiLODViewModel();
}
我不知道它是否相关,但是在调试时我看到在创建 PieCollection 之前分配了 DataContext,然后在 PieCollection 初始化时触发了一次 OnPropertyChanged。PropertyChanged 似乎被触发一次(我不明白,因为我添加了两个值)。
我不确定 ViewModel 是存储饼图使用的数据的正确位置,但我暂时将它放在那里(因为它显然是一个模拟类)。
目前我只想让它工作。任何帮助表示赞赏!
解决方案
Pie
不是真正的数据系列图表,而是代表椭圆的单个部分的形状:https ://github.com/xceedsoftware/wpftoolkit/wiki/PieChart 。
如果你想要一个饼图,你应该看看这个 NuGet 包和这个答案,以获取如何使用它来创建饼图的示例。
这应该给你一个饼图:
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>
推荐阅读
- python - 从客户端摄像头到烧瓶服务器的实时流视频
- django - 如何在django rest框架的post api请求中提供json数据
- api - 设计 REST api 关系的良好实践
- php - Laravel 中的 Axios 和 Blade,如何?对于异步请求类别过滤器
- activemq-artemis - ActiveMQ Artemis 为独立代理配置故障转移和静态分配的队列
- java - 使用线性递归合并/相乘数组中的元素
- reactjs - 使用 Draft.js 从数据库中检索文本
- javascript - 嵌套/递归组件错误,未注册组件
- angular - 在 GitLab CI 页面中未检测到 Angular PWA
- android - 使用 Python 脚本选择 SQLite 失败