以VS2008開發為例,在工具箱也中右擊,選擇”choose items“,打開對話框,選擇COM組件Tab頁,勾選上Microsoft Chart Control, version 6.0(OLEDB),然后在窗體中拖入此控件即可(默認名稱為Chart1)。
關鍵的難點有對MSChart賦值,X、Y軸划分刻度以及生產圖例。下面結合開發實例,稍作總結下:
標題設置:this.Chart1.Title.Text = "tableName";
圖例設置:for (int c = 1; c < iCount + 1; c++)
this.Chart1.Plot.SeriesCollection[Convert.ToInt16(c)].LegendText = legend[c - 1];
this.Chart1.ShowLegend = true;
注:圖例的下標從1開始
圖表類型的選擇:this.Chart1.chartType = VtChChartType.VtChChartType2dBar;(VtChChartType2dPie/VtChChartType2dLine)
圖表內容綁定:this.Chart1.ChartData = arrResult;(一維數組、二維數組)
X、Y軸顯示值間距:
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1.0; //X軸數據顯示間隔
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalType = DateTimeIntervalType.Days;
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 0.0;
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffsetType = DateTimeIntervalType.Days;
Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "M-d";
Chart1.ChartAreas["ChartArea1"].AxisY.Interval = 200;//y軸數據顯示間隔
貼一張統計的效果圖:

