WinFrom饼形图

Posted chaonuanxi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WinFrom饼形图相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Drawing.Drawing2D;
namespace MyFillPie
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection conn;     //声明一个connection变量

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //连接SQLserver数据库
            conn = new SqlConnection("server=.;database=AssetSys;uid=sa;pwd=sa");
            conn.Open();
            string str2 = string.Format("select SUM(tp) from tb_vote");
            SqlCommand cmd=new SqlCommand (str2,conn);
            int sum = Convert.ToInt32(cmd.ExecuteScalar());
            SqlDataAdapter sda = new SqlDataAdapter("select * from tb_vote",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);

            int man20and25 = Convert.ToInt32(ds.Tables[0].Rows[0][2].ToString());
            int man26and30 = Convert.ToInt32(ds.Tables[0].Rows[1][2].ToString());
            int man31and40 = Convert.ToInt32(ds.Tables[0].Rows[2][2].ToString());
            int width = 400, height = 450;
            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);

            try
            {
                 g.Clear(Color.White); //使用Clear方法使画布变成白色
                 Pen pen = new Pen(Color.Red);
                //创建4个Brush对象,用于填充颜色
                Brush brush1 = new SolidBrush(Color.PowderBlue);
                Brush brush2 = new SolidBrush(Color.Blue);
                Brush brush3 = new SolidBrush(Color.Wheat);
                Brush brush4 = new SolidBrush(Color.Orange);
                Font f1 = new Font("Courier New", 16, FontStyle.Bold);
                Font f2 = new Font("Courier New", 8);

                g.FillRectangle(brush1, 0, 0, width, height);   //绘制背景图
                g.DrawString("点赞比例", f1, brush2, new Point(80, 20)); //绘制标题

                int piex = 100, piey = 60, piew = 200, pieh = 200;

                float age1 = Convert.ToSingle((360/Convert.ToSingle(sum))*man20and25);
                float age2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * man26and30);
                float age3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * man31and40);
                g.FillPie(brush2,piex,piey,piew,pieh,0,age1);
                g.FillPie(brush3, piex, piey, piew, pieh, age1,age2);
                g.FillPie(brush4, piex, piey, piew, pieh, age1+age2,age3);

                g.DrawRectangle(pen,50,300,310,130);
                g.FillRectangle(brush2,90,320,20,10);
                g.DrawString("点赞比例1:" + Convert.ToSingle(man20and25) * 100 / sum + "%", f2, brush2, 120, 320);
                g.FillRectangle(brush3, 90, 360, 20, 10);
                g.DrawString("点赞比例2:" + Convert.ToSingle(man26and30) * 100 / sum + "%", f2, brush2, 120, 360);
                g.FillRectangle(brush4, 90, 400, 20, 10);
                g.DrawString("点赞比例3:" + Convert.ToSingle(man31and40) * 100 / sum + "%", f2, brush2, 120, 400);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            pictureBox1.Image = bitmap;
        }
    }
}

T-SQL:

USE [AssetSys]
GO
 
/****** Object:  Table [dbo].[tb_vote]    Script Date: 2018/10/27 9:12:00 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
CREATE TABLE [dbo].[tb_vote](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [TP] [int] NOT NULL,
    [CreateTime] [datetime] NOT NULL,
    [Remake] [nvarchar](50) NOT NULL,
    [State] [int] NOT NULL,
    [Sort] [int] NOT NULL,
 CONSTRAINT [PK_tb_vote] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
 
ALTER TABLE [dbo].[tb_vote] ADD  CONSTRAINT [DF_tb_vote_CreateTime]  DEFAULT (getdate()) FOR [CreateTime]
GO

  技术分享图片

以上是关于WinFrom饼形图的主要内容,如果未能解决你的问题,请参考以下文章

CorelDraw怎么制作一个饼形图轮廓呢?

echarts 有关饼形图和map的使用技巧1(饼图)

利用TCHART做分离饼形图

Seaborn catplot(kind ='count')将条形图更改为饼形图

饼状图表标题怎么修改

JFreeChart