SoFunction
Updated on 2025-03-06

C# curve graph generation code


using System;
using ;
using ;
using ;

using ;
using .Drawing2D;
using ;
using ;
using ;

namespace Curve
{
public class CurveDrawing
{
string title, title2, ytitle, xtitle;
/// <summary>
/// Title of 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 = "")
{
= itemlist;
= title;
this.title2 = title2;

yMax = -100000000;
yMin = 100000000;
for (int i = 0; i < ; i++)
{
if ((itemlist[i][1]) > yMax)
yMax = (itemlist[i][1]);
if ((itemlist[i][1]) < yMin)
yMin = (itemlist[i][1]);
}
}
/// <summary>
/// Create and output pictures
/// </summary>
/// <returns>The generated file path</returns>
public string Draw()
{
#region Basic Definition
//Acquiring the number of records
int count = ;

//Recalculate the chart width
int wd = 80 + 50 * (count - 1);
//Set the minimum width to 640
if (wd < 640) wd = 640;
//Generate Bitmap object
Bitmap img = new Bitmap(wd, 400);
//Define black brush
Pen Bp = new Pen();
//Bold black
Pen BBp = new Pen(, 2);
//Define the red brush
Pen Rp = new Pen();
//Define silver-gray brush
Pen Sp = new Pen();
//Define the large title font
Font Bfont = new Font("bold", 12, );
//Define general font
Font font = new Font("Arial", 8);
//Define larger font
Font Tfont = new Font("Arial", 9);
//Define black transition brush
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, , ), , , 1.2F, true);
//Define the blue transition brush
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, , ), , , 1.2F, true);
LinearGradientBrush Silverbrush = new LinearGradientBrush(new Rectangle(0, 0, , ), , , 1.2F, true);
#endregion

//Generate drawing objects
try
{
using (Graphics g = (img))
{
#region Draw a chart
//Draw the background color
(new Pen(, 400), 0, 0, , );
//Draw a big title
(title, Bfont, brush, wd / 2 - * 10, 5);
//Draw subtitle
(title2, Tfont, Silverbrush, wd / 2 - * 10 + 40, 25);
//Draw picture border
(Bp, 0, 0, - 1, - 1);

//Draw Y coordinate lines
for (int i = 0; i < (count < 12 ? 12 : count); i++)
(Sp, 40 + 50 * i, 60, 40 + 50 * i, 360);
//Draw X-axis coordinate label
for (int i = 0; i < count; i++)
(itemlist[i][0].ToString(), font, brush, 30 + 50 * i, 370);
//Draw X coordinate lines
for (int i = 0; i < 11; i++)
{
(Sp, 40, 60 + 30 * i, 40 + 50 * ((count < 12 ? 12 : count) - 1), 60 + 30 * i);
double s = yMax - (yMax + (yMin)) / 10 * i;//Maximum Y coordinate value
((s).ToString(), font, brush, 10, 55 + 30 * i);
}


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

#endregion

#region Draw curves
//Define the turning point of the curve
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
p[i].X = 40 + 50 * i;
p[i].Y = 360 - (int)((((itemlist[i][1]) + (yMin)) / ((yMax + (yMin)) / 10)) * 30);
}
//Draw the sending curve
(Rp, p);

for (int i = 0; i < count; i++)
{
//Draw the value of the sending record point
(itemlist[i][1].ToString(), font, Bluebrush, p[i].X + 5, p[i].Y - 10);
//Draw the sending record point
(Rp, p[i].X - 2, p[i].Y - 2, 4, 4);
}

#endregion

//Draw the Y coordinate title
(ytitle, Tfont, brush, 10, 40);
//Draw X coordinate title
(xtitle, Tfont, brush, 30, 385);
//Picture quality
= .;
//Save the drawn picture
string basePath = ("/Curve/"),
fileName = () + ".jpg";

using (FileStream fs = new FileStream(basePath + fileName, ))
{
if (!(basePath))
(basePath);
(fs, );
return "/Curve/" + fileName;
}
}

}
catch (Exception)
{
throw;
}

}
}
}