0

I'm new to C# but I've been practicing with making charts and graphs. I made a pie chart for a prior program and I pretty much just copied and pasted the format into my new program, however it doesn't work. Here's the function that sets up the Pie Chart. I probably did something stupid and haven't realized it. lol

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

1 回答 1

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下载可执行文件和源代码。

于 2013-10-10T22:00:07.350 回答