winforms 如何使用MSChart制作饼图?

6xfqseft  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(112)

我已经下载了MS图表示例,在图库中它显示了一个“3DPieInPie.png”,这是我想要的-但看不到任何代码来创建它!!

假设它是ChartType = SeriesChartType.Pie...但是否有其他设置指定我要第二个饼图?以及如何输入第二组数据?-我尝试添加新系列,但似乎被忽略。

2uluyalo

2uluyalo1#

这种类型的图表称为嵌套饼图或多级饼图。它是通过创建两个ChartArea -饼图和圆环图来完成的,这两个图表相对于另一个正确放置。下面是代码。

public partial class Form1 : Form
{
    public class DataInt {
        public string Label { get; set; }
        public int Value { get; set; } }
    public Form1()
    {
        InitializeComponent();
        pieChart.Series.Clear();
        pieChart.Legends.Clear();
        float baseDoughnutWidth = 25;
        float outerSize = 80;
         
        ChartArea outerArea = new ChartArea("OUTER_AREA");
        outerArea.Position = new ElementPosition(0, 10, 100, 100);
        outerArea.InnerPlotPosition = new ElementPosition((100 - outerSize) / 2, (100 - outerSize) / 2, outerSize, outerSize);
        outerArea.BackColor = Color.Transparent;
         
        pieChart.ChartAreas.Add(outerArea);
         
        float innerSize = 53;
       
        ChartArea innerArea = new ChartArea("INNER_AREA");
        innerArea.Position = new ElementPosition(0, 5, 100, 100);
        ElementPosition innerPos = new ElementPosition((100 - innerSize) / 2, ((100 - innerSize) / 2), innerSize, innerSize+5);
        innerArea.InnerPlotPosition = innerPos;
        innerArea.BackColor = Color.Transparent;
        pieChart.ChartAreas.Add(innerArea);
       
        Series outerSeries = new Series("OUTER_SERIES");
        outerSeries.ChartArea = "OUTER_AREA";
        outerSeries.ChartType = SeriesChartType.Doughnut;            
        outerSeries["DoughnutRadius"] = Math.Min(baseDoughnutWidth * (100 / outerSize), 99).ToString().Replace(",", ".");
                    
        Series innerSeries = new Series("INNER_SERIES");
        innerSeries.ChartArea = "INNER_AREA";
        innerSeries.ChartType = SeriesChartType.Pie;

        var innerData = new List<DataInt> { new DataInt { Label = "A", Value = 7 }, 
           new DataInt { Label = "B", Value = 14 }, new DataInt{Label="C",Value=19},
           new DataInt{Label="D",Value=12}, new DataInt{Label="E",Value=20},
             new DataInt{Label="F",Value=28} };

        var outerData = new List<DataInt> { new DataInt { Label = "Gold", Value = 21 },
           new DataInt{Label="Red",Value=31}, new DataInt { Label = "Blue", Value = 48 } };

        innerSeries.Points.DataBindXY(innerData, "Label", innerData, "Value");
        outerSeries.Points.DataBindXY(outerData, "Label", outerData, "Value");

        pieChart.Series.Add(innerSeries);
        pieChart.Series.Add(outerSeries);

        Legend legend = new Legend("pieChartLegend")
        {
            Font=new System.Drawing.Font("Arial",14.0f),
            Alignment = StringAlignment.Center,
            Docking = Docking.Top,
            Enabled = true,
            IsDockedInsideChartArea = false,
            TableStyle = LegendTableStyle.Wide,
        };
     
        pieChart.Legends.Add(legend);
        foreach (Series sr in pieChart.Series)
        {
            sr.Legend = "pieChartLegend";
        }
        var outerAreaColors = new List<Color>(){Color.Gold,Color.Red, Color.DeepSkyBlue };
        var innerAreaColors = new List<Color>() {Color.Goldenrod,Color.DarkGoldenrod,  Color.DarkRed, Color.Firebrick,Color.DodgerBlue,  Color.SteelBlue };
        for (int i = 0; i < outerSeries.Points.Count; i++)
        {
            outerSeries.Points[i].Color = outerAreaColors[i];
        }
        for (int i = 0; i < innerSeries.Points.Count; i++)
        {
            innerSeries.Points[i].Color = ChartColorPallets.Inner[i];
        }

        int inclination = 20;
        int rotation = 45;
        bool enable3d = true;

        pieChart.ChartAreas["OUTER_AREA"].Area3DStyle.Enable3D = enable3d;
        pieChart.ChartAreas["OUTER_AREA"].Area3DStyle.Inclination = inclination;
        pieChart.ChartAreas["OUTER_AREA"].Area3DStyle.Rotation = rotation;

        pieChart.ChartAreas["INNER_AREA"].Area3DStyle.Enable3D = enable3d;
        pieChart.ChartAreas["INNER_AREA"].Area3DStyle.Inclination = inclination;
        pieChart.ChartAreas["INNER_AREA"].Area3DStyle.Rotation = rotation;            
    }
}

相关问题