将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; }

但是窗口似乎是空的。

解决方法

  1. 您应该将string的值传输到double
  2. 使用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);
    }
};

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...