用C#绘制坐标轴及坐标轴刻度
private void DrawXY(Bitmap bmp)
{
/* 描绘坐标轴函数 */
Graphics g = Graphics.FromImage(bmp); /* 利用图片对象生成画板 */
Pen pAxis = new Pen(Brushes.Black, 2);
AdjustableArrowCap aac = new AdjustableArrowCap(5, 10); /* 定义可调箭头 */
pAxis.CustomEndCap = aac;
/* 整体内缩move像素 */
float move = 50f;
float newX = bmp.Width - move;
float newY = bmp.Height - move;
/* 绘制X轴 */
PointF px1 = new PointF(move + 30f, newY);
PointF px2 = new PointF(newX, newY);
g.DrawLine(pAxis, px1, px2);
/* 绘制Y轴 */
PointF py1 = new PointF(move + 30f, move);
PointF py2 = new PointF(move + 30f, newY);
g.DrawLine(pAxis, py2, py1);
}
public static float[] DrawXLine(Bitmap bmp, double minX, double maxX, int len, int n) // 画出X轴上的刻度线,从任意值开始
{
Graphics g = Graphics.FromImage(bmp);
Pen pAxis = new Pen(Brushes.Black, 2);
float move = 50f;
float LenX = bmp.Width - 4 * move;
float[] axis = new float[len + 1]; /* 用于存储X坐标 */
for (int i = 0; i <= len; i++)
{
axis[i] = LenX * i / len + move + 50 + 15f;
PointF py1 = new PointF(axis[i], bmp.Height - move - 4);
PointF py2 = new PointF(axis[i], bmp.Height - move);
string sy = ((maxX - minX) * i / len + minX).ToString(tag);
g.DrawLine(pAxis, py1, py2);
int temp = 40; /* 坐标轴刻度字符的偏移量 */
switch (sy.Length)
{
/* 根据字符的位数不同设置不同的偏移量 */
case 1:
temp = 45; break;
case 2:
temp = 42; break;
case 3:
temp = 40; break;
case 4:
temp = 37; break;
case 5:
temp = 35; break;
case 6:
temp = 33; break;
case 7:
temp = 31; break;
case 8:
temp = 29; break;
case 9:
temp = 27; break;
case 10:
temp = 25; break;
case 11:
temp = 23; break;
default:
break;
}
g.DrawString(sy, new Font("宋体", 8f), Brushes.Black,
new PointF(LenX * i / len + move + 15f + temp, bmp.Height - move / 1.1f));
}
Pen pen = new Pen(Color.Black, 1);
string str = "时间";
g.DrawString(str, new Font("宋体", 10f), Brushes.Black,
new PointF(bmp.Width - 10 * move / 1.5f - 100, bmp.Height - move / 1.5f + 5f));
g.Dispose();
return axis;
}
public static void DrawYLine(Bitmap bmp, double minY, double maxY, int len, int n) // 画出Y轴上的刻度线,从任意值开始
{
/* len表示刻度间隔数目 */
Graphics g = Graphics.FromImage(bmp);
Pen pAxis = new Pen(Brushes.Black, 2);
float move = 50f;
float LenY = bmp.Height - 4 * move;
float tempY;
for (int i = len; i >= 0; i--)
{
tempY = LenY * i / len + move + 50;
PointF px1 = new PointF(move + 30f, tempY);
PointF px2 = new PointF(move + 30f + 4, tempY);
string sx = (maxY - (maxY - minY) * (len - i) / len).ToString(tag);
g.DrawLine(pAxis, px1, px2);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Far;
drawFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sx, new Font("宋体", 8f), Brushes.Black,
new PointF(move / 1.2f + 30f, tempY), drawFormat);
}
Pen pen = new Pen(Color.Black, 1);
string str = "位移";
g.DrawString(str, new Font("宋体", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
g.Dispose();
}