Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Winform中在面板中显示Piechart_C#_Winforms_Charts_Panel_Pie Chart - Fatal编程技术网

C# 如何在Winform中在面板中显示Piechart

C# 如何在Winform中在面板中显示Piechart,c#,winforms,charts,panel,pie-chart,C#,Winforms,Charts,Panel,Pie Chart,我正在winform中创建Piechart。我的图表运行良好。我唯一想添加的是在面板中显示Piechart,但我无法完成此操作 这是Piechart的代码 public void DrawPieChartOnForm() { //Take Total Five Values & Draw Chart Of These Values. int[] myPiePercent = { 10, 20, 25, 5, 40 }; /

我正在winform中创建Piechart。我的图表运行良好。我唯一想添加的是在面板中显示Piechart,但我无法完成此操作

这是Piechart的代码

    public void DrawPieChartOnForm()
    {
        //Take Total Five Values & Draw Chart Of These Values.
        int[] myPiePercent = { 10, 20, 25, 5, 40 };

        //Take Colors To Display Pie In That Colors Of Taken Five Values.
        Color[] myPieColors = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Maroon };

        using (Graphics myPieGraphic = this.CreateGraphics())
        {
            //Give Location Which Will Display Chart At That Location.
            Point myPieLocation = new Point(10, 400);

            //Set Here Size Of The Chart…
            Size myPieSize = new Size(500, 500);

            //Call Function Which Will Draw Pie of Values.
            DrawPieChart(myPiePercent, myPieColors, myPieGraphic, myPieLocation, myPieSize);
        }
    }
请帮帮我。。
提前感谢。

看看Hiren Khirsaria的这篇文章:


我认为您需要做的是创建一个控件来处理piechart,并将其添加到面板上。 比如:

panel.Controls.Add(pieChart);

您需要了解WinForms图形的基础知识

要将饼图放在
面板上,而不是现在的
表单上,只需将
this.CreateGraphics()
更改为
Panel.CreateGraphics()
但那不好最小化表单时会显示饼图,对吗

因此,all绘图必须发生/从
Paint
事件触发,使用其
e.Graphics
对象! 只有这样,绘图才能持久保存各种外部事件

您可以在类级别存储数据,并在
面板
绘制
事件中调用
绘图图表
,将
e.Graphics
而不是
myPieGraphic
。。使用
panel.Invalidate()
在更改值时触发绘制事件..:

//Five Values at class level
int[] myPiePercent = { 10, 20, 25, 5, 40 };

//Take Colors To Display Pie In That Colors Of Taken Five Values.
Color[] myPieColors = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Maroon };


public void DrawPieChart()
{
    // maybe change the values here..
    myPiePercent = { 11, 22, 23, 15, 29 };
    // then let Paint call the draw routine:
    panel1.Invalidate();

}

private void panel1_Paint(object sender, PaintEventArgs e)
{

   //Give Location Which Will Display Chart At That Location.
   Point myPieLocation = new Point(10, 400);

   //Set Size Of The Chart
   Size myPieSize = new Size(500, 500);

   //Call Function Which Will Draw Pie of Values.
   DrawPieChart(myPiePercent, myPieColors, e.Graphics, myPieLocation, myPieSize);

}