【Chart控件】饼状图

 

参考:https://eren.ws/2013/10/15/using-graphs-and-charts-in-windows-store-apps-boredom-challenge-day-11/

添加库

WPF没有系统的Chart控件,因此要使用第三方的chart控件库

1、在Nuget中添加DotNetProjects.DataVisualization.Toolkit。

饼图 功能介绍

(1)图例设置

 <!--表格的LegEnd 隐藏图例-->
            <chart:Chart.LegendStyle>
                <Style TargetType="visualizationToolkit:Legend">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Setter Property="Width" Value="0"/>
                    <Setter Property="Height" Value="0"/>
                </Style>
            </chart:Chart.LegendStyle>

(2)绘图区设置

   <!--表格的绘图区域  将绘图区背景色设置为透明 -->
            <chart:Chart.PlotAreaStyle>
                <Style TargetType="Grid">
                    <Setter Property="Panel.Background" Value="Transparent"/>
                </Style>
            </chart:Chart.PlotAreaStyle>

(3)标题区设置

      <!--表格的标题 设置标题,默认为null-->
            <chart:Chart.Title >
                饼状图 Demo
            </chart:Chart.Title>

(4)坐标轴 区设置

 
    <!--表格的坐标轴 设置-->
            <chart:Chart.Axes>
                <chart:LinearAxis Orientation="X" Interval="1" Title="次数"></chart:LinearAxis>
                <chart:LinearAxis Orientation="Y" Interval="0.5" Title="高度"></chart:LinearAxis>
            </chart:Chart.Axes>

 

(5)设置色板Palette

Background背景属性可以是画刷或者纯色
 <!--饼状图的调色板 使用共同的Key表示一组色块-->
            <chart:Chart.Palette>
                <datavis:ResourceDictionaryCollection>

                    <ResourceDictionary>

                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <RadialGradientBrush MappingMode="Absolute">
                                        <GradientStop Color="LightGray"    Offset="0.8" />
                                        <GradientStop Color="DarkGray"  Offset="1.0" />
                                    </RadialGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" Value="Transparent" />

                        </Style>
                     
                    </ResourceDictionary>

                    <ResourceDictionary>
                        <!-- 也可以写成 <Style x:Key="DataPointStyle" TargetType="Control">-->
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <RadialGradientBrush MappingMode="Absolute">
                                        <GradientStop Color="Lime"    Offset="0.1" />
                                        <GradientStop Color="LimeGreen"  Offset="1.0" />
                                    </RadialGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" Value="Transparent" />

                        </Style>
                    </ResourceDictionary>
                    
                    <ResourceDictionary>
                        <!-- 也可以写成 <Style x:Key="DataPointStyle" TargetType="Control">-->
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#f15c22" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                    
                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#ffd503" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>

                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#93b9b1" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#a78462" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#00394b" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                </datavis:ResourceDictionaryCollection>
            </chart:Chart.Palette>

(6)自定义饼状图 中伞形的样式-PieSeriesDataPointStyle

    <!--其他代码-->
  <Window.Resources>
   <Style x:Key="PieSeriesDataPointStyle1" TargetType="{x:Type chart:PieDataPoint}">

            <Setter Property="Background"  Value="{Binding RelativeSource={RelativeSource Self }, Converter= {StaticResource PiePaletteConverter}}"/>
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="RatioStringFormat" Value="{}{0:p1}">
           
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type chart:PieDataPoint}">
                        <Grid x:Name="Root"  >
                            
                                
                            <Path x:Name="Slice" Data="{TemplateBinding Geometry}" Fill="{TemplateBinding Background}" StrokeMiterLimit="1" Stroke="{TemplateBinding BorderBrush}">
                               
                                <Path.ToolTip>
                                    <StackPanel>
                                            <ContentControl Content="{TemplateBinding FormattedDependentValue}"/>
                                            <ContentControl Content="{TemplateBinding FormattedRatio}"/>
                                        </StackPanel>
                                </Path.ToolTip>
                            </Path>
                            
                            
                            <Path x:Name="SelectionHighlight" Data="{TemplateBinding GeometrySelection}" Fill="Red" IsHitTestVisible="False" Opacity="0" StrokeMiterLimit="1"/>
                            <Path x:Name="MouseOverHighlight" Data="{TemplateBinding  GeometryHighlight}" Fill="White" IsHitTestVisible="False" Opacity="0" StrokeMiterLimit="1"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True" >
                           
                                <Setter TargetName="MouseOverHighlight" Property="Opacity" Value="0.5"/>
                                
                            </Trigger>

                            <Trigger SourceName="Slice" Property="IsFocused" Value="True" >

                                <Setter TargetName="Slice" Property="Fill" Value="Red"/>

                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                    
                </Setter.Value>
            </Setter>
        </Style>
      

        
    </Window.Resources>
    <!--其他代码-->


            <!--表格的序列-->
            <chart:PieSeries  
                 
                ItemsSource="{Binding}" 
                DataPointStyle="{StaticResource PieSeriesDataPointStyle1}"
                DependentValuePath="Value"
                IndependentValuePath="Key"
                Title="Pet Preference" 
                IsSelectionEnabled="True">
             
                
            </chart:PieSeries>

类型转化器

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Media;
using ColorConverter = System.Windows.Media.ColorConverter;

namespace testControl
{
    public class PiePaletteConverter : IValueConverter
    {
       //配色
        string[] colorsPal = new[] { "#318fa9", "#66c4cc", "#48a1e5", "#68bc4a", "#c4ccd3", "#47a0e4", "#61a0a8", "#d48265" };
        int index = 0;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            PieDataPoint pieDataPoint = (PieDataPoint)value;
            if(pieDataPoint.DependentValue is not null)
            {

                index = index <colorsPal.Length ?  index: 0;
             var colo=    new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString(colorsPal[index]));

                ++index;
                return colo;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

 

 

(7)表格的序列设置

 xaml

 <!--表格的序列-->
            <chart:PieSeries ItemsSource="{Binding}"
                    DependentValuePath="Value"
                  
                    IndependentValuePath="Key"
                    Title="Pet Preference" IsSelectionEnabled="True">

            </chart:PieSeries>

C#代码

 public MainWindow()
        {
            InitializeComponent();
          
            chart1.DataContext = new KeyValuePair<string, int>[] {

                                    new KeyValuePair<string, int>("Dog", 30),

                                    new KeyValuePair<string, int>("Cat", 25),

                                    new KeyValuePair<string, int>("Rat", 5),

                                    new KeyValuePair<string, int>("Hampster", 8),

                                    new KeyValuePair<string, int>("Rabbit", 12) };
        }

 

 效果

完整的xaml代码

<Window x:Class="testControl.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:testControl"
         xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
         
        xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=DotNetProjects.DataVisualization.Toolkit" xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="PieChartLegendItemStyle" TargetType="{x:Type chart:LegendItem}">
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type chart:LegendItem}">
                        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                            <StackPanel Orientation="Horizontal">
                                <Rectangle Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="0" Margin="0,0,3,0" />
                                <visualizationToolkit:Title Content="{TemplateBinding Content}" />
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="PieSeriesStyle1" TargetType="{x:Type chart:PieSeries}">
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type chart:PieSeries}">
                        <Canvas x:Name="PlotArea" Margin="0" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>

        <!--表格-->
        <chart:Chart  Height="271" Name="chart1" Width="379"  BorderThickness="0" >
            <!--表格的LegEnd 隐藏图例-->
            <chart:Chart.LegendStyle>
                <Style TargetType="visualizationToolkit:Legend">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Setter Property="Width" Value="0"/>
                    <Setter Property="Height" Value="0"/>
                </Style>
            </chart:Chart.LegendStyle>

            <!--表格的绘图区域  将绘图区背景色设置为透明 -->
            <chart:Chart.PlotAreaStyle>
                <Style TargetType="Grid">
                    <Setter Property="Panel.Background" Value="Transparent"/>
                </Style>
            </chart:Chart.PlotAreaStyle>



            <!--表格的标题 设置标题,默认为null-->
            <chart:Chart.Title >
                饼状图 Demo
            </chart:Chart.Title>

            <!--表格的序列-->
            <chart:PieSeries ItemsSource="{Binding}"
                    DependentValuePath="Value"
                  
                    IndependentValuePath="Key"
                    Title="Pet Preference" IsSelectionEnabled="True">

            </chart:PieSeries>

            <!--饼状图的调色板 使用共同的Key表示一组色块-->
            <chart:Chart.Palette>
                <datavis:ResourceDictionaryCollection>

                    <ResourceDictionary>

                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <RadialGradientBrush MappingMode="Absolute">
                                        <GradientStop Color="LightGray"    Offset="0.8" />
                                        <GradientStop Color="DarkGray"  Offset="1.0" />
                                    </RadialGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" Value="Transparent" />

                        </Style>
                     
                    </ResourceDictionary>

                    <ResourceDictionary>
                        <!-- 也可以写成 <Style x:Key="DataPointStyle" TargetType="Control">-->
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <RadialGradientBrush MappingMode="Absolute">
                                        <GradientStop Color="Lime"    Offset="0.1" />
                                        <GradientStop Color="LimeGreen"  Offset="1.0" />
                                    </RadialGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" Value="Transparent" />

                        </Style>
                    </ResourceDictionary>
                    
                    <ResourceDictionary>
                        <!-- 也可以写成 <Style x:Key="DataPointStyle" TargetType="Control">-->
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#f15c22" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                    
                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#ffd503" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>

                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#93b9b1" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#a78462" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                    <ResourceDictionary>
                        <Style x:Key="DataPointStyle" TargetType="{x:Type chart:PieDataPoint}">
                            <Setter Property="Background" Value="#00394b" />
                            <Setter Property="BorderBrush" Value="White" />

                        </Style>
                    </ResourceDictionary>
                </datavis:ResourceDictionaryCollection>
            </chart:Chart.Palette>
            
            <!--表格的坐标轴 设置-->
            <chart:Chart.Axes>
                <chart:LinearAxis Orientation="X" Interval="1" Title="次数"></chart:LinearAxis>
                <chart:LinearAxis Orientation="Y" Interval="0.5" Title="高度"></chart:LinearAxis>
            </chart:Chart.Axes>

        </chart:Chart>

    </Grid>
