天天看點

WPF程式設計,Live Charts使用說明(20)——餅圖背景前台

WPF程式設計,Live Charts使用說明(20)——餅圖背景前台

背景

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

前台

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