书上讲了折线图的画法,可惜没把坐标画好,整个图成了个表格图,只好自己到网上查资料来试验。
介绍之前,先要说明一下:C#的绘图坐标是以左上角为(0,0)开始的。
private static Bitmap DrawCoordinate(Bitmap image, string[] ItemName, decimal[] ItemValue)
{
//坐标轴
Point P0 = new Point(60, 360);//原点坐标
Point Px = new Point(420, 360);//X轴最高点
Point Py = new Point(60, 65);//y轴最高点
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);
/*按这个值画图像会很中间,如果要画的图希望靠左下角一点需要自己改一下这写点的坐标,再次提醒坐标的值是从左上角(0,0)*开始的,举个例子可以改成:
Point P0 = new Point(30, 300);
Point Px = new Point(390, 300);
Point Py = new Point(30, 5);
Pen pen = new Pen(Color.Black);
Point Py1 = new Point(28, 10);
Point Py2 = new Point(32, 10);
Point Px1 = new Point(385, 298);
Point Px2 = new Point(385, 302);
这样就比较符合我们画出来的图了
*/
//Y,X Value
//y 280-10
int YCount = 10;//Y轴点的数量
int YDistance = Convert.ToInt32(280 / YCount);//Y轴点之间的距离
int[] YValue = GetYValue(ItemValue, YCount);//获取Y轴最大值
/**********************GetYValue函数*****************************
private static int[] GetYValue(decimal[] ItemValue, int YCount)
{
int len = ItemValue.Length;
int[] Value = new int[YCount];
int Max = Convert.ToInt32(ItemValue.Max());
int Min = Convert.ToInt32(ItemValue.Min());
int Distance = Convert.ToInt32((Max - Min) / (YCount - 1));
for (int i = 0; i < YCount; i++)
{
Value[i] = Min + Distance * i;
}
//Value[YCount - 1] = Max;
return Value;
}
*******************************************************************/
int len = 3;//短线的长度
int XCount = ItemName.Length;//X轴点的数量
int XDistance = Convert.ToInt32(350 / XCount);//X轴点间的距离 根据你的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");