網絡查找的其他關於MSChart的屬性方法資料:
最常用的屬性包括
ChartAreas:增加多個繪圖區域,每個繪圖區域包含獨立的圖表組、數據源,用於多個圖表類型在一個繪圖區不兼容時。
AlignmentOrientation:圖表區對齊方向,定義兩個繪圖區域間的對齊方式。
AlignmentStyle:圖表區對齊類型,定義圖表間用以對其的元素。
AlignWithChartArea:參照對齊的繪圖區名稱。
InnerPlotPosition:圖表在繪圖區內的位置屬性。
Auto:是否自動對齊。
Height:圖表在繪圖區內的高度(百分比,取值在0-100)
Width:圖表在繪圖區內的寬度(百分比,取值在0-100)
X,Y:圖表在繪圖區內左上角坐標
Position:繪圖區位置屬性,同InnerPlotPosition。
Name:繪圖區名稱。
Axis:坐標軸集合
Title:坐標軸標題
TitleAlignment:坐標軸標題對齊方式
Interval:軸刻度間隔大小
IntervalOffset:軸刻度偏移量大小
MinorGrid:次要輔助線
MinorTickMark:次要刻度線
MajorGrid:主要輔助線
MajorTickMark:主要刻度線
DataSourceID:MSChart的數據源。
Legends:圖例說明。
Palette:圖表外觀定義。
Series:最重要的屬性,圖表集合,就是最終看到的餅圖 pie、柱狀圖 Column 、線圖 line、點圖 point ,雷達圖 Radar ,
極坐標圖 Polar。錐形圖 ChartType="Pyramid" 等構成的集合;可以將多種相互兼容的類型放在一個繪圖區域內,形成復合圖。
IsValueShownAsLabel:是否顯示數據點標簽,如果為true,在圖表中顯示每一個數據值
Label:數據點標簽文本
LabelFormat:數據點標簽文本格式
LabelAngle:標簽字體角度(有時候如果 標簽過多,這個值可以設置為-30 或30)
Name:圖表名稱
Points:數據點集合
XValueType:橫坐標軸類型
YValueType:縱坐標軸類型
XValueMember:橫坐標綁定的數據源(如果數據源為Table,則填寫橫坐標要顯示的字段名稱)
YValueMembers:縱坐標綁定的數據源(如果數據源為Table,則填寫縱坐標要顯示的字段名稱,縱坐標可以有兩個)
ChartArea:圖表所屬的繪圖區域名稱
ChartType:圖表類型(柱形、餅形、線形、點形等)
Legend:圖表使用的圖例名稱
Titles:標題集合。
width:MSChart的寬度。
height:MSChart的高度。
案例代碼。1 四象限圖
<chartareas> <asp:chartarea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="Gainsboro" ShadowColor="Transparent" BackGradientStyle="TopBottom"> <area3dstyle Rotation="10" 旋轉角度 perspective="10" Inclination="15" IsRightAngleAxes="False" wallwidth="0" IsClustered="False" ></area3dstyle> <axisy linecolor="64, 64, 64, 64" IsLabelAutoFit="False" IsStartedFromZero="false" Crossing="8" y軸原點位置> <labelstyle font="Trebuchet MS, 8.25pt, style=Bold" /> <majorgrid linecolor="64, 64, 64, 64" Enabled="false" /> </axisy> <axisx linecolor="64, 64, 64, 64" IsLabelAutoFit="False" IsStartedFromZero="false" 是否從0點開始 Interval="auto" Minimum="70"X軸最小顯示 Crossing="90" X軸原點位置 > <labelstyle font="Trebuchet MS, 8.25pt, style=Bold" /> <majorgrid linecolor="64, 64, 64, 64" Enabled="false" 是否啟用網格/> </axisx> </asp:chartarea> </chartareas>
設置最大值和最小值不同顏色演示
// Find point with maximum Y value and change color if (Chart_TJ.Series[0].Points.Count > 2) { DataPoint maxValuePoint = Chart_TJ.Series[0].Points.FindMaxByValue(); maxValuePoint.Color = Color.Red; // Find point with minimum Y value and change color DataPoint minValuePoint = Chart_TJ.Series[0].Points.FindMinByValue(); minValuePoint.Color = Color.Orange; }
private void Page_Load(object sender, System.EventArgs e) { // Resolve the address to the Access database string fileNameString = this.MapPath("."); fileNameString += "..\\..\\..\\data\\chartdata.mdb"; // Initialize a connection string string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString; // Define the database query string mySelectQuery="SELECT * FROM REPSALES;"; // Create a database connection object using the connection string OleDbConnection myConnection = new OleDbConnection(myConnectionString); // Create a database command on the connection using query OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection); // Open the connection myCommand.Connection.Open(); // Create a database reader OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // Data bind chart to a table where all rows are grouped in series by the "Name" column Chart1.DataBindCrossTable( myReader, "Name", "Year", "Sales", "Label=Commissions{C}"); // Close the reader and the connection myReader.Close(); myConnection.Close();
}
// Set series appearance
MarkerStyle marker = MarkerStyle.Star4; 這里要注意。線條超過9 的話,最多9個Star。marker.Star 要null下。
foreach(Series ser in Chart1.Series)
{
ser.ShadowOffset = 2;
ser.BorderWidth = 3;
ser.ChartType = SeriesChartType.Line;
ser.MarkerSize = 12;
ser.MarkerStyle = marker;
ser.MarkerBorderColor = Color.FromArgb(64, 64, 64);
ser.Font = new Font("Trebuchet MS", 8, FontStyle.Bold);
marker++;
}
常用事件:
數據綁定方式1
Chart1.Series[0].Points.AddXY(zhiBiaoDt.Rows[i]["TargetName"].ToString(), zhiBiaoDt.Rows[i]["BenDanweiScore"].ToString());
Chart1.Series[0].Points[i - 1].ToolTip = zhiBiaoDt.Rows[i]["BenDanweiScore"].ToString();
Chart1.Series[1].Points.AddXY(zhiBiaoDt.Rows[i]["TargetName"].ToString(), zhiBiaoDt.Rows[i]["QiTaDanweiScore"].ToString());
Chart1.Series[1].Points[i - 1].ToolTip = zhiBiaoDt.Rows[i]["QiTaDanweiScore"].ToString();
//綁定統計報表 添加屬性
Chart_TJ.Series[0].Name = company;
Chart_TJ.Series[0].ChartType = SeriesChartType.Radar;
Chart_TJ.Series[0].IsValueShownAsLabel = false;
// Chart_TJ.Series[0].IsVisibleInLegend = false;
Legend leg = new Legend(); 添加圖例
leg.BackColor = Chart_TJ.Series[0].Color;
leg.Name = company;
Chart_TJ.Legends.Add(leg);
Chart1.Series[0]["DrawingStyle"] = "Cylinder"; 圓柱(圓柱體)風格
Chart1.Series[1]["DrawingStyle"] = "Cylinder";
Chart2.Series[0]["LabelStyle"] ="Bottom"; 數據標簽位置 有left right toplefp bottomleft bottomrigth ……
Chart2.Series[1]["LabelStyle"] = "Bottom";
Series1.Points.DataBind()
綁定數據點集合,如果要在一個MSChart控件的一個繪圖區(ChartArea)內添加多個不同數據源的圖表,就用這個主動綁定數據集合的方法。可以將表中指定字段的值綁定到指定的坐標軸上。
MSChart1.DataBind()
給整個MSChart綁定一個數據源,該MSChart中的圖表全部可以使用該數據源作為統計來源。
注意。給圖綁定數據時候,數據類型盡量統一。不要一個是x字符型 Y decimal 。