C # Graph Generation Code

Source: Internet
Author: User

Copy codeThe Code is 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>
/// Y coordinate title
/// </Summary>
Public string Ytitle
{
Get {return ytitle ;}
Set {ytitle = value ;}
}
/// <Summary>
/// Subtitle
/// </Summary>
Public string Title2
{
Get {return title2 ;}
Set {title2 = value ;}
}
/// <Summary>
/// Subject
/// </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 an image
/// </Summary>
/// <Returns> path of the generated file </returns>
Public string Draw ()
{
# Region basic definition
// Obtain the number of records
Int count = itemlist. Count;

// Calculate the chart width
Int wd = 80 + 50*(count-1 );
// Set the minimum width to 640
If (wd <640) wd = 640;
// Generate a Bitmap object
Bitmap img = new bitmaps (wd, 400 );
// Define the black paint brush
Pen Bp = new Pen (Color. Black );
// Bold black
Pen BBp = new Pen (Color. Black, 2 );
// Define the red paint brush
Pen Rp = new Pen (Color. Red );
// Defines the silver-gray paint brush
Pen Sp = new Pen (Color. Silver );
// Define the title Font
Font Bfont = new Font ("", 12, FontStyle. Bold );
// Define a general font
Font font = new Font ("Arial", 8 );
// Define the font of the big point
Font Tfont = new Font ("Arial", 9 );
// Define the black transition paint brush
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, img. Width, img. Height), Color. Black, Color. Black, 1.2F, true );
// Define the blue transition paint 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 Object
Try
{
Using (Graphics g = Graphics. FromImage (img ))
{
# Region chart
// Draw the background color
G. DrawRectangle (new Pen (Color. White, 400), 0, 0, img. Width, img. Height );
// Draw a large title
G. DrawString (title, Bfont, brush, wd/2-title. Length * 10, 5 );
// Draw a title
G. DrawString (title2, Tfont, Silverbrush, wd/2-title. Length * 10 + 40, 25 );
// Draw the image border
G. DrawRectangle (Bp, 0, 0, img. Width-1, img. Height-1 );

// Draw Y coordinate lines
For (int I = 0; I <(count <12? 12: count); I ++)
G. DrawLine (Sp, 40 + 50 * I, 60, 40 + 50 * I, 360 );
// Draw the X axis coordinate label
For (int I = 0; I <count; I ++)
G. DrawString (itemlist [I] [0]. ToString (), font, brush, 30 + 50 * I, 370 );
// Draw the X coordinate line
For (int I = 0; I <11; I ++)
{
G. DrawLine (Sp, 40, 60 + 30 * I, 40 + 50 * (count <12? 12: count)-1), 60 + 30 * I );
Double s = yMax-(yMax + Math. Abs (yMin)/10 * I; // The maximum Y coordinate value
G. DrawString (Math. Floor (s). ToString (), font, brush, 10, 55 + 30 * I );
}

// Draw the Y axis
G. DrawLine (BBp, 40, 50, 40,360 );
// Draw the X axis
G. DrawLine (BBp, 40,360, 40 + 50 * (count <12? 12: count)-1) + 10,360 );

# Endregion

# Region plot a curve
// Define the curve turning point
Point [] p = new Point [count];
For (int I = 0; I <count; I ++)
{
P [I]. X = 40 + 50 * I;
P [I]. Y = 360-(int) (Convert. toDouble (itemlist [I] [1]) + Math. abs (yMin)/(yMax + Math. abs (yMin)/10) * 30 );
}
// Draw the sending Curve
G. DrawLines (Rp, p );

For (int I = 0; I <count; I ++)
{
// Draw the value of the sending Record Point
G. DrawString (itemlist [I] [1]. ToString (), font, Bluebrush, p [I]. X + 5, p [I]. Y-10 );
// Draw the sending Record Point
G. DrawRectangle (Rp, p [I]. X-2, p [I]. Y-2, 4, 4 );
}

# Endregion

// Draw the Y coordinate title
G. DrawString (ytitle, Tfont, brush, 10, 40 );
// Draw the X coordinate title
G. DrawString (xtitle, Tfont, brush, 30,385 );
// Image Quality
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
// Save the drawn Image
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.