C# 如何在Winform中在面板中显示Piechart
我正在winform中创建Piechart。我的图表运行良好。我唯一想添加的是在面板中显示Piechart,但我无法完成此操作 这是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 }; /
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);
}