在实时图表中创建饼图扇区



我正在使用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只接受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 的任何其他示例代码,有人知道如何做到这一点吗?

所以我

终于让它工作了:

        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的人都有帮助。

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

首先,在窗口或用户控件 xaml 中定义占位符饼图

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

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

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

相关内容

  • 没有找到相关文章

最新更新