2014-09-23 80 views
0

我在winform中创建Piechart.My图表即将上线。我唯一想要添加的是在Panel中显示Piechart,但我无法做到这一点。如何在Winform的面板中显示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); 
     } 
    } 

请帮助我.. 在此先感谢..

回答

0

您需要了解WinForms Graphics的基础知识。

把你的饼到Panel代替Form,因为你现在拥有它,你只需要改变this.CreateGraphics()panel.CreateGraphics()但这不行!当你最小化你的表格时,你的派消失了.. ..?

所以,所有图纸必须发生/从一个Paint事件触发,利用其e.Graphics对象!只有这样,绘画才能坚持各种各样的外部事件。

你可以存储在类级别的数据,并调用DrawPieChartPaint事件Panel的,移交在e.Graphics代替myPieGraphic ..使用panel.Invalidate()触发Paint事件时,你已经改变了价值观..:

//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); 

}