C# 如何将图表类型设置为饼图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14181902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:58:46  来源:igfitidea点击:

How to set chart type to pie

c#mschartpie-chart

提问by a1204773

When I do it without putting chart type is working fine but when I set it to pie its not working correct. It put all series name as Point 1 the pie is only 1 blue piece (one circle) and it show only first point (Value).

当我在没有放置图表类型的情况下执行此操作时,它可以正常工作,但是当我将其设置为饼图时,它无法正常工作。它将所有系列名称作为点 1,饼图只有 1 个蓝色块(一个圆圈),它只显示第一个点(值)。

foreach (var tag in tags)
{
    HtmlNode tagname = tag.SelectSingleNode("a");
    HtmlNode tagcount = tag.SelectSingleNode("span/span");
    chart1.Series.Add(tagname.InnerText);
    chart1.Series[x].Points.AddY(int.Parse(tagcount.InnerText));
    chart1.Series[x].IsValueShownAsLabel = true;
    chart1.Series[x].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
    x++;
}

采纳答案by zeFrenchy

You are adding multiple Series, each with one Point. As a result the charting control only displays the first Series. I believe what you are wanting to do is adding multiple points to a single Series.

您正在添加多个Series,每个都有一个Point。因此,图表控件仅显示第一个Series. 我相信您想要做的是将多个点添加到单个Series.

I'm not sure I understand what you are trying to do with the HtmlNodebut the code below demonstrate how to build a simple pie chart from a Dictionaryusing a tag name as Key and an integer as Value.

我不确定我是否理解您要尝试使用的内容,HtmlNode但下面的代码演示了如何Dictionary使用标签名称作为键和整数作为值来构建一个简单的饼图。

        Dictionary<string, int> tags = new Dictionary<string,int>() { 
            { "test", 10 },
            { "my", 3 },
            { "code", 8 }
        };

        chart1.Series[0].Points.Clear();
        chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
        foreach (string tagname in tags.Keys)
        {
            chart1.Series[0].Points.AddXY(tagname, tags[tagname]);
            //chart1.Series[0].IsValueShownAsLabel = true;
        }