书上讲了折线图的画法,可惜没把坐标画好,整个图成了个表格图,只好自己到网上查资料来试验。
private static Bitmap DrawCoordinate(Bitmap image, string[] ItemName, decimal[] ItemValue)
{
//坐标轴
Point P0 = new Point(60, 360);
Point Px = new Point(420, 360);
Point Py = new Point(60, 65);
Pen pen = new Pen(Color.Black);
//箭头
Point Py1 = new Point(58, 70);
Point Py2 = new Point(62, 70);
Point Px1 = new Point(415, 358);
Point Px2 = new Point(415, 362);
//Y,X Value
//y 280-10
int YCount = 10;//Y轴点的数量
int YDistance = Convert.ToInt32(280 / YCount);//Y轴点击的距离
int[] YValue = GetYValue(ItemValue, YCount);
int len = 3;//短线的长度
int XCount = ItemName.Length;//X轴点的数量
int XDistance = Convert.ToInt32(350 / XCount);//X轴点间的距离
//
Font f = new Font("新宋体", 8, FontStyle.Bold);
//Image
Graphics g = Graphics.FromImage(image);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
try
{
//绘制坐标轴线
g.DrawLine(pen, P0, Px);
g.DrawLine(pen, P0, Py);
//箭头
g.DrawLine(pen, Py, Py1);
g.DrawLine(pen, Py, Py2);
g.DrawLine(pen, Px, Px1);
g.DrawLine(pen, Px, Px2);
//X
for (int i = 1; i <= XCount; i++)
{
Point pl1 = new Point(P0.X + i * XDistance, P0.Y);
Point pl2 = new Point(P0.X + i * XDistance, P0.Y - len);
string str = ItemName[i - 1];
Point ps = new Point(pl1.X - (str.Length * 8), pl1.Y + 5);
g.DrawLine(pen, pl1, pl2);
g.DrawString(str, f, new SolidBrush(Color.Black), ps);
}
//Y
for (int i = 1; i <= YCount; i++)
{
Point pl1 = new Point(P0.X, P0.Y - YDistance * i);
Point pl2 = new Point(pl1.X + len, pl1.Y);
string str = YValue[i - 1].ToString();
Point ps = new Point(pl1.X - str.Length * 8, pl1.Y - 5);
g.DrawLine(pen, pl1, pl2);
g.DrawString(str, f, new SolidBrush(Color.Black), ps);
}
g.DrawString("0", f, new SolidBrush(Color.Black), new Point(P0.X - 10, P0.Y - 10));
return image;
}
finally
{
g.Dispose();
}
}
Bitmap bmp = new Bitmap(500,400);
Bitmap result = new Bitmap(500,400);
Graphics g = Graphics.FromImage(bmp);
string[] x = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17","18","19","20", "21", "22", "23", "24", "25", "26", "27", "28", "29","30"};
//横坐标的坐标值
decimal[] y = {100,200,156,100,200,156,100,200,156};
//纵坐标的坐标值
result = DrawCoordinate(bmp,x,y);
result.Save(Environment.CurrentDirectory + "/week.bmp");