你的位置:首页 > ASP.net教程

[ASP.net教程]winform chart的使用


简单总结了下winform 的chart的绘制曲线图的用法:

private ChartDataModel _axisData;    private Color _borderColor = Color.FromArgb(19, 143, 213);    private Color _xWordColor = Color.FromArgb(19, 143, 213);    public void DrawImage(ChartDataModel model)    {      _axisData = model;      //取得记录数量      int count = model.AreaDataItems[0].DataItems.Count;      //生成Bitmap对像      Bitmap img = new Bitmap(1000, 600);      img.SetResolution(600, 600);      //生成绘图对像      Graphics g = Graphics.FromImage(img);      //定义实线线条的笔      Pen borderPen = new Pen(_borderColor);      //定义虚线线条的笔      Pen dashPen = new Pen(_borderColor);      dashPen.DashStyle = DashStyle.Dash;      //定义大标题字体      Font headFont = new Font("Arial", 12, FontStyle.Bold);      //定义一般字体      Font font = new Font("Arial", 2);      //绘制底色      g.DrawRectangle(new Pen(Color.White, 1000), 0, 0, img.Width, img.Height);      //定义x轴刻度过渡型笔刷      LinearGradientBrush wordBrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), _xWordColor, _xWordColor, 1.2F, true);      //定义蓝色过渡型笔刷      LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);      //绘制大标题      //g.DrawString(model.StockName, Bfont, brush, 40, 5);      #region 绘制间隔线      //绘制竖坐标线      var x_interval = (float)(model.Width / 4);      var leftPoint = new PointF(150, 100);      var rightPoint = new PointF(150 + model.Width, 100);      var bottomPoint = new PointF(150, 100 + model.Height);      //绘制横坐标线      var y_interval = (float)(model.Height / 4);      for (int i = 1; i < 4; i++)      {        //x轴虚线        g.DrawLine(i == 2 ? borderPen : dashPen, leftPoint.X, leftPoint.Y + y_interval * i, rightPoint.X, rightPoint.Y + y_interval * i);        //y轴虚线        g.DrawLine(dashPen, leftPoint.X + x_interval * i, leftPoint.Y, bottomPoint.X + x_interval * i, bottomPoint.Y);      }      #endregion      #region 绘制刻度      //绘制制横坐标标签      string[] xDatas = new string[] { "9:30", "11:30", "15:00" };      for (int i = 0; i < 3; i++)      {        g.DrawString(xDatas[i], font, wordBrush, leftPoint.X + x_interval * i * 2 - 15, bottomPoint.Y);      }      //绘制时间轴坐标标签      var yset = SetYColor("0.0");      for (int i = 0; i < yset.ColorList.Count; i++)      {        g.DrawString(yset.ColorList[i].Value, font,          new SolidBrush(yset.ColorList[i].ForeColor), 80, 100 + y_interval * i - 10);      }      #endregion      #region 绘制坐标轴      //绘制横坐标轴      g.DrawLine(borderPen, leftPoint, rightPoint);      g.DrawLine(borderPen, bottomPoint.X, bottomPoint.Y, rightPoint.X, bottomPoint.Y);      //绘制竖坐标轴      g.DrawLine(borderPen, leftPoint.X, leftPoint.Y, bottomPoint.X, bottomPoint.Y);      g.DrawLine(borderPen, rightPoint.X, rightPoint.Y, rightPoint.X, bottomPoint.Y);      #endregion      //定义曲线转折点      PointF[] p = new PointF[count];      var temp = leftPoint.Y + model.Height;      var changetemp = model.Height / ((float)model.AreaDataItems[0].Max - (float)model.AreaDataItems[0].Min);      for (int i = 0; i < count; i++)      {        p[i].X = leftPoint.X + i * 2;        p[i].Y = temp - changetemp * (model.AreaDataItems[0].DataItems[i].YFData - (float)model.AreaDataItems[0].Min);      }      g.DrawLines(borderPen, p);      for (int i = 0; i < count; i++)      {        g.DrawRectangle(borderPen, p[i].X, p[i].Y, 1, 1);      }      //保存绘制的图片      FileStream file = new FileStream(@"D:\1.jpg", FileMode.Create, FileAccess.ReadWrite);      img.Save(file, ImageFormat.Jpeg);    }    private AxisYSetColor SetYColor(string formatStr)//设置Y轴的文字样式    {      var max = (float)_axisData.AreaDataItems[0].Max;      var center = (float)_axisData.AreaDataItems[0].Center;      var maxcen = (max + center) / 2;      var min = (float)_axisData.AreaDataItems[0].Min;      var mincen = (min + center) / 2;      var yset = new AxisYSetColor();      yset.FormatStr = formatStr;      yset.ChangeColor(max, maxcen, center, mincen, min);      return yset;    }

View Code