</Window>

 自定义 饼状图颜色

 

 

效果

 

MainWindow.xaml

<Window x:Class="testControl.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:testControl"
         xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
         
        xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=DotNetProjects.DataVisualization.Toolkit" xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
 >

    <Grid>

        <!--chart-->
        <chart:Chart  Height="271" Name="chart1" Width="379"  BorderThickness="0" >

            <!--PlotArea   -->
            <chart:Chart.PlotAreaStyle>
                <Style TargetType="Grid">
                    <Setter Property="Panel.Background" Value="Transparent"/>
                </Style>
            </chart:Chart.PlotAreaStyle>

            <!--Legend    -->
            <chart:Chart.LegendStyle>
                <Style TargetType="Control">
                    <Setter Property="Panel.Background" Value="Transparent"/>
                    <Setter Property="BorderThickness" Value="0"/>
                </Style>
            </chart:Chart.LegendStyle>

            <!--Title  -->
            <chart:Chart.Title >
                饼状图 Demo
            </chart:Chart.Title>

            <!--PieSeries  -->
            <chart:PieSeries     
                ItemsSource="{Binding}" 
                LegendItemStyle="{DynamicResource PieChartLegendItemStyle}"
                DataPointStyle="{StaticResource PieSeriesDataPointStyle1}"
                DependentValuePath="Value"
                IndependentValuePath="Key"
                Title="Pet Preference" 
                IsSelectionEnabled="True">
             
                
            </chart:PieSeries>


            <!--Axes  -->
            <chart:Chart.Axes>
                <chart:LinearAxis Orientation="X" Interval="1" Title="次数"></chart:LinearAxis>
                <chart:LinearAxis Orientation="Y" Interval="0.5" Title="高度"></chart:LinearAxis>
            </chart:Chart.Axes>

        </chart:Chart>

    </Grid>
</Window>

 

 

 

 MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace testControl
{
    public  class StylePalette : Collection<Style>
    {
        public StylePalette() { }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
          
            chart1.DataContext = new KeyValuePair<string, int>[] {

                                    new KeyValuePair<string, int>("Dog", 30),

                                    new KeyValuePair<string, int>("Cat", 25),

                                    new KeyValuePair<string, int>("Rat", 5),

                                    new KeyValuePair<string, int>("Hampster", 8),
                                    new KeyValuePair<string, int>("ssse", 50),

                                    new KeyValuePair<string, int>("Rabbit", 12) };
                           
    }
      
    }


}

 

 PieLegendItemPaletteConverter.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Media;
using ColorConverter = System.Windows.Media.ColorConverter;

