C # Graph Generation Code _c# tutorial

Source: Internet
Author: User
Tags abs
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;

Using System.Drawing;
Using System.Drawing.Drawing2D;
Using System.IO;
Using System.Drawing.Imaging;
Using System.Collections;

Namespace Curve
{
public class Curvedrawing
{
String title, Title2, Ytitle, Xtitle;
<summary>
The title of the X coordinate
</summary>
public string Xtitle
{
get {return xtitle;}
set {Xtitle = value;}
}
<summary>
Title of y-coordinate
</summary>
public string Ytitle
{
get {return ytitle;}
set {Ytitle = value;}
}
<summary>
Subtitle
</summary>
public string Title2
{
get {return title2;}
set {title2 = value;}
}
<summary>
Main Title
</summary>
public string Title
{
get {return title;}
set {title = value;}
}
Double Ymax, ymin;
List<arraylist> itemlist;

Public curvedrawing (list<arraylist> itemlist, string title, String title2 = "")
{
This.itemlist = itemlist;
This.title = title;
This.title2 = Title2;

Ymax =-100000000;
Ymin = 100000000;
for (int i = 0; i < itemlist. Count; i++)
{
if (Convert.todouble (itemlist[i][1]) > Ymax)
Ymax = convert.todouble (itemlist[i][1]);
if (Convert.todouble (itemlist[i][1]) < ymin)
Ymin = convert.todouble (itemlist[i][1]);
}
}
<summary>
Create and output pictures
</summary>
<returns> generated file path </returns>
public string Draw ()
{
#region Base Definition
Get the number of records
int count = itemlist. Count;

Count Chart width
int WD = + * (count-1);
Set minimum width of 640
if (WD < 640) WD = 640;
Generating bitmap pairs of image
Bitmap img = new Bitmap (WD, 400);
Define a black brush
Pen Bp = new Pen (color.black);
Bold Black
Pen BBp = new Pen (color.black, 2);
Define a red brush
Pen Rp = new Pen (color.red);
Define silver-gray brushes
Pen Sp = new Pen (color.silver);
Define font for large headings
Font bfont = new Font ("bold", FontStyle.Bold);
Defining generic Fonts
Font font = new Font ("Arial", 8);
Define a larger font
Font Tfont = new Font ("Arial", 9);
Define a black gradient brush
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, IMG). Width, IMG. Height), Color.Black, Color.Black, 1.2F, true);
Define a blue gradient brush
LinearGradientBrush Bluebrush = new LinearGradientBrush (new Rectangle (0, 0, IMG). Width, IMG. Height), Color.Blue, Color.Blue, 1.2F, true);
LinearGradientBrush Silverbrush = new LinearGradientBrush (new Rectangle (0, 0, IMG). Width, IMG. Height), Color.silver, Color.silver, 1.2F, true);
#endregion

Generate a drawing pair like
Try
{
using (Graphics g = Graphics.fromimage (img))
{
#region Draw a chart
Draw an undertone
G.drawrectangle (New Pen (Color.White,), 0, 0, IMG. Width, IMG. Height);
Draw large headings
g.DrawString (title, Bfont, Brush, WD/2-title. Length * 10, 5);
Draw a small caption
g.DrawString (Title2, Tfont, Silverbrush, WD/2-title. Length * 10 + 40, 25);
Draw a picture border
G.drawrectangle (Bp, 0, 0, img.) Width-1, IMG. HEIGHT-1);

Draw y-coordinate lines
for (int i = 0; I < (count < 12:count); i++)
G.drawline (Sp, + * I, p, + * I, 360);
Draw x axis coordinates label
for (int i = 0; i < count; i++)
g.DrawString (Itemlist[i][0]. ToString (), font, brush, + * I, 370);
Draw X Coordinate lines
for (int i = 0; i < i++)
{
G.drawline (Sp, + * I, + * (Count < 12:count)-1), + * i);
Double s = ymax-(ymax + math.abs (ymin))/i;//maximum y-coordinate value
g.DrawString (Math.floor (s). ToString (), font, brush, ten, + * i);
}


Draw a Y-axis
G.drawline (BBp, 40, 50, 40, 360);
Draw X Axis
G.drawline (BBp, 360, (Count < 12:count)-1) + 10, 360);

#endregion

#region Draw Curves
Defining curve turning points
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
P[i]. X = + * I;
P[i]. Y = 360-(int) ((convert.todouble (itemlist[i][1) + Math.Abs (ymin))/((Ymax + math.abs (ymin))/10) * 30);
}
Draw a Send curve
G.drawlines (Rp, p);

for (int i = 0; i < count; i++)
{
To draw a value that sends a record point
g.DrawString (Itemlist[i][1]. ToString (), Font, Bluebrush, P[i]. X + 5, P[i]. Y-10);
Draw Send record point
G.drawrectangle (Rp, P[i]. X-2, P[i]. Y-2, 4, 4);
}

#endregion

Draw y-coordinate title
g.DrawString (Ytitle, Tfont, Brush, 10, 40);
Draw X coordinate title
g.DrawString (Xtitle, Tfont, Brush, 30, 385);
Picture quality
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Save a drawn picture
String basepath = HttpContext.Current.Server.MapPath ("/curve/"),
FileName = Guid.NewGuid () + ". jpg";

using (FileStream fs = new FileStream (BasePath + fileName, filemode.createnew))
{
if (! System.IO.Directory.Exists (BasePath))
System.IO.Directory.CreateDirectory (BasePath);
Img. Save (FS, imageformat.jpeg);
Return "/curve/" + fileName;
}
}

}
catch (Exception)
{
Throw
}

}
}
}
Related Article

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.