View Code
1 public partial class WebForm1 : System.Web.UI.Page 2 { 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 if (!IsPostBack) 6 { 7 CreateChart(); 8 } 9 }10 11 public DataTable GetTempData()12 {13 string strSql = "SELECT * FROM ChartDB";14 SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DEMO;Integrated Security=True");15 SqlDataAdapter da = new SqlDataAdapter(strSql, con);16 DataSet ds = new DataSet();17 da.Fill(ds);18 return ds.Tables[0];19 }20 21 public void CreateChart()22 {23 Chart1.Width = 600;24 Chart1.Height = 400;25 Chart1.BackColor = Color.Azure;26 Chart1.ChartAreas[0].AxisX.LineWidth = 3;27 Chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Red;28 Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Red;29 Chart1.ChartAreas[0].AxisY.LineWidth = 1;30 Chart1.ChartAreas[0].Position.X = 2;31 Chart1.ChartAreas[0].Position.Y = 2;32 Chart1.ChartAreas[0].Position.Height = 98;33 Chart1.ChartAreas[0].Position.Width = 98;34 Chart1.DataSource = GetTempData();35 Series s1 = new Series();36 s1.IsValueShownAsLabel = true;37 s1.ChartType = SeriesChartType.Line;38 s1.MarkerStyle = MarkerStyle.Circle;39 s1.MarkerSize = 6;40 Chart1.Series.Add(s1);41 Series s2 = new Series();42 s2.ChartType = SeriesChartType.Line;43 s2.MarkerStyle = MarkerStyle.Cross;44 45 s2.MarkerSize = 6;46 s2.IsValueShownAsLabel = true;47 Chart1.Series.Add(s2);48 Legend l = new Legend();49 l.Docking = Docking.Right;50 l.Alignment = System.Drawing.StringAlignment.Center;51 l.LegendStyle = LegendStyle.Column;52 l.BackColor = Color.LightGoldenrodYellow;53 l.Position.X = 86;54 l.Position.Y = 5;55 l.Position.Height = 20;56 l.Position.Width = 12;57 Chart1.Legends.Add(l);58 Legend l2 = new Legend();59 l2.Docking = Docking.Bottom;60 l2.Alignment = System.Drawing.StringAlignment.Center;61 l2.LegendStyle = LegendStyle.Row;62 Chart1.Legends.Add(l2);63 Legend l1 = new Legend();64 l1.LegendStyle = LegendStyle.Row;65 l1.Docking = Docking.Bottom;66 l1.Alignment = System.Drawing.StringAlignment.Center;67 Chart1.Legends.Add(l1);68 Chart1.Series[0].ChartType = SeriesChartType.Line;69 Chart1.Series[0].XValueMember = "Worker";70 Chart1.Series[0].YValueMembers = "ProductPrice";71 Chart1.Series[0].LegendText = "日收入";72 Chart1.Series[0].ToolTip = "#VALX\t#VALY";73 Chart1.Series[1].XValueMember = "Worker";74 Chart1.Series[1].YValueMembers = "YearPrice";75 Chart1.Series[1].LegendText = "年收入";76 Chart1.Series[2].XValueMember = "Worker";77 Chart1.Series[2].YValueMembers = "MonthPrice";78 Chart1.Series[2].LegendText = "月收入";79 Chart1.DataBind();80 }81 }