Asp. NET Implementation Data Graph 1

Source: Internet
Author: User
Asp.net| Data | Chart three. Implement the complete source code and running interface of the data graph in asp.net:

In the master of the production of pictures, in the picture coloring, output characters in the picture, and drawing lines and other basic operations, the full use of a variety of basic operations, can be achieved in the asp.net to achieve the complete data graph program, the following figure is the operating interface:

Figure 05: Implementing the Run interface of the data graph in asp.net

The following is the complete code for implementing the data graph in asp.net (chart1.aspx), as follows:

<%@ Import Namespace = "System"%><%@ import Namespace = "System.Drawing"%><%@ import Namespace = "System.Drawing.Drawing 2d "%><%@ Import Namespace =" System.Drawing.Imaging "%> <script language =" C # "runat =" server ">class linechart{public Bitmap b;p ublic string Title = "Implement data graph in asp.net";p ublic ArrayList chartvalues = new ArrayList ();p ublic float Xorigin = 0, Yorigin = 0;p ublic float ScaleX, ScaleY;p ublic float xdivs = 2, Ydivs = 2;p rivate int Width, Height;p rivate G Raphics g;p rivate Page p struct Datapoint {public float x;p ublic float y;p ublic bool valid;} Initializes 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; chart Values. 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);//First create the size p of the picture. Response. ContentType = "Image/jpeg"; G. FillRectangle (New SolidBrush, Color. LightGreen), 0, 0, Width, Height); int chartinset = + int chartwidth = Width-(2 * chartinset); int chartheight = Height-(2 * chartinset); G. DrawRectangle (New Pen (Color). Black, 1), Chartinset, Chartinset, Chartwidth, chartheight);//write the picture above the picture content of the text G. DrawString (Title, New Font ("Arial", Blackbrush, WIDTH/3, 10);//write X tags along x coordinates 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 + g); DrawLine (Blackpen, x, y + 2, x, y-2);} Writes y-tags along y-coordinates 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); DrawLine (Blackpen, x + 2, y, x-2, y);} G. RotateTransform (180); G. TranslateTransform (0,-Height); G. TranslateTransform (-Chartinset, Chartinset); G. ScaleTransform (-1, 1),//Draw the data in the chart datapoint prevpoint = new Datapoint ();p revpoint. valid = false; foreach (Datapoint mypoint in chartvalues) {if (prevpoint. valid = = True) {x0 = Chartwidth * (prevpoi Nt. 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;} Finally, browse B in the form of a picture. Save (p. Response. OutputStream, ImageFormat. JPEG); ~linechart () {G. Dispose (); b. Dispose ();}} void Page_Load (Object sender, EventArgs e) {Linechart c = new Linechart (640,, Page); Title = "Implement data graph in asp.net"; c. Xorigin = 0; C. ScaleX = 500; C. Xdivs = 5; c. Yorigin = 0; C. ScaleY = 1000; C. Ydivs = 5; c. AddValue (0,) c. AddValue (M); c. AddValue (MB); c. AddValue (c); AddValue (c, 450); AddValue (c.) AddValue (450, 450); c. AddValue (c.) Draw ();} </script >




Four Summarize:

Implementation of the chart is always a difficult point in the Internet programming, this article describes how to implement the data graph in the ASP.net page, in the absence of any good components can be used, in the. Net FrameWork SDK GDI + provided in the various methods used to manipulate graphics, such a process although a bit tumultuous , but it's very useful for implementing complex diagrams. I hope this article can not only help readers solve the problem on the Internet, but also to the reader of GDI + also have some understanding.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.