none
ASP.NET 绘图. RRS feed

  • Question

  • using System;
    
    using System.Configuration;
    
    using System.Data;
    
    using System.Linq;
    
    using System.Web;
    
    using System.Web.Security;
    
    using System.Web.UI;
    
    using System.Web.UI.HtmlControls;
    
    using System.Web.UI.WebControls;
    
    using System.Web.UI.WebControls.WebParts;
    
    using System.Xml.Linq;
    
    using System.Drawing;
    
    using System.Drawing.Text;
    
    using System.Drawing.Imaging;
    
    using System.Drawing.Drawing2D;
    
    
    
    
    
    public partial class _Default : System.Web.UI.Page
    
    {
    
        int y = 300;
    
        Random rnd = new Random();
    
        int t = 0;
    
        protected void Page_Load(object sender, EventArgs e)
    
        {
    
            t = rnd.Next(-10, 11);
    
            Bitmap newBitmap = new Bitmap(800, 400);
    
            Graphics g = Graphics.FromImage(newBitmap);
    
            RectangleF Rf = new RectangleF(0, 0, 800, 400);
    
            g.FillRectangle(new SolidBrush(Color.White), Rf);
    
            g.DrawLine(new Pen(Color.Black), 20, 320, 750, 320);
    
            g.DrawLine(new Pen(Color.Black), 20, 10, 20, 320);
    
            for (int x = 20; x <= 750; x++)
    
            {
    
                g.DrawLine(new Pen(Color.Red), x++, y, x + 1, y += t);
    
            }
    
            //g.DrawLine(new Pen(Color.Red), 21, 310, 22, 302);
    
            //g.DrawLine(new Pen(Color.Red), 22, 302, 23, 280);
    
            //g.DrawLine(new Pen(Color.Red), 23, 280, 24, 302);
    
            //g.DrawLine(new Pen(Color.Red), 24, 302, 25, 299);
    
            //g.DrawLine(new Pen(Color.Red), 25, 299, 26, 305);
    
            //g.DrawLine(new Pen(Color.Red), 26, 305, 27, 309);
    
            //g.DrawLine(new Pen(Color.Red), 27, 309, 28, 298);
    
            //g.DrawLine(new Pen(Color.Red), 28, 298, 29, 297);
    
            //g.DrawLine(new Pen(Color.Red), 29, 297, 30, 296);
    
            //g.DrawLine(new Pen(Color.Red), 30, 296, 31, 291);
    
            Response.ContentType = "image/jpeg";
    
            newBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
    
            newBitmap.Dispose();
    
            }
    
    }
    
    
    
    想画一张曲线图,循环不知道怎么写了.麻烦帮小弟一把.
    
    
    Monday, February 1, 2010 2:30 PM

