Create Pie Chart Slices in LiveCharts

10.3k views Asked by At

I'm using LiveCharts to create a pie chart. 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:

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. Here is my attempt at filling the 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?

4

There are 4 answers

0
Michael Moore On

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

1st, define a placeholder pieChart in your Window or UserControl 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>

Then add the slices as PieSeries in your control ctor, with dummy, placeholder values

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;
}
2
Aung Kyaw Nyunt On
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;
        }
    }
}
0
John On

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.

0
Anand A On

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;