zoukankan      html  css  js  c++  java
  • c#制作图表(从数据库读取数据,制作柱状图,扇形图)

    代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    using System.Drawing.Drawing2D;


    namespace WindowsApplication30
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }

    //柱状图
    private void button3_Click(object sender, EventArgs e)
    {
    this.Height = 600;
    this.Width = 500;
    pictureBox1.Height
    = 500;
    pictureBox1.Width
    =450;
    int[] a =new int[]{30,20,50,40,40,78,45,98,87,47,46,98 };
    int w=400;
    int h=450;
    Bitmap bmp
    = new Bitmap(w, h);
    Graphics g
    = Graphics.FromImage(bmp);
    g.Clear(Color.White);
    g.DrawRectangle(Pens.AliceBlue,
    0, 0, 399, 399);
    string title = "2009销售情况统计表";
    Font f
    = new Font("黑体", 16);
    g.DrawString(title, f, Brushes.Blue, (w
    / 2 - g.MeasureString(title, f).Width / 2), 2);
    float v_width = w;
    float v_height=w-2-g.MeasureString(title,f).Height-10;
    float p_width = w / 25;
    float p_heigth = v_height / 150;
    float x = 0;
    float y = 0;
    double k = 0;
    Font f9
    = new Font("宋体", 9);
    for (int i = 0; i < 12;i++ )
    {
    x
    = (i * 2 + 1) * p_width;
    y
    = w - p_heigth * a[i];
    g.FillRectangle(Brushes.Black, x, y, p_width, a[i]
    * p_heigth);
    k
    = i + 1;
    g.DrawString(k.ToString()
    + "", f9, Brushes.Black, x, w + 2);

    g.DrawString(a[i].ToString(), f9, Brushes.Black, x
    -4,y - 2 - g.MeasureString(a[i].ToString(), f9).Width / 2-2);
    }
    MemoryStream ms
    = new MemoryStream();
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    Image img
    = Image.FromStream(ms);
    pictureBox1.Image
    = img;
    ms.Close();
    }
    //画扇形图
    private void button4_Click(object sender, EventArgs e)
    {
    List
    <int> mylist=new List<int>();
    mylist.Add(
    200);
    mylist.Add(
    100);
    mylist.Add(
    300);
    mylist.Add(
    350);

    int sum = 0;
    for (int i = 0; i < mylist.Count; i++)
    {
    sum
    += mylist[i];

    }
    float pangle = 360.0F/sum;
    Bitmap bmp
    = new Bitmap(500, 450);
    Graphics g
    = Graphics.FromImage(bmp);
    g.Clear(Color.White);
    g.SmoothingMode
    = SmoothingMode.AntiAlias;
    g.DrawRectangle(Pens.Blue,
    0, 0, 449, 449);
    Font f16
    = new Font("黑体", 16);
    g.DrawString(
    "2009年季度销售情况统计表", f16, Brushes.Black, 225 - g.MeasureString("2009年季度销售情况统计表", f16).Width / 2, 2);

    Brush[] mybrush
    =new Brush[]{Brushes.Aqua,Brushes.AntiqueWhite,Brushes.AliceBlue,Brushes.Chocolate};
    Rectangle r
    = new Rectangle(25, 25, 400, 400);
    float angle = 0;
    float sangle=0;
    for (int i = 0; i < mylist.Count; i++)
    {
    angle
    = pangle * mylist[i];
    g.DrawPie(Pens.Brown, r, sangle, angle);
    g.FillPie(mybrush[i], r, sangle, angle);
    sangle
    = sangle + angle;

    }
    MemoryStream ms
    = new MemoryStream();
    bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    this.Width = 500;
    this.Height = 500;
    pictureBox1.Width
    = 455;
    pictureBox1.Height
    = 455;
    pictureBox1.Image
    = Image.FromStream(ms);
    ms.Close();
    }

    //画曲线图
    private void button5_Click(object sender, EventArgs e)
    {
    this.Height = 600;
    this.Width = 500;
    pictureBox1.Height
    = 500;
    pictureBox1.Width
    = 450;
    int[] a = new int[] { 30, 20, 50, 40, 40, 78, 45, 98, 87, 47, 46, 98 };
    int w = 400;
    int h = 450;
    Bitmap bmp
    = new Bitmap(w, h);
    Graphics g
    = Graphics.FromImage(bmp);
    g.Clear(Color.White);
    g.DrawRectangle(Pens.AliceBlue,
    0, 0, 399, 399);
    string title = "2009销售情况统计表";
    Font f
    = new Font("黑体", 16);
    g.DrawString(title, f, Brushes.Blue, (w
    / 2 - g.MeasureString(title, f).Width / 2), 2);
    float v_width = w;
    float v_height = w - 2 - g.MeasureString(title, f).Height - 10;
    float p_width = w / 25;
    float p_heigth = v_height / 150;
    float x = 0;
    float y = 0;
    float x1 = 0;
    float y1 = 0;
    double k = 0;

    Font f9
    = new Font("宋体", 9);
    for (int i = 0; i < 11; i++)
    {
    x1
    = ((i+1) * 2 + 1) * p_width;
    y1
    = h - 50 - p_heigth * a[i+1];
    x
    = (i * 2 + 1) * p_width;
    y
    = h-50 - p_heigth * a[i];

    Point p1
    = new Point((int)x, (int)y);
    Point p2
    = new Point((int)x1, (int)y1);

    g.DrawLine(Pens.BlueViolet, p1, p2);

    k
    = i + 1;
    g.DrawString(k.ToString()
    + "", f9, Brushes.Black, x, w + 2);

    g.DrawString(a[i].ToString(), f9, Brushes.Black, x
    - 4, y - 2 - g.MeasureString(a[i].ToString(), f9).Width / 2 - 2);

    }
    MemoryStream ms
    = new MemoryStream();
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    Image img
    = Image.FromStream(ms);
    pictureBox1.Image
    = img;
    ms.Close();
    }
    }
    }
  • 相关阅读:
    蓝桥杯--2012--奇怪的比赛(全排列)
    mysql数据库读写分离教程
    Linux中安装java JDK
    mysql数据库主从复制教程
    mysql数据库安装教程
    http报文详解
    查看磁盘I/O命令iostat详解
    linux如何查看服务器当前的并发访问量
    Another app is currently holding the yum lock; waiting for it to exit...
    linux清理缓存cache
  • 原文地址:https://www.cnblogs.com/zgz_dpl/p/1876682.html
Copyright © 2011-2022 走看看