Answers

  • 你好!

    mschart 提供了丰富的图表功能,下面的关于 mschart 信息的连接。

    示例及详细信息地址:

    控件下载地址:

    控件语言包:

    VS2008插件安装:

    另外我从网上找了另外一个例子希望对你有帮助。

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    
    <script runat="server">
        public class LineChart
        {
            public Bitmap b;
            public string Title = "在ASP.NET中实现数据图表";
            public ArrayList chartValues = new ArrayList();
            public float Xorigin = 0, Yorigin = 0;
            public float ScaleX, ScaleY;
            public float Xdivs = 2, Ydivs = 2;
    
            private int Width, Height;
            private Graphics g;
            private Page p;
    
            struct datapoint
            {
                public float x;
                public float y;
                public bool valid;
            }
    
            public LineChart(int myWidth, int myHeight, Page myPage)
            {
                Width = myWidth;
                Height = myHeight;
                ScaleX = myWidth;
                ScaleY = myHeight;
                b = new Bitmap(myWidth, myHeight);
                g = Graphics.FromImage(b);
                p = myPage;
            }
    
            public void AddValue(int x, int y)
            {
                datapoint myPoint;
                myPoint.x = x;
                myPoint.y = y;
                myPoint.valid = true;
                chartValues.Add(myPoint);
            }
    
            public void Draw()
            {
                int i;
                float x, y, x0, y0;
                string myLabel;
                Pen blackPen = new Pen(Color.Blue, 2);//定义画笔   
                Brush blackBrush = new SolidBrush(Color.Black);//定义画刷   
                Font axesFont = new Font("arial", 10);//定义字体   
    
                //首先要创建图片的大小   
                p.Response.ContentType = "image/jpeg";
                g.FillRectangle(new SolidBrush(Color.LightGreen), 0, 0, Width, Height);
                int ChartInset = 50;
                int ChartWidth = Width - (2 * ChartInset);
                int ChartHeight = Height - (2 * ChartInset);
                g.DrawRectangle(new Pen(Color.Black, 1), ChartInset, ChartInset, ChartWidth, ChartHeight);
                //写出图片上面的图片内容文字   
                g.DrawString(Title, new Font("arial", 14), blackBrush, Width / 3, 10);
                //沿X坐标写入X标签   
                for (i = 0; i <= Xdivs; i++)
                {
                    x = ChartInset + (i * ChartWidth) / Xdivs;
                    y = ChartHeight + ChartInset;
                    myLabel = (Xorigin + (ScaleX * i / Xdivs)).ToString();
                    g.DrawString(myLabel, axesFont, blackBrush, x - 4, y + 10);
                    g.DrawLine(blackPen, x, y + 2, x, y - 2);
                }
                //沿Y坐标写入Y标签   
                for (i = 0; i <= Ydivs; i++)
                {
                    x = ChartInset;
                    y = ChartHeight + ChartInset - (i * ChartHeight / Ydivs);
                    myLabel = (Yorigin + (ScaleY * i / Ydivs)).ToString();
                    g.DrawString(myLabel, axesFont, blackBrush, 5, y - 6);
                    g.DrawLine(blackPen, x + 2, y, x - 2, y);
                }
                g.RotateTransform(180);
                g.TranslateTransform(0, -Height);
                g.TranslateTransform(-ChartInset, ChartInset);
                g.ScaleTransform(-1, 1);
    
                //画出图表中的数据   
                datapoint prevPoint = new datapoint();
                prevPoint.valid = false;
                foreach (datapoint myPoint in chartValues)
                {
                    if (prevPoint.valid == true)
                    {
                        x0 = ChartWidth * (prevPoint.x - Xorigin) / ScaleX;
                        y0 = ChartHeight * (prevPoint.y - Yorigin) / ScaleY;
                        x = ChartWidth * (myPoint.x - Xorigin) / ScaleX;
                        y = ChartHeight * (myPoint.y - Yorigin) / ScaleY;
                        g.DrawLine(blackPen, x0, y0, x, y);
                        g.FillEllipse(blackBrush, x0 - 2, y0 - 2, 4, 4);
                        g.FillEllipse(blackBrush, x - 2, y - 2, 4, 4);
                    }
                    prevPoint = myPoint;
                }
    
                //最后以图片形式来浏览   
                b.Save(p.Response.OutputStream, ImageFormat.Jpeg);
    
            }
    
            public void Dispose()
            {
                g.Dispose();
                b.Dispose();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面   
            LineChart c = new LineChart(640, 480, this.Page);
            c.Title = "在ASP.NET中实现数据图表";
            c.Xorigin = 0; c.ScaleX = 500; c.Xdivs = 5;
            c.Yorigin = 0; c.ScaleY = 1000; c.Ydivs = 5;
            c.AddValue(0, 150);
            c.AddValue(50, 50);
            c.AddValue(100, 700);
            c.AddValue(200, 150);
            c.AddValue(300, 450);
            c.AddValue(400, 75);
            c.AddValue(450, 450);
            c.AddValue(500, 250);
            c.Draw();
            c.Dispose();
        }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
    



    知识改变命运,奋斗成就人生!
    • Marked as answer by KeFang Chen Friday, February 5, 2010 2:45 AM
    Monday, February 1, 2010 3:15 PM
    Moderator

All replies

  • 你好!

    mschart 提供了丰富的图表功能,下面的关于 mschart 信息的连接。

    示例及详细信息地址:

    控件下载地址:

    控件语言包:

    VS2008插件安装:

    另外我从网上找了另外一个例子希望对你有帮助。

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    
    <script runat="server">
        public class LineChart
        {
            public Bitmap b;
            public string Title = "在ASP.NET中实现数据图表";
            public ArrayList chartValues = new ArrayList();
            public float Xorigin = 0, Yorigin = 0;
            public float ScaleX, ScaleY;
            public float Xdivs = 2, Ydivs = 2;
    
            private int Width, Height;
            private Graphics g;
            private Page p;
    
            struct datapoint
            {
                public float x;
                public float y;
                public bool valid;
            }
    
            public LineChart(int myWidth, int myHeight, Page myPage)
            {
                Width = myWidth;
                Height = myHeight;
                ScaleX = myWidth;
                ScaleY = myHeight;
                b = new Bitmap(myWidth, myHeight);
                g = Graphics.FromImage(b);
                p = myPage;
            }
    
            public void AddValue(int x, int y)
            {
                datapoint myPoint;
                myPoint.x = x;
                myPoint.y = y;
                myPoint.valid = true;
                chartValues.Add(myPoint);
            }
    
            public void Draw()
            {
                int i;
                float x, y, x0, y0;
                string myLabel;
                Pen blackPen = new Pen(Color.Blue, 2);//定义画笔   
                Brush blackBrush = new SolidBrush(Color.Black);//定义画刷   
                Font axesFont = new Font("arial", 10);//定义字体   
    
                //首先要创建图片的大小   
                p.Response.ContentType = "image/jpeg";
                g.FillRectangle(new SolidBrush(Color.LightGreen), 0, 0, Width, Height);
                int ChartInset = 50;
                int ChartWidth = Width - (2 * ChartInset);
                int ChartHeight = Height - (2 * ChartInset);
                g.DrawRectangle(new Pen(Color.Black, 1), ChartInset, ChartInset, ChartWidth, ChartHeight);
                //写出图片上面的图片内容文字   
                g.DrawString(Title, new Font("arial", 14), blackBrush, Width / 3, 10);
                //沿X坐标写入X标签   
                for (i = 0; i <= Xdivs; i++)
                {
                    x = ChartInset + (i * ChartWidth) / Xdivs;
                    y = ChartHeight + ChartInset;
                    myLabel = (Xorigin + (ScaleX * i / Xdivs)).ToString();
                    g.DrawString(myLabel, axesFont, blackBrush, x - 4, y + 10);
                    g.DrawLine(blackPen, x, y + 2, x, y - 2);
                }
                //沿Y坐标写入Y标签   
                for (i = 0; i <= Ydivs; i++)
                {
                    x = ChartInset;
                    y = ChartHeight + ChartInset - (i * ChartHeight / Ydivs);
                    myLabel = (Yorigin + (ScaleY * i / Ydivs)).ToString();
                    g.DrawString(myLabel, axesFont, blackBrush, 5, y - 6);
                    g.DrawLine(blackPen, x + 2, y, x - 2, y);
                }
                g.RotateTransform(180);
                g.TranslateTransform(0, -Height);
                g.TranslateTransform(-ChartInset, ChartInset);
                g.ScaleTransform(-1, 1);
    
                //画出图表中的数据   
                datapoint prevPoint = new datapoint();
                prevPoint.valid = false;
                foreach (datapoint myPoint in chartValues)
                {
                    if (prevPoint.valid == true)
                    {
                        x0 = ChartWidth * (prevPoint.x - Xorigin) / ScaleX;
                        y0 = ChartHeight * (prevPoint.y - Yorigin) / ScaleY;
                        x = ChartWidth * (myPoint.x - Xorigin) / ScaleX;
                        y = ChartHeight * (myPoint.y - Yorigin) / ScaleY;
                        g.DrawLine(blackPen, x0, y0, x, y);
                        g.FillEllipse(blackBrush, x0 - 2, y0 - 2, 4, 4);
                        g.FillEllipse(blackBrush, x - 2, y - 2, 4, 4);
                    }
                    prevPoint = myPoint;
                }
    
                //最后以图片形式来浏览   
                b.Save(p.Response.OutputStream, ImageFormat.Jpeg);
    
            }
    
            public void Dispose()
            {
                g.Dispose();
                b.Dispose();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面   
            LineChart c = new LineChart(640, 480, this.Page);
            c.Title = "在ASP.NET中实现数据图表";
            c.Xorigin = 0; c.ScaleX = 500; c.Xdivs = 5;
            c.Yorigin = 0; c.ScaleY = 1000; c.Ydivs = 5;
            c.AddValue(0, 150);
            c.AddValue(50, 50);
            c.AddValue(100, 700);
            c.AddValue(200, 150);
            c.AddValue(300, 450);
            c.AddValue(400, 75);
            c.AddValue(450, 450);
            c.AddValue(500, 250);
            c.Draw();
            c.Dispose();
        }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
    



    知识改变命运,奋斗成就人生!
    • Marked as answer by KeFang Chen Friday, February 5, 2010 2:45 AM
    Monday, February 1, 2010 3:15 PM
    Moderator
  • 你好,

    你上面的代码已经画出曲线了,还想怎么循环?
    Microsoft Online Community Support
    Wednesday, February 3, 2010 2:08 AM