首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中创建饼图-由于某种奇怪的原因无法工作

在C#中创建饼图-由于某种奇怪的原因无法工作
EN

Stack Overflow用户
提问于 2012-07-29 07:08:21
回答 1查看 783关注 0票数 0

我刚接触C#,但我一直在练习制作图表和图形。我为以前的程序做了一个饼状图,我几乎只是将格式复制并粘贴到我的新程序中,但是它不起作用。下面是设置饼图的函数。我可能做了一些愚蠢的事情而没有意识到这一点。LOL

代码语言:javascript
复制
 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());
            }
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2013-10-11 06:00:07

下面是在c#中创建饼图的示例程序

代码语言:javascript
复制
// 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下载可执行文件和源代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11705379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档