简体   繁体   English

从 Linq 查询在 LiveCharts2 中构建饼图

[英]Build PieChart in LiveCharts2 from Linq query

I am trying to get PieCharts visible in my WPF application.我试图让 PieCharts 在我的 WPF 应用程序中可见。 There is an example available and it seems to be working fine.有一个可用的示例,它似乎工作正常。 However I am trying to do the same from linq query and can't understand how to solve that equation.但是我试图从 linq 查询中做同样的事情,但不明白如何解决这个等式。

Here is an example:这是一个例子:

Xaml part: Xaml 部分:

<lvc:PieChart x:Name="PieChart" LegendPosition="Right" Series="{Binding OnLoaded}" InitialRotation="-90"></lvc:PieChart>

and C# code:和 C# 代码:

private async void OnLoaded()
        {
                IEnumerable<double> a = new List<double> { 1 };
                IEnumerable<double> b = new List<double> { 0 };
                IEnumerable<double> c = new List<double> { 0 };

                IEnumerable<ISeries> Series = new List<ISeries>
                {
                    new PieSeries<double> { Values = a, Name = "a" },
                    new PieSeries<double> { Values = b, Name = "b" },
                    new PieSeries<double> { Values = c, Name = "c"  }
                };
                LivePieChart3.Series = Series;
            }
        }

Then I am trying to do the same from linq, but I think this is completely wrong?:然后我试图从 linq 做同样的事情,但我认为这是完全错误的?:

private void InitializePieChart()
{
  List<ISeries> projectNumbers = this.Projects.Select(x => new List<ISeries>
  {
   new PieSeries<double> 
   {
    Values = x.Revenue,
    Name = x.ProjectName,
   }
  });

  this.Series = new ObservableCollection<ISeries>(projectNumbers);
}

How to create a new new PieSeries<double> after new List<ISeries> ?如何在new List<ISeries>之后创建一个新new PieSeries<double> > ?

Here is my model:这是我的 model:

  public class ProjectModel
  {
    public Guid Id { get; set; }
    public string ProjectName { get; set; }
    public string ProjectNumber { get; set; }
    public double Revenue { get; set; }
    public DateTime CreationDate { get; set; }

    public virtual ICollection<TaskModel> Tasks { get; set; }
  }

Projects in ViewModel: ViewModel 中的项目:

private ObservableCollection<ProjectModel> projects;
public ObservableCollection<ProjectModel> Projects
{
  get => this.projects;
  set
  {
    this.projects = value;
    this.OnPropertyChanged();
  }
}

Here is LiveCharts2 project: https://github.com/beto-rodriguez/LiveCharts2这是 LiveCharts2 项目: https://github.com/beto-rodriguez/LiveCharts2


EDIT:编辑:

With Ash's suggestion in comments I receive following:根据 Ash 在评论中的建议,我收到以下内容:

在此处输入图像描述

private void InitializePieChart()
{
    IEnumerable<ISeries> projectNumbers = this.Projects.Select(x => 
    new PieSeries<double> 
    {
        Values = new List<double> { x.Revenue },
        Name = x.ProjectName,
    });

    this.Series = new ObservableCollection<ISeries>(projectNumbers);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM