文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP.NET C# 曲线图动态生成

ASP.NET C# 曲线图动态生成

时间:2008-11-24  来源:ring8595

  本代码主要用于ASP.NET中根据数据动态生成 曲线图。
关键在于 public void draw(Page page, string title,int x, int [] y) 这个函数
 原理为在:内存中画出一张图然后保存入流中
通过    page.Response.ContentType = "image/jpeg";
        page.Response.BinaryWrite(stream.ToArray());
返回给浏览器一张图片。
本程序 也是我从网上收集过来的。具体网址:
http://www.cnblogs.com/aowind/archive/2008/11/12/569536.html#1367686
谢谢作者
我做了点修改。主要是对draw进行的重载。。使之适应 X-Y坐标画图

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;

public class imgdraw
{
    public imgdraw()
    {
    }
    public int find_the_max(int [] y)
    {
        int max = 0 ;
        foreach(int temp in y)
        {
            if (temp > max)
                max = temp;
        }
        return max;
    }
    public void draw(Page page, string title,int x, int [] y)
    {      
       int count = x; //取得记录数量
       int wd = 80 + 20 * (count - 1);//记算图表宽度
       if (wd < 600) wd = 600;//设置最小宽度为600
       Bitmap img = new Bitmap(wd, 400);//生成Bitmap对像
       Graphics g = Graphics.FromImage(img);//生成绘图对像
       Pen Bp = new Pen(Color.Black);//定义黑色画笔 
       Pen Rp = new Pen(Color.Red);//定义红色画笔
       Pen Sp = new Pen(Color.Silver);//定义银灰色画笔
       //定义大标题字体
       Font Bfont = new Font("Arial", 12, FontStyle.Bold);
       Font font = new Font("Arial", 6);//定义一般字体
       Font Tfont = new Font("Arial", 9);//定义大点的字体
       g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);//绘制底色 
        LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true); //定义黑色过渡型笔刷       
        LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);//定义蓝色过渡型笔刷      
        g.DrawString( title, Bfont, brush, 40, 5); //绘制大标题
       string info = "曲线图生成时间:" + DateTime.Now.ToString() ;
        g.DrawString(info, Tfont, Bluebrush, 40, 25); //绘制信息简报
        //绘制图片边框
        g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);

        //绘制竖坐标线 
        for (int i = 0; i < count; i++)
        {
            g.DrawLine(Sp, 40 + 20 * i, 60, 40 + 20 * i, 360);
        }
        //绘制x轴坐标标签
        for (int i = 1; i <= count; i += 1)
        {
            string st = i.ToString();
            g.DrawString(st, font, brush, 15 + 20 * i, 370);
        }
        //绘制横坐标线
        for (int i = 0; i < 10; i++)
        {
            g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 20 * (count - 1), 60 + 30 * i);          
        }
        int max = 50; //= find_the_max(y);
        for (int i = 0; i < 11; i++)//罗 绘制 Y轴坐标的
        {
            int s = max - i * 5;
            g.DrawString(s.ToString(), font, brush, 20, 55 + 30 * i);
        }     
        g.DrawLine(Bp, 40, 55, 40, 360);//绘制竖坐标轴
        //绘制横坐标轴
        g.DrawLine(Bp, 40, 360, 45 + 20 * (count - 1), 360);
        //定义曲线转折点
        Point[] p = new Point[count];
        for (int i = 0; i < count; i++)
        {
            p[i].X = 40 + 20 * i;
            p[i].Y = 360 - 6 * y[i]; // 60 -> 360

        }
        g.DrawLines(Rp, p); //绘制曲线
        for (int i = 0; i < count; i++)
        {
            //绘制发送记录点的发送量
            g.DrawString(y[i].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
            //绘制发送记录点
            g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
        }       
        g.DrawString("频率", Tfont, brush, 5, 40); //绘制竖坐标标题
       
        g.DrawString("日期", Tfont, brush, 40, 385); //绘制横坐标标题
        //保存绘制的图片
        MemoryStream stream = new MemoryStream();
        img.Save(stream, ImageFormat.Jpeg);
        //图片输出
        page.Response.Clear();
        page.Response.ContentType = "image/jpeg";
        page.Response.BinaryWrite(stream.ToArray());

    }
}

使用的代码为
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    Data.DA da = new Data.DA();
    protected void Page_Load(object sender, EventArgs e)
    {
        string keyword = Request.Params["keyword"].ToString();
        string year = Request.Params["year"].ToString();
        string topicid = Request.Params["topicid"].ToString();
        string month = Request.Params["month"].ToString();
        string topictitle = Request.Params["topictitle"].ToString();
        int daysnumber = DateTime.DaysInMonth(Convert.ToInt32(year), Convert.ToInt32(month));
        int[] data = da.getTopicHotSpotFormYearandMonth(year, month, keyword, topicid);
        imgdraw image = new imgdraw();
        image.draw(this, "话题【" + topictitle + "】中关键词【" + keyword + "】曲线图", daysnumber, data);  
    }
}

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载