MSChart绘图控件中折线图和柱形图画法

首先在前台拖入一个名为chart1的MSChart控件

        
//折线图
       string strLegend = "Legend1"; Legend lg = new Legend(strLegend); lg.IsDockedInsideChartArea = false; lg.TitleAlignment = System.Drawing. StringAlignment.Center; chart1.Legends.Add(lg); ChartArea ca = new ChartArea(); ca.AxisX.MajorGrid.Enabled = false; ca.AxisX.MajorTickMark.Enabled = false; ca.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; int index = 1; Series MyScore = new Series( "我的得分" ); MyScore.ChartType = SeriesChartType.Line; MyScore.Legend = "Legend1"; MyScore.ChartArea = "ChartArea1"; MyScore.BorderWidth = 40; Series HighScore = new Series( "最高分" ); HighScore.ChartType = SeriesChartType.Line; HighScore.Legend = "Legend1"; HighScore.ChartArea = "ChartArea1"; MyScore.BorderWidth = 40; Series AvgScore = new Series( "平均分" ); AvgScore.ChartType = SeriesChartType.Line; AvgScore.Legend = "Legend1"; AvgScore.ChartArea = "ChartArea1"; MyScore.BorderWidth = 40; foreach (DataRow dr in m_table.Rows) { DataPoint dp; if (dr["MyScore" ] == null) continue; dp = new DataPoint (index, Convert.ToDouble(dr["MyScore" ])); dp.AxisLabel = dr[ "Name"].ToString(); dp.Label = Convert.ToInt32(dr["MyScore" ]).ToString(); dp.BorderWidth = 10; dp.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle .Star10; MyScore.Points.Add(dp); dp = new DataPoint (index, Convert.ToDouble(dr["HighScore" ])); dp.AxisLabel = dr[ "Name"].ToString(); dp.Label = Convert.ToInt32(dr["HighScore" ]).ToString(); dp.BorderWidth = 10; dp.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle .Star10; HighScore.Points.Add(dp); dp = new DataPoint (index, Convert.ToDouble(dr["AvgScore" ])); dp.AxisLabel = dr[ "Name"].ToString(); dp.Label = Convert.ToInt32(dr["AvgScore" ]).ToString(); dp.BorderWidth = 10; dp.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle .Star10; AvgScore.Points.Add(dp); index++; } chart1.Series.Add(MyScore); chart1.Series.Add(HighScore); chart1.Series.Add(AvgScore); chart1.ChartAreas.Add(ca);
         
//柱形图
          string
strLegend = "Legend1" ; Legend lg = new Legend(strLegend); lg.IsDockedInsideChartArea = false; lg.TitleAlignment = System.Drawing.StringAlignment .Center; chart1.Legends.Add(lg); ChartArea ca = new ChartArea(); ca.AxisX.MajorGrid.Enabled = false; ca.AxisX.MajorTickMark.Enabled = false; ca.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; int index = 1; foreach (DataRow dr in dt.Rows) { Series s = new Series(dr["CourseTermName" ].ToString()); s.ChartType = SeriesChartType.Column; s.Legend = "Legend1"; s.ChartArea = "ChartArea1"; DataPoint dp; dp = new DataPoint (index, Convert.ToDouble(dr["Score" ])); dp.AxisLabel = dr[ "CourseTermName"].ToString(); dp.Label = Convert.ToInt32(dr["Score" ]).ToString(); dp.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle .Star10; s.Points.Add(dp); chart1.Series.Add(s); index++; } chart1.ChartAreas.Add(ca);
设置序列的BorderWidth可以改变线条的粗细,它是一个int值,值越大,线条越粗;

设置序列的MarkerStyle可以 显示出数据点标记,它是一个枚举,有多个样式,圆点、三角、矩形什么的,甚至可以通过MarkerImage来指定一个图形作为数据点,属性方面可以通过 MarkerSize和MarkerBorderWidth来指定数据点大小,MarkerColor和MarkerBorderColor来指定数据点 颜色。

希望对大家有帮助。以上都是我在项目中实现过的。

原文地址:https://www.cnblogs.com/lzgeveryday/p/4315785.html