轉載來源:https://blog.csdn.net/u011854789/article/details/82946553
參考鏈接: http://www.cnblogs.com/winshe/articles/6604406.html https://blog.csdn.net/akof1314/article/details/5710866 http://www.360doc.com/content/13/1016/10/9981262_321805184.shtml
在.NET環境下微軟提供了強大了圖表控件,并給多了很多實例,關于圖表的基本元素如下: 在這里插入圖片描述 并且MSDN給出了創建圖表的示例步驟,原文地址:http://msdn.microsoft.com/en-us/library/dd489237.aspx
在這里,我僅作為自己理解的,并實踐的,以中文方式寫出步驟,未免出錯,還請參照MSDN。添加一個圖表控件到應用程序:
1,在設計視圖下,打開“工具箱 ”; 2,在“Data ”分類下,拖動一個圖表控件到設計區域; 3,在其屬性中展開“ChartAreas ”子屬性,彈出的“ChartAreas 集合編輯器 ”中,這里包含了當前圖表控件的所有圖表區,并且注意此時此集合器已經包含了一個圖表區,名稱為"ChartArea1 "; 4,點擊“添加 ”,新增一個圖表區,請注意,在設計區域的圖表縮小到原來的一半大小。這是因為新創建的圖表區是放置在底部,但不包含一個數據系列; 5,在圖表控件的“Series ”子屬性,展開彈出“Series 集合編輯器 ”,注意這里已經包含了一個序列,名稱為”Series1 “; 6,點擊”添加 “,新增一個序列,在其右邊設置其屬性,“ChartArea ”屬性為ChartArea2 ,“ChartType ”屬性為Bar ,“Name ”屬性為BarChart ,點擊“確定 ”完成。此時運行程序未出現任何結果,因為還沒有數據; 添加數據到圖表:
1,打開“Series 集合編輯器 ”,在左邊的”成員“中選擇”Series1 “,在右邊的屬性區域點擊展開”Points “,彈出”DataPoint 集合編輯器 “; 2,點擊”添加 “,在其右邊屬性的”YValues “屬性中輸入值,重復這一步知道添加了5個數據點; 3,點擊”確定 “,然后對”BarChart “序列進行與第2步一樣的操作; 4,”確定 “完成,運行程序可以在圖表控件上看到兩個圖表區,顯示著剛剛創建的兩個序列的數據點。請注意這兩個序列的不同圖表類型。 添加圖例到圖表:
1,在圖表控件屬性中,點擊”Legends “,展開”Legend 集合編輯器 “,如果沒有已存在的一個圖例,那么就添加一個; 2,設置這個圖例屬性,其中”DockedToChartArea “值為”ChartArea1 “,”Docking “值為”Right “,”IsDockedInsideChartArea “值為”False “; 3,再添加一個圖例,設置屬性其中”DockedToChartArea “值為”ChartArea2 “,”Docking “值為”Right “; 4,點擊”確定 “完成。此時圖表區域的第二個圖例顯示為空。默認所有的序列都分配到第一個圖例,你也可以分配每個序列到不同的圖例; 5,打開”Series 集合編輯器 ”,選擇“BarChart ”項,在其“Legend ”屬性中下拉列表選擇“Legend2 ”,完成運行程序。 添加標題到圖表:
1,在圖表控件屬性中,點擊“Titles ”,展開“Title 集合編輯器 ”,點擊“添加 ”,在右邊的屬性“Text ”中輸入圖表的標題; 2,點擊“確定 ”,運行程序看效果。 使用注解標記圖表上的數據點: 1,在圖表控件屬性中,點擊“Annotations ”,展開“Annotation 集合編輯器 ”; 2,點擊“添加 ”的下拉列表,選擇“ArrowAnnotation ”,如圖: 在這里插入圖片描述 3,在右邊的屬性“定位點 ”分類下,下列選擇“AnchorDataPoint ”值為第一個序列的第一個數據點; 4,其具體屬性“Height ”值為-5,“Width ”值為0,“AnchorOffSetY ”值為-2.5; 5,在其屬性“雜項 ”展開“SmartLabelStyle ”子屬性,設置“IsOverlappedHidden ”為”False“; 6,點擊”確定 “,運行程序。 程序效果如圖: 在這里插入圖片描述
最近公司項目要用到Chart圖表控件,這是一個比較老的東西了,目前網絡上似乎已經不太流行這個控件,但是只要配置了相關的屬性,效果還是可以的。前前后后摸索了好久,接下來談談這個件控件最重要的幾個屬性。
1.ChartAreas屬性
ChartAreas屬性指繪圖區,一個控件可以有多個繪圖區,比如我要在同一個控件內顯示餅圖和柱狀圖,肯定不能放在同一個ChartAreas區域內,應該在同一個Chart控件里增加兩個ChartAreas并分別綁定Series對象。所以ChartAreas屬性對應的是一個集合。 1 2.Series屬性
Series屬性就是各種圖表的圖形啦,比如我們要顯示某月的天氣變化,那么應該有這樣兩組數據,一組是天數,一組是每天對應的溫度值,同時綁定到Series對象中,再將Series對象Add()到Chart控件的Series屬性里即可。為了橫向比較,例如我要看本月與上月的天氣曲線變化圖,并同時顯示在同一個ChartAreas中,那該怎么辦呢?很簡單,再實例一個Series對象,將上月的天數數組與溫度值數組綁定到一個新的Series2實例中,再將Series2實例Add()到Chart控件的Series屬性里,此時,Chart控件的第一個繪圖區ChartArea里就會有兩條曲線。 1 3.Legends屬性
Legend就是指一個圖標的圖例,當一個Series屬性有多個Series時,或是一個Series有幾組數據時,為了區分各自的顏色,通常每個Serie對象一種顏色,這就需要用到圖例來指明哪個顏色的代表的是什么數據。如下圖所示: 1 在這里插入圖片描述 在這里插入圖片描述 在這里插入圖片描述 在這里插入圖片描述 4.關于數據的綁定 將要顯示的X軸和Y軸的數據分別放到兩個數組里在, 然后綁定即可。 cht1.Series[0].Points.DataBindXY(x, y);
完整的代碼如下: 復制代碼 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Web.UI.DataVisualization.Charting;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string[] x = new string[] { “南山大隊”, “福田大隊”, “羅湖大隊”, “寶安大隊”, “指揮處”, “大帝科技”, “南山大隊”, “福田大隊”, “羅湖大隊”, “寶安大隊”, “指揮處”, “大帝科技”}; double[] y = new double[] { 541, 574, 345, 854, 684, 257, 541, 574, 345, 854, 684, 257 }; string[] z = new string[] { “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “” };
string[] a = new string[] { "南山大隊", "福田大隊", "羅湖大隊", "寶安大隊", "指揮處", };
double[] b = new double[] { 541, 574, 345, 854, 257 };#region 柱狀圖//標題
cht1.Titles.Add("柱狀圖數據分析");
cht1.Titles[0].ForeColor = Color.White;
cht1.Titles[0].Font = new Font("微軟雅黑", 12f, FontStyle.Regular);
cht1.Titles[0].Alignment = ContentAlignment.TopCenter;
cht1.Titles.Add("合計:25414 宗");
cht1.Titles[1].ForeColor = Color.White;
cht1.Titles[1].Font = new Font("微軟雅黑", 8f, FontStyle.Regular);
cht1.Titles[1].Alignment = ContentAlignment.TopRight;//控件背景
cht1.BackColor = Color.Transparent;
//圖表區背景
cht1.ChartAreas[0].BackColor = Color.Transparent;
cht1.ChartAreas[0].BorderColor = Color.Transparent;
//X軸標簽間距
cht1.ChartAreas[0].AxisX.Interval = 1;
cht1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
cht1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
cht1.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 14f, FontStyle.Regular);
cht1.ChartAreas[0].AxisX.TitleForeColor = Color.White;//X坐標軸顏色
cht1.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
cht1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
cht1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//X坐標軸標題
//cht1.ChartAreas[0].AxisX.Title = "數量(宗)";
//cht1.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
//cht1.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//cht1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
//cht1.ChartAreas[0].AxisX.ToolTip = "數量(宗)";
//X軸網絡線條
cht1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
cht1.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");//Y坐標軸顏色
cht1.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
cht1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
cht1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//Y坐標軸標題
cht1.ChartAreas[0].AxisY.Title = "數量(宗)";
cht1.ChartAreas[0].AxisY.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
cht1.ChartAreas[0].AxisY.TitleForeColor = Color.White;
cht1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
cht1.ChartAreas[0].AxisY.ToolTip = "數量(宗)";
//Y軸網格線條
cht1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
cht1.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");cht1.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
cht1.ChartAreas[0].BackGradientStyle = GradientStyle.TopBottom;
Legend legend = new Legend("legend");
legend.Title = "legendTitle";cht1.Series[0].XValueType = ChartValueType.String; //設置X軸上的值類型
cht1.Series[0].Label = "#VAL"; //設置顯示X Y的值
cht1.Series[0].LabelForeColor = Color.White;
cht1.Series[0].ToolTip = "#VALX:#VAL"; //鼠標移動到對應點顯示數值
cht1.Series[0].ChartType = SeriesChartType.Column; //圖類型(折線)cht1.Series[0].Color = Color.Lime;
cht1.Series[0].LegendText = legend.Name;
cht1.Series[0].IsValueShownAsLabel = true;
cht1.Series[0].LabelForeColor = Color.White;
cht1.Series[0].CustomProperties = "DrawingStyle = Cylinder";
cht1.Legends.Add(legend);
cht1.Legends[0].Position.Auto = false;//綁定數據
cht1.Series[0].Points.DataBindXY(x, y);
cht1.Series[0].Points[0].Color = Color.White;
cht1.Series[0].Palette = ChartColorPalette.Bright;#endregion#region 餅圖//標題
cht2.Titles.Add("餅圖數據分析");
cht2.Titles[0].ForeColor = Color.White;
cht2.Titles[0].Font = new Font("微軟雅黑", 12f, FontStyle.Regular);
cht2.Titles[0].Alignment = ContentAlignment.TopCenter;
cht2.Titles.Add("合計:25412 宗");
cht2.Titles[1].ForeColor = Color.White;
cht2.Titles[1].Font = new Font("微軟雅黑", 8f, FontStyle.Regular);
cht2.Titles[1].Alignment = ContentAlignment.TopRight;//控件背景
cht2.BackColor = Color.Transparent;
//圖表區背景
cht2.ChartAreas[0].BackColor = Color.Transparent;
cht2.ChartAreas[0].BorderColor = Color.Transparent;
//X軸標簽間距
cht2.ChartAreas[0].AxisX.Interval = 1;
cht2.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
cht2.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
cht2.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 14f, FontStyle.Regular);
cht2.ChartAreas[0].AxisX.TitleForeColor = Color.White;//X坐標軸顏色
cht2.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
cht2.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
cht2.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//X坐標軸標題
cht2.ChartAreas[0].AxisX.Title = "數量(宗)";
cht2.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
cht2.ChartAreas[0].AxisX.TitleForeColor = Color.White;
cht2.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
cht2.ChartAreas[0].AxisX.ToolTip = "數量(宗)";
//X軸網絡線條
cht2.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
cht2.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");//Y坐標軸顏色
cht2.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
cht2.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
cht2.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//Y坐標軸標題
cht2.ChartAreas[0].AxisY.Title = "數量(宗)";
cht2.ChartAreas[0].AxisY.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
cht2.ChartAreas[0].AxisY.TitleForeColor = Color.White;
cht2.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
cht2.ChartAreas[0].AxisY.ToolTip = "數量(宗)";
//Y軸網格線條
cht2.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
cht2.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");cht2.ChartAreas[0].AxisY2.LineColor = Color.Transparent;//背景漸變
cht2.ChartAreas[0].BackGradientStyle = GradientStyle.None;//圖例樣式
Legend legend2 = new Legend("#VALX");
legend2.Title = "圖例";
legend2.TitleBackColor = Color.Transparent;
legend2.BackColor = Color.Transparent;
legend2.TitleForeColor = Color.White;
legend2.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
legend2.Font = new Font("微軟雅黑", 8f, FontStyle.Regular);
legend2.ForeColor = Color.White; cht2.Series[0].XValueType = ChartValueType.String; //設置X軸上的值類型
cht2.Series[0].Label = "#VAL"; //設置顯示X Y的值
cht2.Series[0].LabelForeColor = Color.White;
cht2.Series[0].ToolTip = "#VALX:#VAL(宗)"; //鼠標移動到對應點顯示數值
cht2.Series[0].ChartType = SeriesChartType.Pie; //圖類型(折線)cht2.Series[0].Color = Color.Lime;
cht2.Series[0].LegendText = legend2.Name;
cht2.Series[0].IsValueShownAsLabel = true;
cht2.Series[0].LabelForeColor = Color.White;
cht2.Series[0].CustomProperties = "DrawingStyle = Cylinder";
cht2.Series[0].CustomProperties = "PieLabelStyle = Outside";
cht2.Legends.Add(legend2);
cht2.Legends[0].Position.Auto = true;
cht2.Series[0].IsValueShownAsLabel = true;
//是否顯示圖例
cht2.Series[0].IsVisibleInLegend = true;
cht2.Series[0].ShadowOffset = 0;//餅圖折線
cht2.Series[0]["PieLineColor"] = "White";
//綁定數據
cht2.Series[0].Points.DataBindXY(x, y);
cht2.Series[0].Points[0].Color = Color.White;
//綁定顏色
cht2.Series[0].Palette = ChartColorPalette.BrightPastel;#endregion#region Bar圖//標題
cht3.Titles.Add("交通違法行為TOP5");
cht3.Titles[0].ForeColor = Color.White;
cht3.Titles[0].Font = new Font("微軟雅黑", 12f, FontStyle.Regular);
cht3.Titles[0].Alignment = ContentAlignment.TopCenter;
cht3.Titles.Add("合計:25412 宗 ");
cht3.Titles[1].ForeColor = Color.White;
cht3.Titles[1].Font = new Font("微軟雅黑", 8f, FontStyle.Regular);
cht3.Titles[1].Alignment = ContentAlignment.TopRight;//控件背景
cht3.BackColor = Color.Transparent;
//圖表區背景
cht3.ChartAreas[0].BackColor = Color.Transparent;
cht3.ChartAreas[0].BorderColor = Color.Transparent;
//X軸標簽間距
cht3.ChartAreas[0].AxisX.Interval = 1;
cht3.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
cht3.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
cht3.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 14f, FontStyle.Regular);
cht3.ChartAreas[0].AxisX.TitleForeColor = Color.White;//X坐標軸顏色
cht3.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
cht3.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
cht3.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//X坐標軸標題
//cht3.ChartAreas[0].AxisX.Title = "數量(宗)";
//cht3.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
//cht3.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//cht3.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Auto;
//cht3.ChartAreas[0].AxisX.ToolTip = "數量(宗)";
//X軸網絡線條
cht3.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
cht3.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");//Y坐標軸顏色
cht3.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
cht3.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
cht3.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//Y坐標軸標題
//cht3.ChartAreas[0].AxisY.Title = "數量(宗)";
//cht3.ChartAreas[0].AxisY.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
//cht3.ChartAreas[0].AxisY.TitleForeColor = Color.White;
//cht3.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Auto;
//cht3.ChartAreas[0].AxisY.ToolTip = "數量(宗)";
//Y軸網格線條
cht3.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
cht3.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");cht3.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
cht3.ChartAreas[0].AxisX.IsMarginVisible = false;
cht3.ChartAreas[0].Area3DStyle.Enable3D = true;
//背景漸變
cht2.ChartAreas[0].BackGradientStyle = GradientStyle.None;//圖例樣式
Legend legend3 = new Legend("#VALX");
legend3.Title = "圖例";
legend3.TitleBackColor = Color.Transparent;
legend3.BackColor = Color.Transparent;
legend3.TitleForeColor = Color.White;
legend3.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
legend3.Font = new Font("微軟雅黑", 8f, FontStyle.Regular);
legend3.ForeColor = Color.White;cht3.Series[0].XValueType = ChartValueType.String; //設置X軸上的值類型
cht3.Series[0].Label = "#VAL"; //設置顯示X Y的值
cht3.Series[0].LabelForeColor = Color.White;
cht3.Series[0].ToolTip = "#VALX:#VAL(宗)"; //鼠標移動到對應點顯示數值
cht3.Series[0].ChartType = SeriesChartType.Bar; //圖類型(折線)cht3.Series[0].Color = Color.Lime;
//cht3.Series[0].LegendText = legend3.Name;
cht3.Series[0].IsValueShownAsLabel = true;
cht3.Series[0].LabelForeColor = Color.White;
cht3.Series[0].CustomProperties = "DrawingStyle = Cylinder";
cht3.Series[0].CustomProperties = "PieLabelStyle = Outside";
//cht3.Legends.Add(legend3);
//cht3.Legends[0].Position.Auto = true;//是否顯示圖例
cht3.Series[0].IsVisibleInLegend = true;
cht3.Series[0].ShadowOffset = 0;//餅圖折線
cht3.Series[0]["PieLineColor"] = "White";
//綁定數據
cht3.Series[0].Points.DataBindXY(a, b);//cht3.Series[0].Points[0].Color = Color.White;
//綁定顏色
cht3.Series[0].Palette = ChartColorPalette.BrightPastel;//for (int n = 0; n < x.Length; n++)
//{
// int ptIdx = cht3.Series[0].Points.AddY(Convert.ToDouble(y[n]));
// DataPoint pt = this.cht3.Series[0].Points[ptIdx];
// pt.LegendText = x[n] + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 次" + " ]";//右邊標簽列顯示的文字
// pt.Label = x[n] + " " + "#PERCENT{P2}" + " [ " + "#VAL{D} 次" + " ]"; //圓餅外顯示的信息 // // pt.LabelToolTip = "#PERCENT{P2}";
// //pt.LabelBorderColor = Color.Red;//文字背景色
//}#endregion#region 雷達圖
// //標題 cht4.Titles.Add(“交通違法行為TOP5”); cht4.Titles[0].ForeColor = Color.White; cht4.Titles[0].Font = new Font(“微軟雅黑”, 12f, FontStyle.Regular); cht4.Titles[0].Alignment = ContentAlignment.TopCenter; cht4.Titles.Add("合計:25412 宗 "); cht4.Titles[1].ForeColor = Color.White; cht4.Titles[1].Font = new Font(“微軟雅黑”, 8f, FontStyle.Regular); cht4.Titles[1].Alignment = ContentAlignment.TopRight;
//控件背景
cht4.BackColor = Color.Transparent;
cht4.ChartAreas[0].BackColor = Color.Transparent;
cht4.ChartAreas[0].BorderColor = Color.Transparent;
//X軸標簽間距
cht4.ChartAreas[0].AxisX.Interval = 1;
cht4.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
cht4.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
cht4.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 14f, FontStyle.Regular);
cht4.ChartAreas[0].AxisX.TitleForeColor = Color.White;//X坐標軸顏色
cht4.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
cht4.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
cht4.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//X坐標軸標題
//cht4.ChartAreas[0].AxisX.Title = "數量(宗)";
//cht4.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
//cht4.ChartAreas[0].AxisX.TitleForeColor = Color.White;
//cht4.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Auto;
//cht4.ChartAreas[0].AxisX.ToolTip = "數量(宗)";
//X軸網絡線條
cht4.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
cht4.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");//Y坐標軸顏色
cht4.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
cht4.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
cht4.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微軟雅黑", 10f, FontStyle.Regular);
//Y坐標軸標題
//cht4.ChartAreas[0].AxisY.Title = "數量(宗)";
//cht4.ChartAreas[0].AxisY.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
//cht4.ChartAreas[0].AxisY.TitleForeColor = Color.White;
//cht4.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Auto;
//cht4.ChartAreas[0].AxisY.ToolTip = "數量(宗)";
//Y軸網格線條
cht4.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
cht4.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");cht4.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
cht4.ChartAreas[0].AxisX.IsMarginVisible = false;
cht4.ChartAreas[0].Area3DStyle.Enable3D = true;
cht4.ChartAreas[0].AxisX.IsInterlaced = false;
cht4.ChartAreas[0].AxisX.IsMarginVisible = false;
//刻度線
cht4.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
//cht4.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
//cht4.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
//cht4.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
cht4.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
//背景漸變
cht4.ChartAreas[0].BackGradientStyle = GradientStyle.None;
//cht4.ChartAreas[0].AxisX2.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].BorderWidth = 0;
//cht4.ChartAreas[0].BackSecondaryColor = Color.Red;
//cht4.ChartAreas[0].BackImageTransparentColor = Color.Red;
//cht4.ChartAreas[0].AxisX.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisX.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.MajorGrid.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.MajorTickMark.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisX2.MinorTickMark.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisY.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.InterlacedColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.MajorGrid.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.MajorTickMark.LineColor = Color.Red;
//cht4.ChartAreas[0].AxisY2.MinorTickMark.LineColor = Color.Red;//圖例樣式
Legend legend4 = new Legend();
legend4.Title = "圖例";
legend4.TitleBackColor = Color.Transparent;
legend4.BackColor = Color.Transparent;
legend4.TitleForeColor = Color.White;
legend4.TitleFont = new Font("微軟雅黑", 10f, FontStyle.Regular);
legend4.Font = new Font("微軟雅黑", 8f, FontStyle.Regular);
legend4.ForeColor = Color.White;
cht4.Legends.Add(legend4);
cht4.Legends[0].Position.Auto = true;//Series1
cht4.Series[0].XValueType = ChartValueType.String;
cht4.Series[0].Label = "#VAL";
cht4.Series[0].LabelForeColor = Color.White;
cht4.Series[0].ToolTip = "#LEGENDTEXT:#VAL(宗)";
cht4.Series[0].ChartType = SeriesChartType.Radar;
cht4.Series[0]["RadarDrawingStyle"] = "Line";
cht4.Series[0].LegendText = "2015年";
cht4.Series[0].IsValueShownAsLabel = true;//Series2
cht4.Series.Add(new Series("Series2"));
cht4.Series[1].Label = "#VAL";
cht4.Series[1].LabelForeColor = Color.White;
cht4.Series[1].ToolTip = "#LEGENDTEXT:#VAL(宗)";
cht4.Series[1].ChartType = SeriesChartType.Radar;
cht4.Series[1]["RadarDrawingStyle"] = "Line";
cht4.Series[1].LegendText = "2016年";
cht4.Series[1].IsValueShownAsLabel = true;//Series3
cht4.Series.Add(new Series("Series3"));
cht4.Series[2].Label = "#VAL";
cht4.Series[2].LabelForeColor = Color.White;
cht4.Series[2].ToolTip = "#LEGENDTEXT:#VAL(宗)";
cht4.Series[2].ChartType = SeriesChartType.Radar;
cht4.Series[2]["RadarDrawingStyle"] = "Line";
cht4.Series[2].LegendText = "2017年";
cht4.Series[2].IsValueShownAsLabel = true;double[] yValues = { 65.62, 75.54, 60.45, 34.73, 85.42, 55.9, 63.6, 55.2, 77.1 };
string[] xValues = { "France", "Canada", "Germany", "USA", "Italy", "Spain", "Russia", "Sweden", "Japan" };//Seris2
double[] y2 = { 45.62, 65.54, 70.45, 84.73, 35.42, 55.9, 63.6 };
double[] y3 = { 88.62, 35.54, 52.45, 45.73, 88.42, 14.9, 33.6 };
this.cht4.Series[0].Points.DataBindXY(xValues, yValues);
this.cht4.Series[1].Points.DataBindY(y2);
this.cht4.Series[2].Points.DataBindY(y3);//設置X軸顯示間隔為1,X軸數據比較多的時候比較有用
cht4.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
//設置XY軸標題的名稱所在位置位遠
cht4.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;for (int i = 0; i < cht4.Series[2].Points.Count; i++)
{cht4.Series[2].Points[i].MarkerStyle = MarkerStyle.Circle;//設置折點的風格 cht4.Series[2].Points[i].MarkerColor = Color.Red;//設置seires中折點的顏色
// cht4.Series[1].Points[i].MarkerStyle = MarkerStyle.Square;//設置折點的風格
// cht4.Series[1].Points[i].MarkerColor = Color.Blue;//設置seires中折點的顏色
// cht4.Series[2].Points[i].MarkerStyle = MarkerStyle.Square;//設置折點的風格
// cht4.Series[2].Points[i].MarkerColor = Color.Green;//設置seires中折點的顏色
}
for (int i = 0; i < cht4.Series.Count; i++)
{for (int j = 0; j < cht4.Series[i].Points.Count; j++){cht4.Series[i].Points[j].Label = " ";//cht4.Series[i].Points[j].LabelToolTip = "string.Empty";}
}
//cht4.ImageType = ChartImageType.Jpeg;
//反鋸齒
cht4.AntiAliasing = AntiAliasingStyles.All;
//調色板 磨沙:SemiTransparent
cht4.Palette = ChartColorPalette.BrightPastel;cht4.Series[0].ChartType = SeriesChartType.Radar;
cht4.Series[1].ChartType = SeriesChartType.Radar;
cht4.Series[2].ChartType = SeriesChartType.Radar;
cht4.Width = 500;
cht4.Height = 350;#endregion/* #VALX 顯示當前圖例的X軸的對應文本(或數據) #VAL, #VALY, 顯示當前圖例的Y軸的對應文本(或數據) #VALY2, #VALY3, 顯示當前圖例的輔助Y軸的對應文本(或數據) #SER: 顯示當前圖例的名稱 #LABEL 顯示當前圖例的標簽文本 #INDEX 顯示當前圖例的索引 #PERCENT 顯示當前圖例的所占的百分比 #TOTAL 總數量 #LEGENDTEXT 圖例文本 */
}
創作挑戰賽 新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔 為你收集整理的C#chart控件绘制折线图、柱状图、饼图、雷达图 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。