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:
Class Linechart
{
public Bitmap B;
public string Title = "Implement data graph in 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;
}
Class
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);
To create the size of a picture first
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);
Write the picture above the picture content 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 + 10);
G. DrawLine (Blackpen, x, y + 2, x, y-2);
}
Write y tag along y-coordinate
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);
Draw the data in the chart
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;
}
Finally, the picture form to browse
B. Save (p. Response. OutputStream, ImageFormat. JPEG);
}
~linechart () {
G. Dispose ();
B. Dispose ();
}
}
void Page_Load (Object sender, EventArgs e)
{
Linechart C = new Linechart (640, the Page);
C. 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, 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 ();
}
</script >
Four. Summarize:
Implementation of the chart is always a difficult point in the Internet programming, this article describes how to implement the data in the ASP.net page chart, in the absence of any good components can be used under the premise of using. Net FrameWork SDK GDI + provides a variety of methods for manipulating graphics, which, while somewhat tumultuous, is 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.
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