我使用System.Windows.Controls.DataVisualization.Charting绘制PieChart。 但我不知道如何显示图表上每个饼图的百分比/价值。 这可能吗?chartingToolkit:图表如何显示每个饼图的值
0
A
回答
0
将百分比添加到System.Windows.Controls.DataVisualization.Charting PieChart
不是那么直接,因为没有用于管理标签的属性。
无论如何有一些方法可以达到目标。我写了一个article on my blog来描述我使用的那个。
的第一步是创建一个自定义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;
}
}
正如你可以看到它增加了一个FormattedText
几何(用百分比)为原始Geometry
。然后,您需要为使用新几何属性(名为TextedGeometry
)创建默认样式(在generic.xaml字典中)。
样式必须包含 - 至少 - 这样的事情:
<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>
正如你所看到的“切片”路径都有其Data
属性绑定到TextedGeometry
。
现在有了自定义PieSeries
我们可以强制Chart
控制使用我们PieDataPoint
:
public class PieSeries : System.Windows.Controls.DataVisualization.Charting.PieSeries
{
protected override DataPoint CreateDataPoint()
{
return new PieDataPoint();
}
}
所以在你的XAML可以使用:
<chartingToolkit:Chart Name="pieChart" Title="Pie Series Demo">
<local:PieSeries DependentValuePath="Value" IndependentValuePath="Key"
ItemsSource="{Binding}" IsSelectionEnabled="True" />
</chartingToolkit:Chart>
凡local
指的是你的自定义命名空间。我希望它能帮助你。
相关问题
- 1. Silverlight制图 - 饼图在图表上显示一个相关值
- 2. 饼图中不显示饼图(Android)
- 3. 如何显示饼图时,我们只有一个值
- 4. 如何在饼图中显示两个值
- 5. Highcharts datalabels未显示在饼图的每个切片的盈
- 6. 在饼图上显示百分比值
- 7. 如何在android中显示饼图
- 8. 如何在饼图上显示标签
- 9. 如何使用高图显示多个饼图
- 10. 如何将matplotlib饼图中显示的值整圆?
- 11. Android:如何显示每分钟值的图表
- 12. 如何在饼图外显示高位图工具提示?
- 13. 饼图显示成员
- 14. 未显示饼图WPF
- 15. Bootstrap饼图不显示
- 16. MPAndroid不显示饼图
- 17. Angular-nvD3饼图不显示
- 18. Primeng饼图不显示
- 19. 在QGIS我想不是每个国家显示每个区域一个饼图
- 20. 谷歌图表饼图:如何在0开始两个值?
- 21. 如何显示条形图中每个条的条值?
- 22. 如何在饼图中显示百分比和值obiee11g
- 23. 如何在饼图扇区中显示值
- 24. RDLC饼图,用于显示数据库值的图例
- 25. 如何在SpreadsheetGear的不同位置显示多个饼图
- 26. 如何显示饼图上的多个json_encode
- 27. 如何实现谷歌饼图(带有图例显示和相应的值)
- 28. 2个值的XRChart饼图
- 29. 饼图没有显示任何
- 30. 图表js饼图未显示使用JSON的动态数据
感谢您的非常详细的答案,先生。 –
@NguyenMinhDat欢迎您,但如果您认为我的回答适合您的问题,请将其标记为正确答案 –