C# chart控件基礎使用

基本介紹:chart(圖表)windows

功能:主要用來繪製折線圖,柱狀圖與餅狀圖,也可達到動態效果(例如做示波器);字體

須要說明this

 一個chart能夠包含多個chartArea。 chartArea是具體的座標區域。 每個chartArea主要包含X軸,Y軸,副X軸(上方),副Y軸(右方),綁定的線條,綁定的圖例。  數據列能夠有許多,只要將線條綁定到chartArea就能夠在對應的chartArea顯示。spa

關於chart類的官方文檔:orm

https://msdn.microsoft.com/zh-cn/library/system.windows.forms.datavisualization.charting.chart(v=vs.110).aspx#%E5%B1%9E%E6%80%A7文檔

 

 chart中全部數據儲存在series類中,對數據的操做也集成在series類下。get

具體請參照官方series類說明:string

https://msdn.microsoft.com/zh-cn/library/system.windows.forms.datavisualization.charting.series(v=vs.100).aspxit

 

chart的使用:io

                chart控件的基本使用包括:

                                                      1.設置圖表基本屬性,包括背景色,樣式等 (必要)

                                                      2.設置圖表標題及其格式

                                                      3.設置X軸Y軸相關屬性

                                                      4.設置圖例相關屬性

                                                      5.數據列設置(必要)

                                                      6.添加數據列,並設置繪圖類型(必要)

一個實例:

            #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 = newTitle();

            //標題內容

            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 = newLegend();

            legend.Alignment = StringAlignment.Center;

            legend.Docking = Docking.Bottom;

            legend.BackColor = Color.Transparent;

 

            this.chart1.Legends.Add(legend);

            #endregion

 數據點設置:

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;

 繪製圖表/折線:

//添加數據點

                this.chart1.Series.Add(series);

//使用折線圖

                this.chart1.Series.ChartType = SeriesChartType.Line;

相關文章
相關標籤/搜索