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>
/// X Title of coordinates
/// </summary>
public string Xtitle
{
get { return xtitle; }
set { xtitle = value; }
}
/// <summary>
/// Y Title of coordinates
/// </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 a picture
/// </summary>
/// <returns> The generated file path </returns>
public string Draw()
{
#region Basic definition
// Number of records obtained
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 Bitmap To like
Bitmap img = new Bitmap(wd, 400);
// Define the black brush
Pen Bp = new Pen(Color.Black);
// Bold black
Pen BBp = new Pen(Color.Black, 2);
// Define the red brush
Pen Rp = new Pen(Color.Red);
// Defines a silver-gray brush
Pen Sp = new Pen(Color.Silver);
// Define a large heading font
Font Bfont = new Font(" blackbody ", 12, FontStyle.Bold);
// define 1 Like the font
Font font = new Font("Arial", 8);
// Define a larger font
Font Tfont = new Font("Arial", 9);
// Define a black transition brush
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
// Define a blue transition 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 opposition
try
{
using (Graphics g = Graphics.FromImage(img))
{
#region Draw diagrams
// Draw the impression
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
// Draw a headline
g.DrawString(title, Bfont, brush, wd / 2 - title.Length * 10, 5);
// Draw subheadings
g.DrawString(title2, Tfont, Silverbrush, wd / 2 - title.Length * 10 + 40, 25);
// Draw picture border
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
// draw Y Grid line
for (int i = 0; i < (count < 12 ? 12 : count); i++)
g.DrawLine(Sp, 40 + 50 * i, 60, 40 + 50 * i, 360);
// draw X Axis label
for (int i = 0; i < count; i++)
g.DrawString(itemlist[i][0].ToString(), font, brush, 30 + 50 * i, 370);
// draw X Grid 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;// One of the biggest Y Coordinate values
g.DrawString(Math.Floor(s).ToString(), font, brush, 10, 55 + 30 * i);
}
// draw Y coordinate
g.DrawLine(BBp, 40, 50, 40, 360);
// draw X coordinate
g.DrawLine(BBp, 40, 360, 40 + 50 * ((count < 12 ? 12 : count) - 1) + 10, 360);
#endregion
#region Draw the curve
// Define 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++)
{
// Draws the value of the sending record point
g.DrawString(itemlist[i][1].ToString(), font, Bluebrush, p[i].X + 5, p[i].Y - 10);
// Draws the sending record point
g.DrawRectangle(Rp, p[i].X - 2, p[i].Y - 2, 4, 4);
}
#endregion
// draw Y Coordinates the title
g.DrawString(ytitle, Tfont, brush, 10, 40);
// draw X Coordinates the title
g.DrawString(xtitle, Tfont, brush, 30, 385);
// Image quality
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// Save the drawing
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;
}
}
}
}