ChartSeriesCollection seriesCollection = chart.Series; 提供了向图表插入数据的操作,有点类似 List 如果你只有一条曲线(柱状图),写一个 Add 就行,如果是多条曲线(柱状图),则添加多个 Add 数据即可展示多条曲线(柱状图)
1 #region 曲线
2 builder.MoveToBookmark("双曲线");
3 var shape = builder.InsertChart(ChartType.Line, 430, 210);//插入线形图
4 Chart chart = shape.Chart;
5 ChartSeriesCollection seriesCollection = chart.Series;
6 seriesCollection.Clear();//清除默认
7 chart.Title.Text = "双曲线";
8 chart.AxisX.TickLabelPosition = AxisTickLabelPosition.Low;
9
10 dt = DBHelper.ExecuteDataTable("select top 5 TNAME,sal,sal+100 as SAL1 from TEACHER");
11
12 var categories = new string[dt.Rows.Count];
13 var values = new double[dt.Rows.Count];
14 var values1 = new double[dt.Rows.Count];
15
16 for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
17 {
18 categories[rowIndex] = dt.Rows[rowIndex][0].ToString();
19 values[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][1]);
20 values1[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][2]);
21 }
22
23 chart.Legend.Position = LegendPosition.Top;
24
25 seriesCollection.Add("线条一", categories, values);//添加数据
26 seriesCollection.Add("线条二", categories, values1);//添加数据
27 #endregion
28
29 #region 饼状图
30 dt = DBHelper.ExecuteDataTable("select top 1 sal,sal+100 as SAL1,sal+200 as SAL2 from TEACHER");
31
32 List<CountModel> list1 = new List<CountModel>();
33 for (int i = 0; i < dt.Rows.Count; i++)
34 {
35 list1.Add(new CountModel { mark = "一", num = Convert.ToDouble(dt.Rows[i][0]) });
36 list1.Add(new CountModel { mark = "二", num = Convert.ToDouble(dt.Rows[i][1]) });
37 list1.Add(new CountModel { mark = "三", num = Convert.ToDouble(dt.Rows[i][2]) });
38 }
39
40 builder.MoveToBookmark("饼状图");
41 var shape1 = builder.InsertChart(ChartType.Pie, 430, 210);//插入线形图
42 Chart chart1 = shape1.Chart;
43 ChartSeriesCollection seriesCollection1 = chart1.Series;
44 seriesCollection1.Clear();//清除默认
45 chart1.Title.Text = "饼状图";
46
47 var categories2 = new string[list1.Count];
48 var values2 = new double[list1.Count];
49
50 for (int rowIndex = 0; rowIndex < list1.Count; rowIndex++)
51 {
52 categories2[rowIndex] = list1[rowIndex].mark.ToString();
53 values2[rowIndex] = Convert.ToDouble(list1[rowIndex].num);
54 }
55
56 chart1.Legend.Position = LegendPosition.Top;
57
58 seriesCollection1.Add("饼状图", categories2, values2);//添加数据
59 #endregion
60
61 #region 柱状图
62 dt = DBHelper.ExecuteDataTable("select top 5 tname,sal,sal+100 as SAL1,sal+200 as SAL2 from TEACHER");
63
64 builder.MoveToBookmark("柱状图");
65 var shape2 = builder.InsertChart(ChartType.Column, 430, 210);//插入线形图
66 Chart chart2 = shape2.Chart;
67 ChartSeriesCollection seriesCollection3 = chart2.Series;
68 seriesCollection3.Clear();//清除默认
69 chart2.Title.Text = "柱状图";
70 chart2.AxisX.TickLabelPosition = AxisTickLabelPosition.Low;
71
72 var categories3 = new string[dt.Rows.Count];
73 var values11 = new double[dt.Rows.Count];
74 var values22 = new double[dt.Rows.Count];
75 var values3 = new double[dt.Rows.Count];
76
77 for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
78 {
79 categories3[rowIndex] = dt.Rows[rowIndex][0].ToString();
80 values11[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][1]);
81 values22[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][2]);
82 values3[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][3]);
83 }
84
85 chart2.Legend.Position = LegendPosition.Top;
86
87 seriesCollection3.Add("一", categories3, values11);//添加数据
88 seriesCollection3.Add("二", categories3, values22);//添加数据
89 seriesCollection3.Add("三", categories3, values3);//添加数据
90 #endregion