0

我正在使用 LiveCharts 创建饼图。我有一个要在饼图中表示的双精度列表。问题是列表的值可以并且将会改变,因此我希望能够相应地更改图表。

以下是 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;
    }
}

}

本质上,我想做同样的事情,但相反,迭代列表并为饼图创建适当数量的切片。LiveCharts 提供PieSlicesPieChart Control只接受 a SeriesCollection,这就是为什么我的代码在分配pieChartData给图表时崩溃的原因。这是我填充饼图的尝试:

        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

我很难找到 LiveCharts 的任何其他示例代码,有人知道该怎么做吗?

4

4 回答 4

3

所以我终于让它工作了:

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

我希望这可以帮助任何使用 LiveCharts 的人。

于 2017-01-10T18:45:25.960 回答
1

这很有帮助。在我看到你的解决方案之前,我一直在努力解决这个问题 - 但它仍然可以简单得多。在此示例中,我创建了一个简单的 2 切片 %bad 饼图,但很容易将其扩展到任意数量的切片

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>

然后将切片作为 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;
}

要更新数据 - 只需索引并更新每个系列/切片中的第一个值

internal void RefreshData(double badPct)
{
    myPieChart.Series[0].Values[0] = badPct;
    myPieChart.Series[1].Values[0] = 100.0 - badPct;
}
于 2017-11-28T00:03:47.920 回答
0

如果有人仍在寻找解决方案,这就是我解决它的方法

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;
于 2018-01-31T14:19:22.467 回答
0
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;
        }
    }
}
于 2018-12-07T10:18:04.823 回答