【问题标题】:How to display name, value and percentage in the legend? [duplicate]如何在图例中显示名称、值和百分比? [复制]
【发布时间】:2018-07-31 14:17:00
【问题描述】:

我的图表由以下内容填充:

    private class Data
    {
        public List<Row> Rows{ get; set; }
    }

    private class Row
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }

我绘制了一个饼图如下:

    chart.Series.Clear();

    var series1 = new Series {
        IsVisibleInLegend = true,
        IsXValueIndexed = true,
        ChartType = SeriesChartType.Pie
    };
    chart.Series.Add(series1);

    foreach (var row in data.Rows)
    {
        series1.Points.Add(new DataPoint(row.Count, row.Count));
    };

在图例中,我想将名称、计数和计数显示为百分比。这可能吗?如果可以,怎么做?

【问题讨论】:

  • 这完全可以使用图表上的Legends 属性来实现。

标签: c# winforms mschart


【解决方案1】:

是的,这是完全可能的;随意查看 documentation 关于图表控件上的图例(我假设您使用的是 System.Windows.Forms.DataVisualization.Charting.Chart)。

图例使人们能够区分图表图片中的系列和数据点。它们作为 Legend 对象存储在 Chart.Legends 集合属性中。

Legends 属性在您的属性窗格中可用,在向图表添加系列时,您可以指定与其关联的图例文本。上面提供的文档将演示如何在运行时以编程方式添加图例。

以防万一链接失效,代码如下:

// Create a new legend called "Legend2".
chart1.Legends.Add(new Legend("Legend2"));

// Set Docking of the Legend chart to the Default Chart Area.
chart1.Legends["Legend2"].DockToChartArea = "Default"; 

// Assign the legend to Series1.
chart1.Series["Series1"].Legend = "Legend2";
chart1.Series["Series1"].IsVisibleInLegend = true;

要将单个系列中的值作为单独的标签添加到图例中,您必须发挥创意并从本质上创建两个空的附加系列。然后只需将其LegendText 属性设置为所需的值。

chart1.Series["Series1"].LegendText = "Name";
chart1.Series["EmptySeries1"].LegendText = "99";
chart1.Series["EmptySeries2"].LegendText = "30%";

就向图例添加标签的直接方法而言,我认为这是不可能的。我相信图例是基于 Series 属性及其相关的图例属性构建的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多