《C#中CHART画图.docx》由会员分享,可在线阅读,更多相关《C#中CHART画图.docx(7页珍藏版)》请在三一办公上搜索。
1、C#中CHART画图C# chart控件绘制多图表区图像 想要根据数据库中记录个数动态决定chart控件的绘图区域,保证它们大小均匀,在网上找了N久么有发现满意答案,纠结了快一天的时间,终于把问题解决了,贴出来供大家分享O(_)O 前面的代码中定义一个series对象数组: List listSer = new List; 引发动态生成图表的按钮事件完整如下: private void button1_Click(object sender, EventArgs e) OCon Mycon = new OCon; OracleConnection conn = Mycon.getCon; co
2、nn.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 Seri
3、es(serName);/对象数组 fillData(date);/初始化数组此方法自己定义的 initialSeries(Series)listSeri, Color.Red, date); chart1.ChartAreas.Add(ANOTHER + i); /这里是关键! chart1.ChartAreasi.Position.Width = 100;/绘图区域在控件中的宽度 100是百分比 chart1.ChartAreasi.Position.Height = heightPer; chart1.ChartAreasi.Position.X = 5;/绘图区域在控件中的绝对位置 横
4、坐标 chart1.ChartAreasi.Position.Y = 3 + heightPer * i;/绘图区域在控件中的绝对位置纵坐标 chart1.ChartAreasi.AxisX.LineColor = Color.Blue; chart1.ChartAreasi.AxisY.LineColor = Color.Blue; chart1.ChartAreasi.AxisX.LineWidth = 2; chart1.ChartAreasi.AxisY.LineWidth = 2; chart1.ChartAreasi.AxisY.Title = 监测值; /设置网格线 chart1
5、.ChartAreasi.AxisX.MajorGrid.LineColor = Color.Blue; chart1.ChartAreasi.AxisX.MajorGrid.Interval = 2;/网格间隔 chart1.ChartAreasi.AxisX.MinorGrid.Interval = 2; chart1.ChartAreasi.AxisY.MajorGrid.LineColor = Color.Blue; chart1.ChartAreasi.AxisY.MajorGrid.Interval = 2; chart1.ChartAreasi.AxisY.MinorGrid.I
6、nterval = 2; chart1.Seriesi.IsValueShownAsLabel = false; /是否显示数据 chart1.Seriesi.IsVisibleInLegend = false; /是否显示数据说明 chart1.Seriesi.MarkerStyle = MarkerStyle.Circle; /线条上的数据点标志类型 chart1.Seriesi.MarkerSize = 3; /标志大小 chart1.Seriesi.ChartType = SeriesChartType.Line; /图表类型为曲线 string name = ANOTHER + i.
7、ToString; chart1.Seriesi.ChartArea = name;/指定绘图区域 chart1.ChartAreasi.AxisX.LineColor = Color.Blue; /X轴颜色 chart1.ChartAreasi.AxisY.LineColor = Color.Blue; /Y轴颜色 chart1.ChartAreasi.AxisX.LineWidth = 2; /X轴宽度 chart1.ChartAreasi.AxisY.LineWidth = 2; /Y轴宽度 chart1.ChartAreasi.AxisY.Title = 人数; /Y轴标题 /fill
8、Data(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.Series0.YValueMembers = Volume1; Cha
9、rt1.Series1.YValueMembers = Volume2; /设置图表X轴对应项 Chart1.Series0.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.
10、Add(Volume1); dt.Columns.Add(Volume2); DataRow dr; /Add rows to the table which contains some random data for demonstration dr = dt.NewRow; drDate = Jan; drVolume1 = 3731; drVolume2 = 4101; dt.Rows.Add(dr); dr = dt.NewRow; drDate = Feb; drVolume1 = 6024; drVolume2 = 4324; dt.Rows.Add(dr); dr = dt.Ne
11、wRow; drDate = Mar; drVolume1 = 4935; drVolume2 = 2935; dt.Rows.Add(dr); dr = dt.NewRow; drDate = Apr; drVolume1 = 4466; drVolume2 = 5644; dt.Rows.Add(dr); dr = dt.NewRow; drDate = May; drVolume1 = 5117; drVolume2 = 5671; dt.Rows.Add(dr); dr = dt.NewRow; drDate = Jun; drVolume1 = 3546; drVolume2 = 4
12、646; 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; /设?置?图?
13、表括?边?框为a实害?线? chart.BorderlineWidth = 1; /设?置?图?表括?边?框的?宽度 chart.Series0.ChartType = SeriesChartType.Pie; /设?置?图?表括?类?型为a饼纘图? chart.Series0.CustomProperties ?置?饼纘图?的?参?数簓 chart.Series0.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; /设