首先第一步,写一个实体,来从数据库中读取实体数据.
public class StatYear
{
/// <summary>
/// 类型
/// </summary>
public string type { get; set; }
/// <summary>
/// 月份
/// </summary>
public string month { get; set; }
/// <summary>
/// 数量
/// </summary>
public int count { get; set; }
}
根据type类型,来区分多条折线.
/// <summary> /// 分类集合 /// </summary> /// <returns></returns> public Dictionary<string, List<StatYear>> DicStat(List<StatYear> list) { Dictionary<string, List<StatYear>> dic = new Dictionary<string, List<StatYear>>(); foreach (StatYear node in list) { if (string.IsNullOrWhiteSpace(node.type)) { continue; } if (dic.ContainsKey(node.type)) { dic[node.type].Add(node); } else { dic[node.type] = new List<StatYear>(); dic[node.type].Add(node); } } return dic; }
随时来显示处理.生成多条折线图实体类型.
public void Draw()
{
//年某商品走势
List<StatYear> list = new List<StatYear>();
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 12; j++)
{
list.Add(new StatYear() { month = j.ToString() + "月", count = Random(1, (int)zmax.Value), type = "类型" + i.ToString() });
}
}
DrawLine(list, DateTime.Now.ToString("yyyy") + "年各月订单数");
}
调用主方法,来生成多条折线图:
public void DrawLine(List<StatYear> list, string title)
{
int height = this.zpic.Height, width = this.zpic.Width;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
Brush brush1 = new SolidBrush(Color.Blue);
g.DrawString(title, font1, brush1, new PointF((width - title.ToString().Length * 22) / 2, 20));
//计算数量最大值
int countmax = list.Max(x => x.count);
if (countmax.ToString().Length <= 2) { countmax = countmax + (10 - int.Parse(countmax.ToString().Substring(countmax.ToString().Length - 1, 1))); }
else { countmax = countmax + (int.Parse("1".PadRight(countmax.ToString().Length, '0')) - int.Parse(countmax.ToString().Substring(1, countmax.ToString().Length - 1))); }
Font font = new System.Drawing.Font("Arial", 10, 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);
Pen mypen = new Pen(brush, 1);
//坐标距离上下左右距离
int stop = 90, sleft = 60, sbottom = 40, sright = 30;
//画2条主坐标X和Y线条
Pen penxy = new Pen(Color.Blue, 2);
//绘制Y柱主线
g.DrawLine(penxy, sleft, stop, sleft, height - sbottom);
//绘制X柱主线
g.DrawLine(penxy, sleft - 1, height - sbottom, width - sright, height - sbottom);
//Y柱拆分成10条线路,间距为
int yspace = (height - sbottom - stop) / 10;
for (int i = 1; i <= 10; i++)
{
//从底部向上画Y线条
g.DrawLine(mypen, sleft, height - sbottom - yspace * i, width - sright, height - sbottom - yspace * i);
}
//Y柱文字写入
int value = countmax / 10;
for (int i = 0; i <= 10; i++)
{
//从底部向上写文字
g.DrawString((value * i).ToString(), font, Brushes.Red, 25, height - sbottom - yspace * i - 8); //设置文字内容及输出位置
}
//写X坐标线
//因为是多条,所有月份需要固定,坐标也需要固定
string[] month = new string[12];
int xspace = (width - sleft - sright) / month.Length;
for (int i = 1; i <= month.Length; i++)
{
month[i - 1] = i.ToString() + "月";
g.DrawLine(mypen, sleft + xspace * i, stop, sleft + xspace * i, height - sbottom);
g.DrawString(month[i - 1], font, Brushes.Red, sleft + xspace * (i - 1) - 15, stop - 20); //设置文字内容及输出位置
}
//写X柱状效果
Dictionary<string, List<StatYear>> dic = DicStat(list);
int left = 0;
//随机一种颜色
int R = new Random().Next(Random().Next(255));
int G = new Random().Next(Random().Next(255));
int B = new Random().Next(Random().Next(255));
B = (R + G > 400) ? R + G - 400 : B;
B = (B > 255) ? 255 : B;
Color color = Color.FromArgb(R, G, B);
foreach (string key in dic.Keys)
{
List<StatYear> statlist = dic[key];
//未防止其中月份有丢失,可进行判断读取
List<Point> listpoint = new List<Point>();
for (int i = 0; i < month.Length; i++)
{
StatYear statyear = statlist.Find((w) => { return string.Equals(w.month, month[i]); });
if (statyear != null)
{
Point point = new Point();
point.X = sleft + xspace * i;
point.Y = height - sbottom - statyear.count * (yspace * 10) / countmax;
listpoint.Add(point);
}
}
color = GetDarkerColor(color);
System.Drawing.Drawing2D.LinearGradientBrush brushnew = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), color, color, 1.2f, true);
Pen pennew = new Pen(brushnew, 2);
g.DrawLines(pennew, listpoint.ToArray());
left = left + key.Length * 24 + 20;
}
this.zpic.BackgroundImage = image;
}
最终生成的效果如下: