在C#中创建一个饼图 - 不是出于某种奇怪的原因而工作

时间:2012-07-28 23:08:21

标签: c# graph charts pie-chart

我是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());
            }
        }
    }

1 个答案:

答案 0 :(得分: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