2014-09-23 55 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); 

}