namespace testControl
{
    public class PieLegendItemPaletteConverter : IValueConverter
    {
       //配色
        string[] colorsPal = new[] { "#16cf45", "#109fd9", "#e5d118", "#fe7c40", "#4a5873", "#5baae4", "#e87e01", "#00d893", "#70ac48", "#fcc02c" };
        int index = 0;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            LegendItem pieDataPoint = (LegendItem)value;
            if(pieDataPoint.Background is   null)
            {

                index = index <colorsPal.Length ?  index: 0;
             var colo=    new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString(colorsPal[index]));

                ++index;
                return colo;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

PiePaletteConverter.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Media;
using ColorConverter = System.Windows.Media.ColorConverter;

namespace testControl
{
    public class PiePaletteConverter : IValueConverter
    {
        //配色
        string[] colorsPal = new[] { "#16cf45", "#109fd9", "#e5d118", "#fe7c40", "#4a5873", "#5baae4", "#e87e01", "#00d893", "#70ac48", "#fcc02c" };
        int index = 0;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            PieDataPoint pieDataPoint = (PieDataPoint)value;
            if(pieDataPoint.DependentValue is not null)
            {

                index = index <colorsPal.Length ?  index: 0;
             var colo=    new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString(colorsPal[index]));

                ++index;
                return colo;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

PieChartStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
                    xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=DotNetProjects.DataVisualization.Toolkit" xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
                   
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:testControl">
    <local:PiePaletteConverter x:Key="PiePaletteConverter" />
    <local:PieLegendItemPaletteConverter x:Key="PieLegendItemPaletteConverter" />
    
    
    <!--Legend-->
    <Style x:Key="PieChartLegendItemStyle" TargetType="{x:Type chart:LegendItem}">
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Background"  Value="{Binding RelativeSource={RelativeSource Self }, Converter={StaticResource PieLegendItemPaletteConverter}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type chart:LegendItem}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="8" Height="8" Fill="{TemplateBinding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="0" Margin="0,0,3,0" />
                            <visualizationToolkit:Title Content="{TemplateBinding Content}" />
                     
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--PieSeriesDataPoint-->
    <Style x:Key="PieSeriesDataPointStyle1" TargetType="{x:Type chart:PieDataPoint}">
        <Setter Property="Background"  Value="{Binding RelativeSource={RelativeSource Self }, Converter={StaticResource PiePaletteConverter}}"/>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="RatioStringFormat" Value="{}{0:p1}">

        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type chart:PieDataPoint}">
                    <Grid x:Name="Root"  >
                        <Path x:Name="Slice" Data="{TemplateBinding Geometry}" Fill="{TemplateBinding Background}" StrokeMiterLimit="1" Stroke="{TemplateBinding BorderBrush}">
                            <!--ToolTip-->
                            <Path.ToolTip>
                                <StackPanel>
                                    <!--FormattedDependentValue:值  FormattedRatio:百分比   IndependentValue:关键字 -->
                                    <ContentControl Content="{TemplateBinding FormattedDependentValue}"/>
                                    <ContentControl Content="{TemplateBinding FormattedRatio}"/>
                                    <ContentControl Content="{TemplateBinding IndependentValue}"/>
                                </StackPanel>
                            </Path.ToolTip>
                        </Path>
                        <Path x:Name="SelectionHighlight" Data="{TemplateBinding GeometrySelection}" Fill="Red" IsHitTestVisible="False" Opacity="0" StrokeMiterLimit="1"/>
                        <Path x:Name="MouseOverHighlight" Data="{TemplateBinding  GeometryHighlight}" Fill="White" IsHitTestVisible="False" Opacity="0" StrokeMiterLimit="1"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True" >
                            <Setter TargetName="MouseOverHighlight" Property="Opacity" Value="0.5"/>
                        </Trigger>
                        <Trigger SourceName="Slice" Property="IsFocused" Value="True" >
                            <Setter TargetName="Slice" Property="Fill" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>


</ResourceDictionary>

App.xaml

<Application x:Class="testControl.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:testControl"
               xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=DotNetProjects.DataVisualization.Toolkit" xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
      
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Theme/PieChartStyle.xaml" />
            
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
       
    </Application.Resources>
</Application>

 

 

 

 

 

posted @ 2022-12-10 14:18  小林野夫  阅读(127)  评论(0)    收藏  举报
原文链接:https://www.cnblogs.com/cdaniu/