c# - 将 PieChart 与 LiveCharts 和 WPF .NET Core 一起使用
问题描述
我为 WPF 使用图表插件: https ://lvcharts.net
在 xaml 我有:
<Window x:Class="GoogleDriveManager.WPF.ChartWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GoogleDriveManager.WPF" xmlns:wpf="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
Title="Chart statistics" Height="450" Width="800">
<Grid>
<wpf:PieChart LegendLocation="Bottom" Series="{Binding Items}"/>
</Grid>
</Window>
和后面的代码:
public ChartWindow()
{
InitializeComponent();
DataContext = this;
Items = new SeriesCollection();
ContentRendered += (s, ev) =>
{
Dictionary<string, List<string>> data = GetData(...);
foreach (var item in data)
{
ISeriesView series = new PieSeries(new { title = item.Key });
IChartValues values = new ChartValues<string>(item.Value);
series.Values = values;
Items.Add(series);
}
};
}
public SeriesCollection Items { get; }
但似乎窗口是空的。
解决方案
- 您应该将
string
值转移到double
- 使用
PieSeries
属性Title
分配标题
我创建了一个简单的案例,这段代码有效:
ContentRendered += (s, ev) =>
{
Dictionary<string, List<string>> data = new Dictionary<string, List<string>>(){
{"AAA", new List<string>(){"1","2"}},
{"BBB", new List<string>(){"3","4"}}
};
foreach (var item in data)
{
var curValues = item.Value.Select(x => double.Parse(x)).ToList();
ISeriesView series = new PieSeries{
Title = item.Key,
Values = new ChartValues<double>(curValues),
DataLabels = true
};
Items.Add(series);
}
};
推荐阅读
- c# - 在从不重新分配字段的情况下复制方法主体中的字段引用以读取它有什么好处?
- django - 如何在 Django 和 Python 中使用 JWT(JSON Web 令牌)来创建用于注册和登录的 REST API
- ajax - Laravel BadMethodCallException:方法 [signdoc] 不存在
- python - 对于训练和验证集,CNN 模型的准确度永远不会很高
- amazon-dynamodb - 是否可以在 DynamoDBMapper 保存表达式中使用“ADD”函数
- wpf - WPF OnKeyDown Alt+Key 组合产生哔声
- mysql - MySQL (InnoDB) 死锁
- c++ - C ++矩阵 - 获取最低值与其后列中的最高值在同一行中的列数
- javascript - 如何使用本机反应在另一个屏幕上发送和显示文本输入的值?
- autocomplete - Sublime Text 3:自动完成对 for 循环使用了不正确的语法