簡體   English   中英

在LiveCharts中創建餅圖切片

[英]Create Pie Chart Slices in LiveCharts

我正在使用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到圖表。 這是我嘗試填充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

我很難找到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餅圖,但是很容易將其擴展到任意數量的片

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

萬一有人還在尋找解決方案,這就是我解決的方式

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