利用MsChart控件绘制多曲线图表

openkk 12年前

在.Net4.0框架中,微软已经将Mschart控件集成了进来,以前一直在web下面用过,原来winform下的Mschart控件更加简单更加方便,今天我们用mschart绘制一个多曲线图,话不多说,直接上效果图:

利用MsChart控件绘制多曲线图表

发现,MsChart 的显示效果还是不错的。

代码如下,供大家参考:

public partial class FrmMain : Form      {          public FrmMain()          {              InitializeComponent();              InitializeChart();              this.Load += new EventHandler(FrmMain_Load);              this.myChart.GetToolTipText += new EventHandler(myChart_GetToolTipText);          }            void FrmMain_Load(object sender, EventArgs e)          {              float[][] data = new float[3][];              //第一条数据              data[0] = new float[10] { 1.3f, 2.5f, 2.1f, 3.3f, 2.8f, 3.9f, 4.3f, 3.6f, 4.2f, 3.6f };              //第二条数据              data[1] = new float[12] { -2f, -1.3f, 0.1f, 0.5f, -1.5f, 0.7f, 1f, 1.4f, 1.9f, 2f, 2.6f, 3.1f };              //第三条数据              data[2] = new float[10] { 7.8f, 9.2f, 6.5f, 8.3f, 9.0f, 5.9f, 6.3f, 7.2f, 8.8f, 9.8f };                for (int i = 0; i < data.Length; i++)              {                  //横坐标时间                  DateTime dt = DateTime.Now.Date;                  Series series = this.SetSeriesStyle(i);                  for (int j = 0; j < data[i].Length; j++)                  {                      series.Points.AddXY(dt, data[i][j]);                      dt = dt.AddDays(1);                  }                  this.myChart.Series.Add(series);              }          }            private void myChart_GetToolTipText(object sender, ToolTipEventArgs e)          {              if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)              {                  int i = e.HitTestResult.PointIndex;                  DataPoint dp = e.HitTestResult.Series.Points[i];                  e.Text = string.Format("时间:{0};数值:{1:F1} ", DateTime.FromOADate(dp.XValue),dp.YValues[0]);              }          }            ///          /// 初始化Char控件样式          ///          public void InitializeChart()          {              #region 设置图表的属性              //图表的背景色              myChart.BackColor = Color.FromArgb(211, 223, 240);              //图表背景色的渐变方式              myChart.BackGradientStyle = GradientStyle.TopBottom;              //图表的边框颜色、              myChart.BorderlineColor = Color.FromArgb(26, 59, 105);              //图表的边框线条样式              myChart.BorderlineDashStyle = ChartDashStyle.Solid;              //图表边框线条的宽度              myChart.BorderlineWidth = 2;              //图表边框的皮肤              myChart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;              #endregion                #region 设置图表的Title              Title title = new Title();              //标题内容              title.Text = "多曲线图演示";              //标题的字体              title.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Bold);              //标题字体颜色              title.ForeColor = Color.FromArgb(26, 59, 105);              //标题阴影颜色              title.ShadowColor = Color.FromArgb(32, 0, 0, 0);              //标题阴影偏移量              title.ShadowOffset = 3;                myChart.Titles.Add(title);              #endregion                #region 设置图表区属性              //图表区的名字              ChartArea chartArea = new ChartArea("Default");              //背景色              chartArea.BackColor = Color.FromArgb(64, 165, 191, 228);              //背景渐变方式              chartArea.BackGradientStyle = GradientStyle.TopBottom;              //渐变和阴影的辅助背景色              chartArea.BackSecondaryColor = Color.White;              //边框颜色              chartArea.BorderColor = Color.FromArgb(64, 64, 64, 64);              //阴影颜色              chartArea.ShadowColor = Color.Transparent;                //设置X轴和Y轴线条的颜色和宽度              chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);              chartArea.AxisX.LineWidth = 1;              chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);              chartArea.AxisY.LineWidth = 1;                //设置X轴和Y轴的标题              chartArea.AxisX.Title = "横坐标标题";              chartArea.AxisY.Title = "纵坐标标题";                //设置图表区网格横纵线条的颜色和宽度              chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);              chartArea.AxisX.MajorGrid.LineWidth = 1;              chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);              chartArea.AxisY.MajorGrid.LineWidth = 1;                myChart.ChartAreas.Add(chartArea);              #endregion                #region 图例及图例的位置              Legend legend = new Legend();              legend.Alignment = StringAlignment.Center;              legend.Docking = Docking.Bottom;                this.myChart.Legends.Add(legend);              #endregion          }            //设置Series样式          private Series SetSeriesStyle(int i)          {              Series series = new Series(string.Format("第{0}条数据", i + 1));                //Series的类型              series.ChartType = SeriesChartType.Line;              //Series的边框颜色              series.BorderColor = Color.FromArgb(180, 26, 59, 105);              //线条宽度              series.BorderWidth = 3;              //线条阴影颜色              series.ShadowColor = Color.Black;              //阴影宽度              series.ShadowOffset = 2;              //是否显示数据说明              series.IsVisibleInLegend = true;              //线条上数据点上是否有数据显示              series.IsValueShownAsLabel = false;              //线条上的数据点标志类型              series.MarkerStyle = MarkerStyle.Circle;              //线条数据点的大小              series.MarkerSize = 8;              //线条颜色              switch (i)              {                  case 0:                      series.Color = Color.FromArgb(220, 65, 140, 240);                      break;                  case 1:                      series.Color = Color.FromArgb(220, 224, 64, 10);                      break;                  case 2:                      series.Color = Color.FromArgb(220, 120, 150, 20);                      break;              }              return series;          }      }

另外附上微软Demo的下载地址:http://archive.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=4418

Demo 分为WEB版本和WinForm版本,样式和图表内容很全,希望对大家有所帮助。
出处:http://www.cnblogs.com/lxblog/