如何在饼图中设置图例中的不同标签

时间:2016-11-04 06:21:28

标签: c# winforms pie-chart mschart

我目前正在做一个里面有Pie Chart的窗体。我需要显示饼的百分比。

但现在我遇到了一个问题:当我将此#PERCENT{P2}添加到系列时,Pie Chart会显示如下:

enter image description here

但如果删除它,Pie Chart将会显示为

enter image description here

是否有可能使Pie Chart像这样?enter image description here

我的代码:

DataTable dt = new DataTable();
        dt.Columns.Add("completed", typeof(string));
        dt.Columns.Add("no", typeof(int));
        int noin = allitem - intime;
        dt.Rows.Add("Complete In Time", intime);
        dt.Rows.Add("OverDue", noin);

        chart1.DataSource = dt;

        chart1.DataBind();

1 个答案:

答案 0 :(得分:3)

是的,通过为LegendText设置每个DataPoints,可以轻松完成此操作:

foreach (DataPoint dp in yourSeries.Points) dp.LegendText = yourLabel;

如果您的x值包含有意义的数字,您可以使用它们:

foreach (DataPoint dp in s.Points) dp.LegendText = dp.XValue + "";

但如果您将x值添加为字符串,则丢失,您必须使用LegendTexts的其他来源。

如果您只有DataPoints的固定数量,则可以直接设置LegendTexts

yourSeries.Points[0].LegendText = "hi";
yourSeries.Points[1].LegendText = "ho";
yourSeries.Points[2].LegendText = "hum";

请注意,Legend通常会显示每个Series一个条目。但对于Pie图表,它会显示每个DataPoint的一个条目!

相关问题