private void CreateChart(DataTable dt)
{
// xValues 為日期; yValues 為點擊數量;
string[] xValues = new string[dt.Rows.Count];
int[] yValues = new int[dt.Rows.Count];
int rowCount = 0;
foreach (DataRow row in dt.Rows)
{
xValues[rowCount] = row["Date"].ToString();
yValues[rowCount] = Convert.ToInt32(row["Count"]);
rowCount++;
}
Chart1.Legends.Add("Legends1"); //圖例集合說明
Chart1.Legends["Legends1"].DockedToChartArea = "ChartArea1"; //顯示在圖表內
//Chart1.Legends["Legends1"].Docking = Docking.Bottom; //自訂顯示位置
Chart1.Legends["Legends1"].BackColor = Color.FromArgb(235, 235, 235); //背景色
//斜線背景
Chart1.Legends["Legends1"].BackHatchStyle = ChartHatchStyle.DarkDownwardDiagonal;
Chart1.Legends["Legends1"].BorderWidth = 1;
Chart1.Legends["Legends1"].BorderColor = Color.FromArgb(200, 200, 200);
Chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.CollapsibleSpaceThreshold = 20;
Chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.LineColor = Color.Red;
Chart1.ChartAreas["ChartArea1"].BackGradientStyle = GradientStyle.VerticalCenter;
//3D設定
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false; //3D效果
//Chart1.ChartAreas["ChartArea1"].Area3DStyle.IsClustered = true; //並排顯示
//Chart1.ChartAreas["ChartArea1"].Area3DStyle.Rotation = 40; //垂直角度
//Chart1.ChartAreas["ChartArea1"].Area3DStyle.Inclination = 50; //水平角度
//Chart1.ChartAreas["ChartArea1"].Area3DStyle.PointDepth = 30; //數據條深度
//Chart1.ChartAreas["ChartArea1"].Area3DStyle.WallWidth = 0; //外牆寬度
//Chart1.ChartAreas["ChartArea1"].Area3DStyle.LightStyle = LightStyle.Realistic; //光源
Chart1.ChartAreas["ChartArea1"].BackColor = Color.FromArgb(240, 240, 240); //背景色
//Chart1.ChartAreas["ChartArea1"].AxisX.Enabled = AxisEnabled.False; //隱藏 X 標示
//Chart1.ChartAreas["ChartArea1"].AxisY.Enabled = AxisEnabled.False; //隱藏 Y 標示
Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false; //隱藏 X 軸線
//Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false; //隱藏 Y 軸線
//Y 軸線顏色
Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.FromArgb(150, 150, 150);
//X 軸線顏色
Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.FromArgb(150, 150, 150);
// 讓 X 軸的座軸值能完全 show 出來
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsStaggered = false;
// 讓 Y 軸的座軸值能完全 show 出來
//Chart1.ChartAreas["ChartArea1"].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
//Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.IsStaggered = false;
Chart1.Series["Series1"].Legend = "Legends1";
Chart1.Series["Series1"].IsValueShownAsLabel = false; //顯示數據
//設定 Series1-----------------------------------------------------------------------
Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues); // 利用 DataBindXY 分別把數列的 X 及 Y 值繫結至不同的來源
Chart1.Series["Series1"].LegendText = "Hits";
//Chart1.Series["Series1"].LabelFormat = "#,###"; //金錢格式
//顯示X軸資料
//Chart1.Series["Series1"].LegendText = "#VALX";
//顯示Y軸資料
//Chart1.Series["Series1"].Label = "#VALY";
//圖型
Chart1.Series["Series1"].ChartType = SeriesChartType.Line;
//Chart1.Series["Series1"].Color = Color.Blue;
//Chart1.Series["Series1"].IsValueShownAsLabel = true;
//字體顏色設定
//Chart1.Series["Series1"].LabelForeColor = Color.Red;
//Chart1.Series["Series1"].MarkerBorderColor = Color.Silver;
//字體設定
Chart1.Series["Series1"].Font = new System.Drawing.Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold);
Chart1.Series["Series1"].BorderColor = Color.FromArgb(255, 101, 101, 101);
//數值顯示在圓餅外
//Chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
//設定圓餅效果,除 Default 外其他效果3D不適用
////Chart1.Series["Series1"]["PieDrawingStyle"] = "Default";
}
參考資料:
MS Chart Control 學習手記(二) - 圓餅圖
http://www.dotblogs.com.tw/suehilary/archive/2011/10/24/46163.aspx
[修練營_MSChart, C#][005] 簡易的圖表「複製」功能
http://www.dotblogs.com.tw/nobel12/archive/2009/12/31/12750.aspx
ASP.NET 3.5 Chart Code With C# 使用範例
http://www.wretch.cc/blog/jakeuj/16431027
[立体,3D]MS Chart Control 學習手記(一) - 直條圖&橫條圖
http://www.cnblogs.com/-clq/archive/2012/02/09/2344273.html
C# Asp.net chart的圖表設定方式
http://fongzih.blogspot.tw/2011/09/c-aspnet-chart.html
[ASP.NET] MS Chart (2)
http://www.dotblogs.com.tw/shunnien/archive/2013/04/22/102049.aspx
MS Chart Control 學習手記(一) - 直條圖&橫條圖(這篇寫的非常的好)
http://www.dotblogs.com.tw/suehilary/archive/2011/10/22/45430.aspx
chart鼠标悬停时显示数据和checkBox的默认状态
http://blog.csdn.net/lllljz/article/details/7616935
C#chart柱形图时,如何设置可以在柱子顶上显示数值
http://bbs.csdn.net/topics/390522713?page=1#post-395081594
0 意見: