网上流传的版本:
using System;
using System.Windows.Controls;
using LiveCharts;
using LiveCharts.Wpf;
namespace Wpf.PieChart
{
public partial class PieChartExample : UserControl
{
public PieChartExample()
{
InitializeComponent();
PointLabel = chartPoint =>
string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
DataContext = this;
}
public Func<ChartPoint, string> PointLabel { get; set; }
private void Chart_OnDataClick(object sender, ChartPoint chartpoint)
{
var chart = (LiveCharts.Wpf.PieChart) chartpoint.ChartView;
//clear selected slice.
foreach (PieSeries series in chart.Series)
series.PushOut = 0;
var selectedSeries = (PieSeries) chartpoint.SeriesView;
selectedSeries.PushOut = 8;
}
}
}
XAML
<UserControl x:Class="Wpf.PieChart.PieChartExample"
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"
xmlns:local="clr-namespace:Wpf.PieChart"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500"
d:DataContext="{d:DesignInstance local:PieChartExample}">
<Grid>
<lvc:PieChart LegendLocation="Bottom" DataClick="Chart_OnDataClick" Hoverable="False" DataTooltip="{x:Null}">
<lvc:PieChart.Series>
<lvc:PieSeries Title="Maria" Values="3" DataLabels="True"
LabelPoint="{Binding PointLabel}"/>
<lvc:PieSeries Title="Charles" Values="4" DataLabels="True"
LabelPoint="{Binding PointLabel}"/>
<lvc:PieSeries Title="Frida" Values="6" DataLabels="True"
LabelPoint="{Binding PointLabel}"/>
<lvc:PieSeries Title="Frederic" Values="2" DataLabels="True"
LabelPoint="{Binding PointLabel}"/>
</lvc:PieChart.Series>
</lvc:PieChart>
</Grid>
</UserControl>
效果图、
效果可以代码不如意于是用chatgpt生成一段
PointLabel = chartPoint =>
string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
DataContext = this;
// 创建饼图数据
ChartValues<double> chartValues = new ChartValues<double> { 40,20,30 };
// 设置饼图的值
pieSeries.Values = chartValues;
xaml
<Grid>
<lvc:PieChart LegendLocation="Bottom" DataClick="Chart_OnDataClick" Hoverable="False" DataTooltip="{x:Null}">
<lvc:PieChart.Series>
<lvc:PieSeries Title="Maria" DataLabels="True" x:Name="pieSeries" LabelPoint="{Binding PointLabel}"/>
</lvc:PieChart.Series>
</lvc:PieChart>
</Grid>
图:
不尽人意啊预算各种gpt和百度,最后还得靠自己。
前台XAML代码
<Grid>
<lvc:PieChart LegendLocation="Bottom" DataClick="Chart_OnDataClick" Hoverable="False" DataTooltip="{x:Null}">
<lvc:PieChart.Series>
<lvc:PieSeries Title="Maria" DataLabels="True" x:Name="pieSeries" LabelPoint="{Binding PointLabel}"/>
<lvc:PieSeries Title="Charles" DataLabels="True" x:Name="pieSeries1" LabelPoint="{Binding PointLabel}"/>
</lvc:PieChart.Series>
</lvc:PieChart>
</Grid>
后台代码
using System;
using System.IO;
using System.Windows;
using System.Management;
using System.Diagnostics;
using Org.BouncyCastle.Utilities;
using LiveCharts.Wpf;
using LiveCharts;
using System.Xml.Linq;
using System.ComponentModel;
using YadinghaoOri.ViewModels.Main;
namespace YadinghaoOri
{
/// <summary>
/// Index.xaml 的交互逻辑
/// </summary>
public partial class Index : Window
{
public Index()
{
InitializeComponent();
PointLabel = chartPoint =>
string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
DataContext = this;
// 创建饼图数据
ChartValues<double> chartValues = new ChartValues<double> { 40 };
ChartValues<double> chartValues1 = new ChartValues<double> { 20 };
// 设置饼图的值
pieSeries.Values = chartValues;
pieSeries1.Values = chartValues1;
}
public Func<ChartPoint, string> PointLabel { get; set; }
private void Chart_OnDataClick(object sender, ChartPoint chartpoint)
{
var chart = (LiveCharts.Wpf.PieChart)chartpoint.ChartView;
//clear selected slice.
foreach (PieSeries series in chart.Series)
series.PushOut = 0;
var selectedSeries = (PieSeries)chartpoint.SeriesView;
selectedSeries.PushOut = 8;
}
}
}
好了大功告成