【问题标题】:How to set different Label in Legend in Pie Chart如何在饼图中的图例中设置不同的标签
【发布时间】:2023-03-24 04:51:02
【问题描述】:

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

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

但如果我删除它,Pie Chart 将显示如下

有没有可能让Pie Chart像这样?

我的代码:

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();

【问题讨论】:

    标签: c# winforms pie-chart mschart


    【解决方案1】:

    是的,这很容易通过为每个DataPoints 设置LegendText 来完成,如下所示:

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

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

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

    但是,如果您将 x 值添加为 strings,它们将 丢失,您必须为 LegendTexts 使用另一个来源。

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

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

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

    【讨论】:

    • 我现在才知道那个item是call legend text,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多