C#绘制饼状图和柱状图的方法

本文实例为大家分享了C#绘制饼状图和柱状图的具体代码,供大家参考,具体内容如下

#代码如下:

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

namespace TEST3._2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BarMap();
        }
        private void BitMap()//饼状图
        {

            int[] saleNum = { 300, 500, 400 };
            int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0;
            for (int i = 0; i < saleNum.Length; i++)
            {
                sum += saleNum[i];
                if (i == 0)
                    threeNum = saleNum[0];
                else if (i == 1)
                    fourNum = saleNum[1];
                else
                    fiveNum = saleNum[2];
            }

            int height = pictureBox1.Height, width = pictureBox1.Width;
            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            Pen pen1 = new Pen(Color.Red);
            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 font1 = new Font("Couriter New", 16, FontStyle.Bold);
            Font font2 = new Font("Couriter New", 10);
            g.FillRectangle(brush1, 0, 0, width, height);
            g.DrawString("每月销售占比饼状图", font1, brush2, new Point(70, 20));
            int piex = 100, piey = 60, piew = 200, pieh = 200;
            float angle1 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(threeNum));
            float angle2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fourNum));
            float angle3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fiveNum));
            g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1);
            g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2);
            g.FillPie(brush4, piex, piey, piew, pieh, angle1 + angle2, angle3);
            g.DrawRectangle(pen1, 50, 300, 310, 130);

            g.FillRectangle(brush2, 90, 320, 20, 10);
            g.DrawString(string.Format("3月份销量占比:{0:P2}", Convert.ToSingle(threeNum) / Convert.ToSingle(sum)), font2, brush2, 120, 320);
            g.FillRectangle(brush3, 90, 360, 20, 10);
            g.DrawString(string.Format("4月份销量占比:{0:P2}", Convert.ToSingle(fourNum) / Convert.ToSingle(sum)), font2, brush2, 120, 360);
            g.FillRectangle(brush4, 90, 400, 20, 10);
            g.DrawString(string.Format("5月份销量占比:{0:P2}", Convert.ToSingle(fiveNum) / Convert.ToSingle(sum)), font2, brush2, 120, 400);
           
            this.groupBox1.Text = "饼状图";
            this.pictureBox1.Width = bitmap.Width;
            this.pictureBox1.Height = bitmap.Height;
            this.pictureBox1.BackgroundImage = bitmap;

        }
        private void BarMap()//柱状图
        {
            int[] saleNum = { 300, 500, 400 };
            int sum = saleNum[0]+ saleNum[1]+ saleNum[2];
            float[] Y_Num ={ Convert.ToSingle(saleNum[0]) / Convert.ToSingle(sum),Convert.ToSingle(saleNum[1]) / Convert.ToSingle(sum),
                    Convert.ToSingle(saleNum[2]) / Convert.ToSingle(sum) };
            int height = pictureBox1.Height, width = pictureBox1.Width;
            Bitmap image = new Bitmap(width, height);
            //创建Graphics类对象
            Graphics g = Graphics.FromImage(image);
            try
            {
                //清空图片背景色
                g.Clear(Color.White);
                Font font = new Font("Arial", 10, FontStyle.Regular);
                Font font1 = new Font("宋体", 20, FontStyle.Bold);
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),Color.Blue, Color.BlueViolet, 1.2f, true);

                Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
                SolidBrush mybrush = new SolidBrush(Color.Red);
                SolidBrush mybrush2 = new SolidBrush(Color.Green);
                Pen mypen = new Pen(brush, 1);
                //绘制线条
                //绘制横向线条
                int x = 100;
                Pen mypen1 = new Pen(Color.Blue, 2);
                x = 60;
                g.DrawLine(mypen1, x, 0, x, 300);

                //绘制纵向线条
                int y = 0;
                for (int i = 0; i <11; i++)
                {
                    g.DrawLine(mypen, 45, y, 60, y);
                    y = y + 30;
                }
               g.DrawLine(mypen1, 60, y-30, 620, y-30);

                //x轴
                String[] n = { "3月份", "4月份", "5月份"};
                x = 100;
                for (int i = 0; i < 3; i++)
                {
                    g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 300); //设置文字内容及输出位置

                    Console.WriteLine(300 - Y_Num[i] * 100 * 3);
                    g.FillRectangle(mybrush, x, 300 - Y_Num[i] * 100 * 3, 20, Y_Num[i] * 100 * 3);
                    g.DrawString(Y_Num[i].ToString(), font2, Brushes.Green, x, 300 - Y_Num[i] * 100 * 3 - 15);

                    x = x + 100;
                }

                //y轴
                String[] m = {"0","0.10","0.20", "0.30", "0.40", "0.50", "0.60", "0.70", " 0.80"," 0.90", " 1.00" };
                y = 0;
                for (int i = 10; i >= 0; i--)
                {
                    g.DrawString(m[i].ToString(), font, Brushes.Blue, 20, y); //设置文字内容及输出位置
                    y = y + 30;
                }
                //绘制标识
                Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
                g.DrawRectangle(new Pen(Brushes.Blue), 170, 390, 250, 50); //绘制范围框
                g.FillRectangle(Brushes.Red, 200, 410, 20, 10); //绘制小矩形
                g.DrawString("月销量占比", font3, Brushes.Red, 292, 408);
                this.button1.Text = "查看饼状图";
                this.groupBox1.Text = "柱状图";
                this.pictureBox1.Width = image.Width;
                this.pictureBox1.Height = image.Height;
                this.pictureBox1.BackgroundImage = image; 
            }
            catch { }
 
         }
        private void Button1_Click(object sender, EventArgs e)
        {
            BitMap();
            this.button1.Visible =false;//隐藏button
        }
     
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#实现运行状态堆叠柱状图

    本文实例为大家分享了C#实现运行状态堆叠柱状图的具体代码,供大家参考,具体内容如下 需求通过柱状图显示设备运行时间停止时间,稼动率等通过数据库查询记录,按小时显示显示目标数据. 界面设计 添加用户控件(窗体) 代码如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.F

  • C# 绘制统计图大全(柱状图, 折线图, 扇形图)

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用就C# 制作三款最经典的统计图: 柱状图, 折线图和扇形图:既然是统计, 当然需要数据, 这里演示的数据存于Sql Server2000中, 三款统计图形都是动态生成. 其中柱状图我会附上制作步骤, 其他两款统计图直接附源码. 说明: 需求不一样, 统计图形绘制后的显示效果也不一样, 比如这里柱状图

  • C#制作二维柱状图方法

    在程序运行中经常需要对数据进行对比显示,其中使用柱状图显示非常直观,可以更显著的比较出数据量的走势. 下面介绍在C#中柱状图的制作方法: 1.方法/步骤1 打开Microsoft Visual Studio 2010, 新建名字为[柱状图]的程序. 打开Microsoft Visual Studio 2010, 新建名字为[柱状图]的程序. 2.添加OWC11的引用. 3.在新程序界面上放置合适的控件:包括, 1个image控件: 用于输入6个月份的消费金额的输入框: 产生柱状图的按钮. 4.添

  • C#绘制饼状图和柱状图的方法

    本文实例为大家分享了C#绘制饼状图和柱状图的具体代码,供大家参考,具体内容如下 #代码如下: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Drawing.Drawing2D

  • Android动态绘制饼状图的示例代码

    项目里面的需求,当时搜索到MPAndroidChart库,可以实现,但是只是一个需求就引用偌大的一个库,感觉不太爽,打算自己自定义一个. 一.惯例先上效果图 更新图 二.GitHub 代码地址,欢迎指正https://github.com/MNXP/XPPieChart 三.思路 1.空心图(一个大圆中心绘制一个小圆)   2.根据数据算出所占的角度   3.根据动画获取当前绘制的角度   4.根据当前角度获取Paint使用的颜色   5.动态绘制即将绘制的 和 绘制已经绘制的部分(最重要) 四

  • Python可视化神器pyecharts绘制饼状图

    目录 饼图 概念 用法 优势 饼状图系列模板 简单多色饼状图(类别可配色) 象形饼状图 环形饼状图 不调色饼状图(大小位置可控制) 数据类别大量显示柱状图 多饼状图同时显示 玫瑰饼状图双图显示 环形饼状图(数据标签左放) 饼图 概念 饼图(pie chart)是用圆形及圆内扇形的角度来表示数值大小的图形,它主要用于表示一个样本(或总体)中各组成部分的数据占全部数据的比例.仅排列在工作表的一列或一行中的数据可以绘制到饼图中.饼图显示一个数据系列 (数据系列:在图表中绘制的相关数据点,这些数据源自数

  • php使用Jpgraph绘制饼状图的方法

    本文实例讲述了php使用Jpgraph绘制饼状图的方法.分享给大家供大家参考.具体实现方法如下: <?php include ("src/jpgraph.php"); include ("src/jpgraph_pie.php"); $data = array(19,23,34,38,45,67,71,78,85,87,90,96); $graph = new PieGraph(400,300); $graph->SetShadow(); $graph-

  • Python使用matplotlib的pie函数绘制饼状图功能示例

    本文实例讲述了Python使用matplotlib的pie函数绘制饼状图功能.分享给大家供大家参考,具体如下: matplotlib具体安装方法可参考前面一篇http://www.jb51.net/article/51812.htm,具体使用代码如下: #coding=utf8 import matplotlib as mpl import numpy as np import matplotlib.pyplot as plt ''''' matplotlib.pyplot.pie函数:画一个饼

  • Python matplotlib绘制饼状图功能示例

    本文实例讲述了Python matplotlib绘制饼状图功能.分享给大家供大家参考,具体如下: 一 代码 import numpy as np import matplotlib.pyplot as plt #The slices will be ordered and plotted counter-clockwise. labels ='Frogs','Hogs','Dogs','Logs' sizes =[15,30,45,10] colors =['yellowgreen','gold

  • R语言绘制饼状图代码实例

    R编程语言有许多库来创建图表和图表. 饼图是将值表示为具有不同颜色的圆的切片. 切片被标记,并且对应于每个片的数字也在图表中表示. 在R语言中,饼图是使用pie()函数创建的,它使用正数作为向量输入. 附加参数用于控制标签,颜色,标题等. 语法 使用R语言创建饼图的基本语法是 pie(x, labels, radius, main, col, clockwise) 以下是所使用的参数的描述 x是包含饼图中使用的数值的向量. labels用于给出切片的描述. radius表示饼图圆的半径(值-1和

  • Python实战基础之绘制饼状图分析商品库存

    目录 一.实战场景 二.主要知识点 三.菜鸟实战 1.创建 python 文件 2.运行结果 补充:其中pie()函数中的参数: 总结 一.实战场景 实战场景:如何绘制饼状图分析商品库存 二.主要知识点 文件读写 基础语法 字符串处理 文件生成 数据构建 三.菜鸟实战 马上安排! 1.创建 python 文件 """ Author: 菜鸟实战 实战场景: 如何绘制饼状图分析商品库存 """ # 导入系统包 import platform from

  • iOS使用Charts框架绘制饼状图

    首先先看一下效果: 饼状图 一.创建饼状图对象 创建饼状图对象用到类是PieChartView.h, 代码如下: self.pieChartView = [[PieChartView alloc] init]; self.pieChartView.backgroundColor = BgColor; [self.view addSubview:self.pieChartView]; [self.pieChartView mas_makeConstraints:^(MASConstraintMak

  • 利用php绘制饼状图的实现代码

    drawPieImg()函数包含8个参数,$title为饼状图的标题:$dataArr为需要显示的数据数组:$labelArr为对应数据的标签分类数组:$colorArr为对应数据的绘图颜色数组,这4个参数是必须的,对于不同的系统应用传递相应的参数即可.接下来的4个参数,负责设置要生成的饼状图的大小,如果不设置则使用系统默认值.程序按照床底数组数据的大小,从0度开始绘制,方向按照顺时针方向依次绘制对应数据占据的扇面大小. 复制代码 代码如下: <?php //变量定义,画椭圆弧时的角度大小 de

随机推荐