想要根据数据库中记录个数动态决定chart控件的绘图区域,保证它们大小均匀,在网上找了N久么有发现满意答案,纠结了快一天的时间,终于把问题解决了,贴出来供大家分享O(∩_∩)O~
前面的代码中定义一个series对象数组: List private void button1_Click(object sender, EventArgs e) { OCon Mycon = new OCon(); OracleConnection conn = Mycon.getCon(); conn.Open(); string sqlString = \"select count(*) from dotrelation where dotdesc like '2%路基%温度%'\"; num = Mycon.countORL(conn, sqlString);//获得数据库中满足条件的记录数 //MessageBox.Show(\"满足条件的数据记录为\" + num); int heightPer = 100 / num;//计算每个绘图区高度 for (int i = 0; i < num; i++) { string serName = \"test\" + i.ToString(); listSer.Add(new Series(serName));//对象数组 fillData(date);//初始化数组此方法自己定义的 initialSeries((Series)listSer[i], Color.Red, date); chart1.ChartAreas.Add(\"ANOTHER\" + i); //这里是关键!!!!! chart1.ChartAreas[i].Position.Width = 100;//绘图区域在控件中的宽度 100是百分比 chart1.ChartAreas[i].Position.Height = heightPer; chart1.ChartAreas[i].Position.X = 5;//绘图区域在控件中的绝对位置 横坐标 chart1.ChartAreas[i].Position.Y = 3 + heightPer * i;//绘图区域在控件中的绝对位置纵坐标 chart1.ChartAreas[i].AxisX.LineColor = Color.Blue; chart1.ChartAreas[i].AxisY.LineColor = Color.Blue; chart1.ChartAreas[i].AxisX.LineWidth = 2; chart1.ChartAreas[i].AxisY.LineWidth = 2; chart1.ChartAreas[i].AxisY.Title = \"监测值\"; //设置网格线 chart1.ChartAreas[i].AxisX.MajorGrid.LineColor = Color.Blue; chart1.ChartAreas[i].AxisX.MajorGrid.Interval = 2;//网格间隔 chart1.ChartAreas[i].AxisX.MinorGrid.Interval = 2; chart1.ChartAreas[i].AxisY.MajorGrid.LineColor = Color.Blue; chart1.ChartAreas[i].AxisY.MajorGrid.Interval = 2; chart1.ChartAreas[i].AxisY.MinorGrid.Interval = 2; chart1.Series[i].IsValueShownAsLabel = false; //是否显示数据 chart1.Series[i].IsVisibleInLegend = false; //是否显示数据说明 chart1.Series[i].MarkerStyle = MarkerStyle.Circle; //线条上的数据点标志类型 chart1.Series[i].MarkerSize = 3; //标志大小 chart1.Series[i].ChartType = SeriesChartType.Line; //图表类型为曲线 string name = \"ANOTHER\" + i.ToString(); chart1.Series[i].ChartArea = name;//指定绘图区域 chart1.ChartAreas[i].AxisX.LineColor = Color.Blue; //X轴颜色 chart1.ChartAreas[i].AxisY.LineColor = Color.Blue; //Y轴颜色 chart1.ChartAreas[i].AxisX.LineWidth = 2; //X轴宽度 chart1.ChartAreas[i].AxisY.LineWidth = 2; //Y轴宽度 chart1.ChartAreas[i].AxisY.Title = \"人数\"; //Y轴标题 } //fillData(date);//初始化数组 //initialSeries(series1, Color.Red, date); initalChart();//图表初始化 } C#中CHART控件 简单示例: protected void Page_Load(object sender, EventArgs e) { DataTable dt = default(DataTable); dt = CreateDataTable(); //设置图表的数据源 Chart1.DataSource = dt; //设置图表Y轴对应项 Chart1.Series[0].YValueMembers = \"Volume1\"; Chart1.Series[1].YValueMembers = \"Volume2\"; //设置图表X轴对应项 Chart1.Series[0].XValueMember = \"Date\"; //绑定数据 Chart1.DataBind(); } private DataTable CreateDataTable() { //Create a DataTable as the data source of the Chart control DataTable dt = new DataTable(); //Add three columns to the DataTable dt.Columns.Add(\"Date\"); dt.Columns.Add(\"Volume1\"); dt.Columns.Add(\"Volume2\"); DataRow dr; //Add rows to the table which contains some random data for demonstration dr = dt.NewRow(); dr[\"Date\"] = \"Jan\"; dr[\"Volume1\"] = 3731; dr[\"Volume2\"] = 4101; dt.Rows.Add(dr); dr = dt.NewRow(); dr[\"Date\"] = \"Feb\"; dr[\"Volume1\"] = 6024; dr[\"Volume2\"] = 4324; dt.Rows.Add(dr); dr = dt.NewRow(); dr[\"Date\"] = \"Mar\"; dr[\"Volume1\"] = 4935; dr[\"Volume2\"] = 2935; dt.Rows.Add(dr); dr = dt.NewRow(); dr[\"Date\"] = \"Apr\"; dr[\"Volume1\"] = 4466; dr[\"Volume2\"] = 5644; dt.Rows.Add(dr); dr = dt.NewRow(); dr[\"Date\"] = \"May\"; dr[\"Volume1\"] = 5117; dr[\"Volume2\"] = 5671; dt.Rows.Add(dr); dr = dt.NewRow(); dr[\"Date\"] = \"Jun\"; dr[\"Volume1\"] = 3546; dr[\"Volume2\"] = 4646; dt.Rows.Add(dr); return dt; } 浏览页面: private void showChart(Chart chart, double[] value) { string[] xValue = { \"A\", \"B\", \"C\", \"D\" }; //设Θ?置?标括?签? double[] yValue = value; //获?取?要癮显?示?的?值μ chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss; //设Θ?置?图?表括?边?框ò为a浮?雕?效§果? chart.BorderlineDashStyle = ChartDashStyle.Solid; //设Θ?置?图?表括?边?框ò为a实害?线? chart.BorderlineWidth = 1; //设Θ?置?图?表括?边?框ò的?宽í度è chart.Series[0].ChartType = SeriesChartType.Pie; //设Θ?置?图?表括?类え?型í为a饼纘图? chart.Series[0].CustomProperties Θ?置?饼纘图?的?参?数簓 chart.Series[0].Points.DataBindXY(xValue, yValue); //将?数簓据Y绑悒?定¨到?图?表括? } double[] value = { 30, 40, 50, 60 }; //要癮显?示?的?数簓据Y showChart(this.chart1, value); //显?示?图?表括? = \"DoughnutRadius=25, PieDrawingStyle=Concave, CollectedLabel=Other, MinimumRelative\" + \"PieSize=20\"; //设 因篇幅问题不能全部显示,请点此查看更多更全内容