2012-07-28 24 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用于与源代码一起下载的可执行文件。

相关问题