1. 定义画布
2.确定画笔
3.定好点的坐标(可以使整数型坐标点对,也可以是浮点数组对。不能使double型)
4.g.drawLine(pen,x,y)//划线
int height = 350, width = 690;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("宋体", 12, FontStyle.Regular);
Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
Brush brush2 = new SolidBrush(Color.SaddleBrown);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 5, image.Width -10, image.Height -10);
Pen mypen = new Pen(brush, 1);
Pen mypen2 = new Pen(Color.Red, 2);
//绘制线条
//绘制纵向线条
int x = 50;//横坐标初始位置
for (int i = 0; i < 31; i++)
{
g.DrawLine(mypen, x, 50, x, 250);
x = x + 20;
}
Pen mypen1 = new Pen(Color.Blue, 2);
Pen mypen11 = new Pen(Color.Red, 2);
g.DrawLine(mypen1, 50, 50, 50, 250);
//绘制横向线条
int y = 50;//初始位置
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 50, y, 670, y);
y = y + 20;
}
g.DrawLine(mypen1, 50, 250, 670, 250);
x = 45;// 横坐标文字 初始位置
for (int i = 0; i < row_Num; i++)
{
g.DrawString( ChrPici[i].ToString(), font, Brushes.Red, x, 255); //设置文字内容及输出位置
g.DrawString(NumChr_RQ[i].ToString(), font, Brushes.Red, x,270); //设置文字内容及输出位置
x = x + 20;
}
//横坐标名称
g.DrawString("批次日期",font1,Brushes.Blue,250,290);
//y轴
//y轴值内容及输出位置
double[] Y_zhi=new double[10];
for (int i = 0; i < 10; i++) {
Y_zhi[i] =Math.Round( max-yi * i,2);
}
y = 58;
for (int i = 0; i < 10; i++)
{
g.DrawString(Y_zhi[i].ToString(), font, Brushes.Red, 10, y);
y = y + 20;
}
//纵坐标名称
g.DrawString("含", font1, Brushes.Blue, 50, 100);
g.DrawString("水", font1, Brushes.Blue, 50, 120);
g.DrawString("率", font1, Brushes.Blue, 50, 140);
int[] Count_X = new int[31];
for (int i = 0; i < 31; i++)
{
Count_X[i] = 50 + 20 * i;
}
double[] Count_Y = new double[row_Num];
for (int i = 0; i < row_Num; i++) {
Count_Y[i] =(Math.Round(junzhi[i],2)-Y_zhi[9]) * 20 /Math.Round(yi,2);
}
//均值的均值横线
double averge = 0;
for (int i = 0; i < junzhi.Length;i++ ) {
averge +=Math.Round( junzhi[i],2); //四舍五入
}
averge = averge / junzhi.Length;
averge = (averge - Y_zhi[9]) * 20 / Math.Round(yi,2);
Pen mypen3 = new Pen(Color.Green, 1);
g.DrawLine(mypen3, 50, 250 - averge, 50 + 20 * (row_Num - 1), 250 - averge);
for (int t = 0; t < row_Num - 1; t++)
{
g.DrawLine(mypen11, Count_X[t], 250 - Count_Y[t], Count_X[t + 1], 250 - Count_Y[t + 1]);
}