/// <summary>
/// 制作折线图类
/// </summary>
/// <param name="x">中心点x值</param>
/// <param name="y">中心点y值</param>
/// <param name="xylength">x,y轴长度</param>
/// <param name="xString">x轴说明</param>
/// <param name="yString">y轴说明</param>
/// <param name="xInfo">x数据</param>
/// <param name="yInfo">y数据</param>
/// <param name="maxPoint">从数据库获取y轴最大数据</param>
public Bitmap CreateLine(float x,float y,float xylength,string xString,string yString,string[] xInfo,float[] yInfo,float maxPoint)
{
Bitmap bitmap = new Bitmap(this.Width, this.Height);//定义画布
Graphics g = Graphics.FromImage(bitmap);
g.Clear(Color.White);
PointF p = new PointF(x, y);//中心点
PointF[] xpt = new PointF[3] { new PointF(p.X + xylength + 15, p.Y), new PointF(p.X + xylength, p.Y - 8), new PointF(p.X + xylength, p.Y + 8) };//x轴三角形
PointF[] ypt = new PointF[3] { new PointF(p.X, p.Y - xylength - 15), new PointF(p.X - 8, p.Y - xylength), new PointF(p.X + 8, p.Y - xylength) };//y轴三角形
//x
g.DrawLine(Pens.Black, p.X, p.Y, p.X + xylength, p.Y);
g.DrawPolygon(Pens.Black, xpt);
g.FillPolygon(Brushes.Black, xpt);
g.DrawString(xString, new Font("宋体", 12), new SolidBrush(Color.Red), p.X + xylength + 10, p.Y + 10);
//y
g.DrawLine(Pens.Black, p.X, p.Y - xylength, p.X, p.Y);
g.DrawPolygon(Pens.Black, ypt);
g.FillPolygon(Brushes.Black, ypt);
g.DrawString(yString, new Font("宋体", 12), new SolidBrush(Color.Red), p.X - 40, p.Y - xylength - 30);
//像素点
int point = (int)(p.Y / xInfo.Length);
int ypoint = 0;
if (maxPoint.ToString().Substring(0, 1) == "1")
{
ypoint = 10;
}
else
{
int sale = Convert.ToInt32(maxPoint);
int length = sale.ToString().Length;
string max = maxPoint.ToString().Substring(0, 1);
string cz = "1";
for (int a = 1; a < length; a++)
{
cz += "0";
max += "0";
}
maxPoint = Convert.ToSingle(cz) + Convert.ToSingle(max);
ypoint = (int)(maxPoint / Convert.ToSingle(cz));
}
//画Y刻度
for (int kd = 1; kd <= ypoint; kd++)
{
float kdy = p.Y - (360 / maxPoint) * (maxPoint / ypoint * kd);
g.DrawString(Convert.ToString(maxPoint / ypoint * kd), new Font("宋体", 10), Brushes.Black, p.X - 40, kdy - 5);
g.DrawLine(Pens.Black, p.X - 5, kdy, p.X, kdy);
}
for (int i = 0; i < xInfo.Length; i++)
{
//画x轴项目
g.DrawString(xInfo[i], new Font("宋体", 10), Brushes.Black, new PointF(p.X + i * point - 5, p.Y + 5));
g.DrawEllipse(Pens.Black, p.X + i * point - 1.5f, p.Y - 360 / maxPoint * yInfo[i], 3, 3);
g.FillEllipse(new SolidBrush(Color.Black), p.X + i * point - 1.5f, p.Y - 360 / maxPoint * yInfo[i], 3, 3);
//画数值
g.DrawString(yInfo[i].ToString(), new Font("宋体", 10), Brushes.Black, p.X + i * point, p.Y - 360 / maxPoint * yInfo[i]);
//画折线
if (i + 1 < xInfo.Length)
g.DrawLine(Pens.Red, p.X + i * point, p.Y - 360 / maxPoint * yInfo[i], p.X + (i + 1) * point, p.Y - 360 / maxPoint * yInfo[i + 1]);
}
return bitmap;
}
//在窗体中调用
DataSet ds = DBUitily.ExecuteQuerySql("select * from t_Users");//横坐标数据源
string[] Users = new string[ds.Tables[0].Rows.Count];
float[] Sales = new float[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Users[i] = ds.Tables[0].Rows[i][0].ToString();
Sales[i] = Convert.ToSingle(DBUitily.ExecuteScalarSql("select sum(SaleMoney) from t_Sales where UserId='" + Users[i] + "'"));//纵坐标数据源
}
float maxValue = Convert.ToSingle(DBUitily.ExecuteScalarSql("select Sum(SaleMoney) from t_Sales group by UserId order by Sum(SaleMoney) Desc"));
picChart.Image = CreateLine(300, 450, 400, "人员编号", "销售价格(元)", Users, Sales, maxValue);
效果图片: