這幾天我決定花一些時間,逐個研究下.NET中的Chart控件。web
個人IDE版本爲VS2012,.NET框架版本爲.NET 4.5框架
運行本文代碼須要用到命名空間System.Windows.Forms.DataVisualization.Chartingide
一、餅圖(SeriesChartType.Pie)函數
現有一個Chart控件,取名爲chart,在程序Load函數中輸入以下代碼:ui
private void FormMain_Load(object sender, EventArgs e) { //清空chart圖表 chart.ChartAreas.Clear(); //圖表區 chart.Titles.Clear(); //圖表標題 chart.Series.Clear(); //圖表序列 chart.Legends.Clear(); //圖表圖例 //新建chart圖表要素 chart.ChartAreas.Add(new ChartArea("chartArea")); chart.ChartAreas["chartArea"].AxisX.IsMarginVisible = false; chart.ChartAreas["chartArea"].Area3DStyle.Enable3D = false; chart.Titles.Add("某行業各公司市場佔有率調查報告"); //標題 chart.Titles[0].Font = new Font("宋體", 20); chart.Series.Add("data"); chart.Series["data"].ChartType = SeriesChartType.Pie; //圖標類型 chart.Series["data"]["PieLabelStyle"] = "Outside"; chart.Series["data"]["PieLineColor"] = "Black"; chart.Legends.Add(new Legend("legend")); chart.Palette = ChartColorPalette.BrightPastel; //爲chart圖表賦值 //點1 int idxA = chart.Series["data"].Points.AddY(20); DataPoint pointA = chart.Series["data"].Points[idxA]; pointA.Label = "甲公司"; pointA.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點2 int idxB = chart.Series["data"].Points.AddY(15); DataPoint pointB = chart.Series["data"].Points[idxB]; pointB.Label = "乙公司"; pointB.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點3 int idxC = chart.Series["data"].Points.AddY(30); DataPoint pointC = chart.Series["data"].Points[idxC]; pointC.Label = "丙公司"; pointC.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點4 int idxD = chart.Series["data"].Points.AddY(30); DataPoint pointD = chart.Series["data"].Points[idxD]; pointD.Label = "丁公司"; pointD.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點5 int idxE = chart.Series["data"].Points.AddY(85); DataPoint pointE = chart.Series["data"].Points[idxE]; pointE.Label = "戊公司"; pointE.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; }
繪製好的圖像,效果以下:code
將ChartArea的屬性Area3DStyle.Enable3D改成true,效果以下:orm
二、圓環圖(SeriesChartType.Doughnut)get
圖表控件的標識符還用chart不變:it
private void FormMain_Load(object sender, EventArgs e) { //清空chart圖表 chart.ChartAreas.Clear(); //圖表區 chart.Titles.Clear(); //圖表標題 chart.Series.Clear(); //圖表序列 chart.Legends.Clear(); //圖表圖例 //新建chart圖表要素 chart.ChartAreas.Add(new ChartArea("chartArea")); chart.ChartAreas["chartArea"].AxisX.IsMarginVisible = false; chart.ChartAreas["chartArea"].Area3DStyle.Enable3D = false; chart.Titles.Add("某行業各公司市場佔有率調查報告"); chart.Titles[0].Font = new Font("宋體", 20); chart.Series.Add("data"); chart.Series["data"].ChartType = SeriesChartType.Doughnut; //這一行與上個例子不一樣 chart.Series["data"]["PieLabelStyle"] = "Outside"; chart.Series["data"]["PieLineColor"] = "Black"; chart.Legends.Add(new Legend("legend")); chart.Palette = ChartColorPalette.BrightPastel; //爲chart圖表賦值 //點1 int idxA = chart.Series["data"].Points.AddY(20); DataPoint pointA = chart.Series["data"].Points[idxA]; pointA.Label = "甲公司"; pointA.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點2 int idxB = chart.Series["data"].Points.AddY(15); DataPoint pointB = chart.Series["data"].Points[idxB]; pointB.Label = "乙公司"; pointB.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點3 int idxC = chart.Series["data"].Points.AddY(30); DataPoint pointC = chart.Series["data"].Points[idxC]; pointC.Label = "丙公司"; pointC.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點4 int idxD = chart.Series["data"].Points.AddY(30); DataPoint pointD = chart.Series["data"].Points[idxD]; pointD.Label = "丁公司"; pointD.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; //點5 int idxE = chart.Series["data"].Points.AddY(85); DataPoint pointE = chart.Series["data"].Points[idxE]; pointE.Label = "戊公司"; pointE.LegendText = "#LABEL(#VAL) #PERCENT{P2}"; }
繪製好的圖像,效果以下:io
將ChartArea的屬性Area3DStyle.Enable3D改成true,效果以下:
注意:
一、MS Chart支持的繪圖種類能夠參考:
https://msdn.microsoft.com/library/system.web.ui.datavisualization.charting.seriescharttype.aspx
二、除了IDE中所見即所得的編輯可修改的部分外,Chart還有一些自定義屬性,詳見頁面:
https://msdn.microsoft.com/zh-cn/library/dd456764
END