Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<Window x:Class="LiveCharts.Wpf.Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LiveCharts.Wpf.Example"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<lvc:PieChart SeriesColors="{Binding SeriesColors, Mode=OneWay}"
LegendLocation="Left"
Series="{Binding Series}"
StartingRotationAngle="0" />
</Grid>
</Window>
namespace LiveCharts.Wpf.Example
{
using System;
using System.Collections.Generic;
using System.Linq;
using Prism.Mvvm;
public class MainWindowViewModel : BindableBase
{
public MainWindowViewModel()
{
List<WorkTime> workTimes = new List<WorkTime>
{
new WorkTime
{
Date = DateTime.Today.AddDays(-3)
, ActualTime = 900
}
, new WorkTime
{
Date = DateTime.Today.AddDays(-3)
, ActualTime = 7200
}
, new WorkTime
{
Date = DateTime.Today.AddDays(-2)
, ActualTime = 1800
}
, new WorkTime
{
Date = DateTime.Today.AddDays(-2)
, ActualTime = 6000
}
, new WorkTime
{
Date = DateTime.Today.AddDays(-2)
, ActualTime = 7200
}
};
Series = new SeriesCollection();
Series.AddRange(workTimes.GroupBy(x => x.Date)
.Select(x => new PieSeries
{
Title = x.Key.ToString("yyyy/MM/dd")
, Values = new ChartValues<int>(new[]
{
x.Sum(v => v.ActualTime)
})
, DataLabels = true
, LabelPoint = p => $"{p.SeriesView.Title}{Environment.NewLine} {p.Participation:P}"
, FontSize = 18.0d
}));
SeriesColors = new ColorsCollection();
SeriesColors.AddRange(new[] {"#FFE65100", "#FFEF6C00", "#FFF57C00", "#FFFB8C00", "#FFFF9800"}
.Select(System.Windows.Media.ColorConverter.ConvertFromString)
.OfType<System.Windows.Media.Color>()
.ToList());
}
public SeriesCollection Series { get; }
public ColorsCollection SeriesColors { get; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.