chart控件的使用
本文介紹如何使用工具箱裡的chart控件,繪製多條曲線。效果圖如下:
1.InitializeChart
在窗體中加入chart控件,然後在屬性裡清空ChartAreas、Legends和Series集合,它們會由下面程式碼動態實作。在窗體建構函式裡,實作InitializeChart和DrawSeries方法。
InitializeChart程式碼如下。
public partial class MainFormBERT : Form { public MainFormBERT() { InitializeComponent(); InitializeChart(); DrawSeries(); } public void InitializeChart() { #region 设置图表的属性 //图表的背景色 chart1.BackColor = Color.FromArgb(211, 223, 240); //图表背景色的渐变方式 chart1.BackGradientStyle = GradientStyle.None; //图表的边框颜色、 chart1.BorderlineColor = Color.FromArgb(26, 59, 105); //图表的边框线条样式 chart1.BorderlineDashStyle = ChartDashStyle.Solid; //图表边框线条的宽度 chart1.BorderlineWidth = 2; //图表边框的皮肤 chart1.BorderSkin.SkinStyle = BorderSkinStyle.None; #endregion #region 设置图表的Title Title title = new Title(); //标题内容 title.Text = "BER"; //标题的字体 title.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Regular); //标题字体颜色 //title.ForeColor = Color.FromArgb(26, 59, 105); //标题阴影颜色 //title.ShadowColor = Color.FromArgb(32, 0, 0, 0); //标题阴影偏移量 //title.ShadowOffset = 3; chart1.Titles.Add(title); #endregion #region 设置图表区属性 //图表区的名字 ChartArea chartArea = new ChartArea("Default"); //背景色 chartArea.BackColor = Color.White;// Color.FromArgb(64, 165, 191, 228); //背景渐变方式 chartArea.BackGradientStyle = GradientStyle.None; //渐变和阴影的辅助背景色 chartArea.BackSecondaryColor = Color.White; //边框颜色 chartArea.BorderColor = Color.Blue; //边框线条宽度 chartArea.BorderWidth = 2; //边框线条样式 chartArea.BorderDashStyle = ChartDashStyle.Solid; //阴影颜色 //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 = "time"; //chartArea.AxisY.Title = "count"; //chartArea.AxisX.TitleFont = new System.Drawing.Font("Microsoft Sans Serif", 10, FontStyle.Regular); //chartArea.AxisY.TitleFont = new System.Drawing.Font("Microsoft Sans Serif", 10, FontStyle.Regular); //设置图表区网格横纵线条的颜色和宽度 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; chart1.ChartAreas.Add(chartArea); #endregion #region 图例及图例的位置 Legend legend = new Legend(); legend.Alignment = StringAlignment.Center; legend.Docking = Docking.Bottom; legend.BackColor = Color.Transparent; this.chart1.Legends.Add(legend); #endregion } }
2.設定曲線樣式
類型、顏色和寬度等
private Series SetSeriesStyle(int i) { Series series = new Series(string.Format("Ch{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.None; //线条数据点的大小 //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; case 3: series.Color = Color.FromArgb(220, 12, 128, 232); break; } return series; }
3.繪製曲線
從datatable裡獲取數據,繪製四條曲線。
//绘制曲线 private void DrawSeries() { dt = new TestDataTable(); dt.CreateTable(); for (int i = 0; i < 4; i++) { Series series = this.SetSeriesStyle(i); DataRow[] foundRows; string expression = "Ch = " + i; foundRows = dt.Select(expression); foreach (DataRow row in foundRows) { series.Points.AddXY(row[0], row[2]); } this.chart1.Series.Add(series); } }
4.顯示或隱藏曲線
根據checkbox的狀態顯示和隱藏這四條曲線。
//显示隐藏曲线 private void DisOrPlaySeries(int i) { if (checkBox[i].Checked) { DataRow[] foundRows; string expression = "Ch = " + i; foundRows = dt.Select(expression); foreach (DataRow row in foundRows) { this.chart1.Series[i].Points.AddXY(row[0], row[2]); } } else { this.chart1.Series[i].Points.Clear(); } }
以上就是 從0自學C#08--繪製曲線chart控制的內容,更多相關內容請關注PHP中文網(www.php.cn)!