2012-07-28 10 views
0

我是C#的新手,但我一直在練習製作圖表和圖形。我爲之前的程序製作了一張餅圖,而且我幾乎只是將該格式複製並粘貼到我的新程序中,但它不起作用。這是設置餅圖的功能。我可能做了一些愚蠢的事情,並沒有意識到這一點。洛爾在C#中創建餅圖 - 由於某種奇怪的原因而無法工作

public void setup_pieChart() 
    { 
     float total_count = 0; 
     for (int k = 0; k < referrals.Count(); k++) 
     { 
      total_count += referrals[k].count; 
     } 
     if (total_count > 0) 
     { 
      Array.Sort(referrals); 
      // ----------------------- CREATE PIE CHART ---------------------// 

      Graphics g = this.CreateGraphics(); 
      Pen pen = new Pen(Color.Black, 2); 
      Rectangle rec = new Rectangle(referralBox.Location.X + referralBox.Size.Width + 10, 25, 200, 200); 

      g.Clear(Color.White); 
      float degreeSum = 0; 

      for (int k = 0; k < referrals.Count(); k++) 
      { 
       referrals[k].degrees = (referrals[k].count/total_count) * 360; 
       g.DrawPie(pen, rec, degreeSum, referrals[k].degrees); 
       g.FillPie(new SolidBrush(referrals[k].color), rec, degreeSum, referrals[k].degrees); 
       degreeSum += referrals[k].degrees; 
       Console.WriteLine("count " + referrals[k].count); 
       Console.WriteLine("degree " + referrals[k].degrees); 
       Console.WriteLine("color " + referrals[k].color.ToString()); 
      } 
     } 
    } 
+0

我想通了。我必須觸發事件才能顯示圖形 – 2012-07-28 23:26:49

+0

需要丟棄筆,畫筆和圖形對象。 – Brannon 2012-07-29 04:21:01

+0

_Graphics g = this.CreateGraphics(); _ Dead at arrival。永遠不要使用contro.CreateGraphics來保存圖形。 – TaW 2016-08-27 08:55:56

回答

0

下面是示例程序在c#創建餅圖

// PieChartForm.Designer.cs 
namespace CSharpPieChart 
{ 
    partial class PieChartForm 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 
      // 
      // PieChartForm 
      // 
      this.ClientSize = new System.Drawing.Size(323, 273); 
      this.Name = "PieChartForm"; 
      this.Text = "C# Pie Chart - softwareandfinance.com"; 
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); 
      this.ResumeLayout(false); 

     } 

     #endregion 
    } 
} 


// MainProgram.cs 

using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 

namespace CSharpPieChart 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new PieChartForm()); 
     } 
    } 
} 

參見http://www.softwareandfinance.com/CSharp/Pie_Chart.html用於與源代碼一起下載的可執行文件。

相關問題