Wpf 图表工具包:图表如何显示每个饼图的值

Wpf 图表工具包:图表如何显示每个饼图的值,wpf,data-visualization,pie-chart,Wpf,Data Visualization,Pie Chart,我正在使用System.Windows.Controls.DataVisualization.Charting绘制图表。 但我不知道如何在图表上显示每个饼图切片的百分比/值。 这可能吗?向System.Windows.Controls.DataVisualization.ChartingPieChart添加百分比并不是那么直接,因为没有用于管理标签的属性 无论如何,有一些方法可以达到目标。我写了一封信来描述我使用的那封信 第一步是创建自定义PieDataPoint类: public class

我正在使用System.Windows.Controls.DataVisualization.Charting绘制图表。 但我不知道如何在图表上显示每个饼图切片的百分比/值。
这可能吗?

向System.Windows.Controls.DataVisualization.Charting
PieChart
添加百分比并不是那么直接,因为没有用于管理标签的属性

无论如何,有一些方法可以达到目标。我写了一封信来描述我使用的那封信

第一步是创建自定义
PieDataPoint
类:

public class PieDataPoint : System.Windows.Controls.DataVisualization.Charting.PieDataPoint
{
    public static readonly DependencyProperty TextedGeometryProperty =
        DependencyProperty.Register("TextedGeometry", typeof(Geometry), typeof(PieDataPoint));

    public Geometry TextedGeometry
    {
        get { return (Geometry)GetValue(TextedGeometryProperty); }
        set { SetValue(TextedGeometryProperty, value); }
    }

    static PieDataPoint()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(PieDataPoint),
            new FrameworkPropertyMetadata(typeof(PieDataPoint)));
    }

    public PieDataPoint()
    {
        DependencyPropertyDescriptor dependencyPropertyDescriptor
            = DependencyPropertyDescriptor.FromProperty(GeometryProperty, GetType());

        dependencyPropertyDescriptor.AddValueChanged(this, OnGeometryValueChanged);
    }

    private double LabelFontSize
    {
        get
        {
            FrameworkElement parentFrameworkElement = Parent as FrameworkElement;
            return Math.Max(8, Math.Min(parentFrameworkElement.ActualWidth,
                parentFrameworkElement.ActualHeight) / 30);
        }
    }

    private void OnGeometryValueChanged(object sender, EventArgs arg)
    {
        Point point;
        FormattedText formattedText;

        CombinedGeometry combinedGeometry = new CombinedGeometry();
        combinedGeometry.GeometryCombineMode = GeometryCombineMode.Exclude;

        formattedText = new FormattedText(FormattedRatio, 
            CultureInfo.CurrentCulture,
            FlowDirection.LeftToRight,
            new Typeface("Arial"), 
            LabelFontSize, 
            Brushes.White);

        if (ActualRatio == 1)
        {
            EllipseGeometry ellipseGeometry = Geometry as EllipseGeometry;

            point = new Point(ellipseGeometry.Center.X - formattedText.Width / 2,
                ellipseGeometry.Center.Y - formattedText.Height / 2);
        }
        else if (ActualRatio == 0)
        {
            TextedGeometry = null;
            return;
        }
        else
        {
            Point tangent;
            Point half;
            Point origin;

            PathGeometry pathGeometry = Geometry as PathGeometry;
            pathGeometry.GetPointAtFractionLength(.5, out half, out tangent);
            pathGeometry.GetPointAtFractionLength(0, out origin, out tangent);

            point = new Point(origin.X + ((half.X - origin.X) / 2) - formattedText.Width / 2,
                origin.Y + ((half.Y - origin.Y) / 2) - formattedText.Height / 2);

        }

        combinedGeometry.Geometry1 = Geometry;
        combinedGeometry.Geometry2 =  formattedText.BuildGeometry(point);

        TextedGeometry = combinedGeometry;
    }
}
如您所见,它将
格式化文本
几何图形(带有百分比)添加到原始
几何图形
。然后,需要创建默认样式(在generic.xaml字典中)以使用新的几何体属性(名为
TextedGeometry

样式必须至少包含以下内容:

<Path Name="Slice" Data="{TemplateBinding local:PieDataPoint.TextedGeometry}" 
            Fill="{TemplateBinding Control.Background}" 
            Stroke="{TemplateBinding Control.BorderBrush}" 
            StrokeMiterLimit="1">
    <ToolTipService.ToolTip>
        <StackPanel>
            <ContentControl Content="{TemplateBinding chartingToolkit:DataPoint.FormattedDependentValue}" />
            <ContentControl Content="{TemplateBinding chartingToolkit:PieDataPoint.FormattedRatio}" />
        </StackPanel>
    </ToolTipService.ToolTip>
</Path>
因此,在XAML中,您可以使用:

<chartingToolkit:Chart Name="pieChart" Title="Pie Series Demo">
    <local:PieSeries DependentValuePath="Value" IndependentValuePath="Key"
                        ItemsSource="{Binding}" IsSelectionEnabled="True" />

</chartingToolkit:Chart>


其中
local
指的是您的自定义命名空间。我希望它能对您有所帮助。

谢谢您的详细回答,先生。@NguyenMinhDat不客气,但如果您认为我的回答适合您的问题,请将其标记为正确答案
<chartingToolkit:Chart Name="pieChart" Title="Pie Series Demo">
    <local:PieSeries DependentValuePath="Value" IndependentValuePath="Key"
                        ItemsSource="{Binding}" IsSelectionEnabled="True" />

</chartingToolkit:Chart>