简体   繁体   English

在LiveCharts中创建饼图切片

[英]Create Pie Chart Slices in LiveCharts

I'm using LiveCharts to create a pie chart. 我正在使用LiveCharts创建饼图。 I have a list of doubles that I want to represent in the pie chart. 我有一个要在饼图中表示的双打列表。 The problem is that the values of the list can, and will change, therefore I want to be able to change the chart accordingly. 问题在于列表的值可以并且将会更改,因此我希望能够相应地更改图表。

Here is some sample code from the LiveCharts website: 这是LiveCharts网站上的一些示例代码:

using System;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.Wpf;

namespace Winforms.PieChart
{
public partial class PieChartExample : Form
{
    public PieChartExample()
    {
        InitializeComponent();

        Func<ChartPoint, string> labelPoint = chartPoint =>
            string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);

        pieChart1.Series = new SeriesCollection
        {
            new PieSeries
            {
                Title = "Maria",
                Values = new ChartValues<double> {3},
                PushOut = 15,
                DataLabels = true,
                LabelPoint = labelPoint
            },
            new PieSeries
            {
                Title = "Charles",
                Values = new ChartValues<double> {4},
                DataLabels = true,
                LabelPoint = labelPoint
            },
            new PieSeries
            {
                Title = "Frida",
                Values = new ChartValues<double> {6},
                DataLabels = true,
                LabelPoint = labelPoint
            },
            new PieSeries
            {
                Title = "Frederic",
                Values = new ChartValues<double> {2},
                DataLabels = true,
                LabelPoint = labelPoint
            }
        };

        pieChart1.LegendLocation = LegendLocation.Bottom;
    }
}

} }

Essentially, I want to do the same thing, but instead, iterate over the list and create an appropriate number of slices for the pie chart. 本质上,我想做同样的事情,但是,要遍历列表并为饼图创建适当数量的切片。 LiveCharts offers PieSlices but the PieChart Control only accepts a SeriesCollection , which is why my code crashes when I assign pieChartData to the chart. LiveCharts提供PieSlicesPieChart Control只接受SeriesCollection ,这就是为什么当我给你我的代码崩溃pieChartData到图表。 Here is my attempt at filling the PieChart: 这是我尝试填充PieChart的尝试:

        LiveCharts.Wpf.PieChart pieChartData = new LiveCharts.Wpf.PieChart();

        foreach (var n in areavalues)
        {
            pieChartData.AddToView(new PieSlice
            {
                PieceValue = n
            });
        }

        areaChart.Series.Add(new PieSeries(pieChartData)); //<-- CRASH

I'm having a difficult time finding any other example code for LiveCharts, does anyone know how to do this? 我很难找到LiveCharts的其他示例代码,有人知道该怎么做吗?

So I finally got it working: 所以我终于让它工作了:

        foreach (var n in classChartData.Slice)
        {
            areaChart.Series.Add(new PieSeries
            {
                Title = n.Key,
                Values = new ChartValues<double> { n.Value }
            });
        }

        areaChart.LegendLocation = LegendLocation.Bottom;

classChartData

    private Dictionary<string, double> slice = new Dictionary<string, double>();

    public Dictionary<string, double> Slice
    {
        get { return slice; }
        set { slice = value; }
    }

    public void AddSlice(string slicename, double slicevalue)
    {
        slice.Add(slicename, slicevalue);
    }

I hope this helps anyone using LiveCharts. 我希望这对使用LiveCharts的人有所帮助。

This was very helpful. 这非常有帮助。 I was having a struggle figuring this out myself until I saw your solution - but it can be much simpler still. 直到我看到您的解决方案之前,我一直在努力弄清楚这一点-但它仍然可以简单得多。 In this example, I'm creating a simple, 2 slice %bad pie chart, but it is easy to extend this to any number of slices 在此示例中,我将创建一个简单的2片%bad饼图,但是很容易将其扩展到任意数量的片

1st, define a placeholder pieChart in your Window or UserControl xaml 1,在您的Window或UserControl xaml中定义一个占位符pieChart

<UserControl x:Class="BillyBob.SimplePieControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mlns:local="clr-namespace:BillyBob"
    xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
    mc:Ignorable="d" d:DesignHeight="185" d:DesignWidth="150">

    <lvc:PieChart x:Name="myPieChart" StartingRotationAngle="0" Height="120"/>
</UserControl>

Then add the slices as PieSeries in your control ctor, with dummy, placeholder values 然后将切片作为PieSeries添加到控件ctor中,并带有虚拟的占位符值

public SimplePieControl()
{
    InitializeComponent();
    myPieChart.Series.Add(new PieSeries { Title = "BAD", Fill = Brushes.Red, StrokeThickness=0, Values = new ChartValues<double> { 0.0 } });
    myPieChart.Series.Add(new PieSeries { Title = "GOOD", Fill = Brushes.Green, StrokeThickness=0, Values = new ChartValues<double> { 100.0 } });

    DataContext = this;
}

To update data - simply index into and update the 1st Value in each Series/slice 要更新数据-只需索引并更新每个系列/切片中的第一个值

internal void RefreshData(double badPct)
{
    myPieChart.Series[0].Values[0] = badPct;
    myPieChart.Series[1].Values[0] = 100.0 - badPct;
}

In case someone is still looking for a solution, this is how i resolved it 万一有人还在寻找解决方案,这就是我解决的方式

Func<ChartPoint, string> labelPoint = chartPoint =>
        string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);

        ChartValues<double> cht_y_values = new ChartValues<double>();

        LiveCharts.SeriesCollection series = new LiveCharts.SeriesCollection();

        foreach (DataRow dr in dt.Rows)
        {
            PieSeries ps = new PieSeries
            {
                Title = dr[x_column_name].ToString(),
                Values = new ChartValues<double> {
                                double.Parse(dr[y1_column_name].ToString())},
                DataLabels = true,
                LabelPoint = labelPoint

            };

            series.Add(ps);
        }
   pieChart.Series = series;
using System;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.Wpf;

namespace Winforms.PieChart
{
    public partial class PieChartExample : Form
    {
        public PieChartExample()
        {
            InitializeComponent();

            Func<ChartPoint, string> labelPoint = chartPoint =>
                string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);

            pieChart1.Series = new SeriesCollection
            {
                new PieSeries
                {
                    Title = "Maria",
                    Values = new ChartValues<double> {3},
                    PushOut = 15,
                    DataLabels = true,
                    LabelPoint = labelPoint
                },
                new PieSeries
                {
                    Title = "Charles",
                    Values = new ChartValues<double> {4},
                    DataLabels = true,
                    LabelPoint = labelPoint
                },
                new PieSeries
                {
                    Title = "Frida",
                    Values = new ChartValues<double> {6},
                    DataLabels = true,
                    LabelPoint = labelPoint
                },
                new PieSeries
                {
                    Title = "Frederic",
                    Values = new ChartValues<double> {2},
                    DataLabels = true,
                    LabelPoint = labelPoint
                }
            };

            pieChart1.LegendLocation = LegendLocation.Bottom;
        }
    }
}

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

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