我可以在MSChart的饼图标签上显示标签和值

时间:2011-11-29 14:55:35

标签: c# asp.net mschart data-visualization

我有一个来自Microsoft图表控件的Donut类型的图表(在命名空间System.Web.UI.DataVisualization.Charting中)。

我绑定到图表系列的数据是Dictionary<string, int>类型,其中标签是字典的键,值是Value属性。我希望饼图上的标签看起来像这样:Some label (4),其中“Some label”是字典中条目的键,4是值。

我尝试了IsValueShownAsLabelLabelFormat的各种组合,但这没有用,因为它只显示了值。

我已经提出了以下代码,它可以满足我的需求但是,坦率地说,最后三行(设置标签)让我感到害怕!

Dictionary<string, int> myDictionary = GetMyDataForTheChart();
var mySeries = Chart1.Series.Add("MySeries");
mySeries.Points.DataBind(myDictionary, "Key", "Value", null);
int i = 0;
foreach (var p in mySeries.Points)
    p.Label = myDictionary.ElementAt(i++).Key + " (" + p.YValues[0].ToString("0") + ")";

有更好的方法吗?

1 个答案:

答案 0 :(得分:5)

要显示x和y值,您可以设置Label=#VALX #VALY,具体取决于绑定的值的数量,您也可以选择将其扩展为#VALY1 #VALY2等等