Search code examples
c#livecharts

How to create a Pie Chart with LiveCharts


I need help creating a (test) pie chart with LiveCharts. I am using this xaml code

<Grid>
<lvc:PieChart x:Name="myPieChart"/>
</Grid>

and then in code behind

LiveCharts.Wpf.PieSeries ps = new LiveCharts.Wpf.PieSeries
{
    Values = new LiveCharts.ChartValues<decimal> { 1, 3} 
};
myPieChart.Series.Add(ps);

But instead of getting one pie chart with 2 slices, I get 2 concentric pie charts, each with 1 complete slice only.


Solution

  • Ok, I was able to get the job done by doing this

    LiveCharts.SeriesCollection psc = new LiveCharts.SeriesCollection
    {
        new LiveCharts.Wpf.PieSeries
        {
            Values = new LiveCharts.ChartValues<decimal> {1},
        },
        new LiveCharts.Wpf.PieSeries
        {
            Values = new LiveCharts.ChartValues<decimal> {3},
        }
    };
    
    foreach (LiveCharts.Wpf.PieSeries ps in psc)
    {
        myPieChart.Series.Add(ps);
    }
    

    If anybody is interested, I discovered that doing

    LiveCharts.SeriesCollection psc = new LiveCharts.SeriesCollection
    {
        new LiveCharts.Wpf.PieSeries
        {
            Values = new LiveCharts.ChartValues<decimal> {1,1},
        },
        new LiveCharts.Wpf.PieSeries
        {
            Values = new LiveCharts.ChartValues<decimal> {3,7},
        }
    };
    
    foreach (LiveCharts.Wpf.PieSeries ps in psc)
    {
        myPieChart.Series.Add(ps);
    }
    

    creates 2 concentric pie charts, one with values (1,3) and the other with values (1,7).