这几天在做项目时要对数据进行统计分析,所以必须生成一些统计图(如柱形图、饼图、曲线图等),于是我首先想到了微软的owc11控件,owc11控件是microsoft office 图表控件(owc10为ofiiceXP的组件、owc11为office2003的组件,组件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\owc11,帮助文件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\2052),调用它可以生成三维图、柱状图、饼状图、趋势图和误差图等。
这几天在做项目时要对数据进行统计分析,所以必须生成一些统计图(如柱形图、饼图、曲线图等),于是我首先想到了微软的owc11控件,owc11控件是microsoft office 图表控件(owc10为ofiiceXP的组件、owc11为office2003的组件,组件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\owc11,帮助文件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\2052),调用它可以生成三维图、柱状图、饼状图、趋势图和误差图等。
首先必须添加引用owc11:在“com选项卡”中选择“misrosoft office 11.0 object library”或直接选择路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\owc11的owc11;
接下来是自己写的对owc11操作的封装类,以便于在程序里调用。

具体代码及说明如下:
1
using System;
2
using System.Data;
3
using System.Text;
4
using Microsoft.Office.Interop.Owc11;
5
6
namespace FLX.ComplexQuery
7

{
8
/**//// <summary>
9
/// 彭建军
10
/// 根据数据动态生成图形(柱形图、饼图、曲线图)
11
/// 2008-06-19
12
/// </summary>
13
public class ShowChart
14
{
15
属性#region 属性
16
private string _phaysicalimagepath;//图片存放路径
17
private string _title; //图片标题
18
private string _xtitle;//图片x座标名称
19
private string _ytitle;//图片y座标名称
20
private string _seriesname;//图例名称
21
private int _picwidth;//图片宽度
22
private int _pichight;//图片高度
23
private DataTable _datasource;//图片数据源
24
private string strCategory;//图片数据源的分类
25
private string strValue;//图片数据源的值
26
27
/**//// <summary>
28
/// 图片存放路径
29
/// </summary>
30
public string PhaysicalImagePath
31
{
32
set
{_phaysicalimagepath=value;}
33
get
{return _phaysicalimagepath;}
34
}
35
/**//// <summary>
36
/// 图片标题
37
/// </summary>
38
public string Title
39
{
40
set
{_title=value;}
41
get
{return _title;}
42
}
43
/**//// <summary>
44
/// 图片标题
45
/// </summary>
46
public string XTitle
47
{
48
set
{_xtitle=value;}
49
get
{return _xtitle;}
50
}
51
/**//// <summary>
52
/// 图片标题
53
/// </summary>
54
public string YTitle
55
{
56
set
{_ytitle=value;}
57
get
{return _ytitle;}
58
}
59
60
/**//// <summary>
61
/// 图例名称
62
/// </summary>
63
public string SeriesName
64
{
65
set
{_seriesname=value;}
66
get
{return _seriesname;}
67
}
68
/**//// <summary>
69
/// 图片宽度
70
/// </summary>
71
public int PicWidth
72
{
73
set
{_picwidth=value;}
74
get
{return _picwidth;}
75
}
76
/**//// <summary>
77
/// 图片高度
78
/// </summary>
79
public int PicHight
80
{
81
set
{_pichight=value;}
82
get
{return _pichight;}
83
}
84
/**//// <summary>
85
/// 图片数据源
86
/// </summary>
87
public DataTable DataSource
88
{
89
set
90
{
91
_datasource=value;
92
strCategory=GetColumnsStr(_datasource);
93
strValue=GetValueStr(_datasource);
94
}
95
get
{return _datasource;}
96
}
97
/**//// <summary>
98
/// 图片数据源的分类
99
/// </summary>
100
private string GetColumnsStr(DataTable dt)
101
{
102
StringBuilder strList=new StringBuilder();
103
foreach(DataRow r in dt.Rows)
104
{
105
strList.Append(r[0].ToString()+'\t');
106
}
107
return strList.ToString();
108
}
109
/**//// <summary>
110
/// 图片数据源的值
111
/// </summary>
112
private string GetValueStr(DataTable dt)
113
{
114
StringBuilder strList=new StringBuilder();
115
foreach(DataRow r in dt.Rows)
116
{
117
strList.Append(r[1].ToString()+'\t');
118
}
119
return strList.ToString();
120
}
121
122
#endregion
123
124
构造函数#region 构造函数
125
public ShowChart()
126
{
127
//
128
// TODO: 在此处添加构造函数逻辑
129
//
130
}
131
132
public ShowChart(string PhaysicalImagePath,string Title,string XTitle,string YTitle,string SeriesName)
133
{
134
_phaysicalimagepath=PhaysicalImagePath;
135
_title=Title;
136
_xtitle=XTitle;
137
_ytitle=YTitle;
138
_seriesname=SeriesName;
139
}
140
#endregion
141
142
输出柱形图#region 输出柱形图
143
/**//// <summary>
144
/// 柱形图
145
/// </summary>
146
/// <returns></returns>
147
public string CreateColumn()
148
{
149
Microsoft.Office.Interop.Owc11.ChartSpace objCSpace = new Microsoft.Office.Interop.Owc11.ChartSpaceClass();//创建ChartSpace对象来放置图表
150
Microsoft.Office.Interop.Owc11.ChChart objChart = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
151
152
//指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到//Microsoft.Office.Interop.OWC.ChartChartTypeEnum
153
objChart.Type=Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnClustered;
154
155
//指定图表是否需要图例
156
objChart.HasLegend = true;
157
158
//标题
159
objChart.HasTitle = true;
160
objChart.Title.Caption= _title;
161
// objChart.Title.Font.Bold=true;
162
// objChart.Title.Font.Color="blue";
163
164
165
样式设置#region 样式设置
166
167
//旋转
168
// objChart.Rotation = 360;//表示指定三维图表的旋转角度
169
// objChart.Inclination = 10;//表示指定三维图表的视图斜率。有效范围为 -90 到 90
170
171
//背景颜色
172
// objChart.PlotArea.Interior.Color = "red";
173
174
//底座颜色
175
// objChart.PlotArea.Floor.Interior.Color = "green";
176
//
177
// objChart.Overlap = 50;//单个类别中标志之间的重叠量
178
179
#endregion
180
181
//x,y轴的图示说明
182
objChart.Axes[0].HasTitle = true;
183
objChart.Axes[0].Title.Caption = "X : "+this._xtitle;
184
objChart.Axes[1].HasTitle = true;
185
objChart.Axes[1].Title.Caption = "Y : "+this._ytitle;
186
187
188
//添加一个series
189
Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);
190
191
192
//给定series的名字
193
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
194
Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),SeriesName);
195
//给定分类
196
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
197
Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strCategory);
198
//给定值
199
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
200
Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strValue);
201
//表示柱形图上的单个数据标志
202
Microsoft.Office.Interop.Owc11.ChDataLabels dl=objChart.SeriesCollection[0].DataLabelsCollection.Add();
203
dl.HasValue=true;
204
// dl.Position=Microsoft.Office.Interop.Owc11.ChartDataLabelPositionEnum.chLabelPositionOutsideEnd;
205
206
207
string filename=DateTime.Now.ToString("yyyyMMddHHmmssff")+".gif";
208
string strAbsolutePath = _phaysicalimagepath + "\\"+filename;
209
objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _pichight);//输出成GIF文件.
210
211
return filename;
212
213
}
214
#endregion
215
216
输出饼图#region 输出饼图
217
/**//// <summary>
218
/// 饼图
219
/// </summary>
220
/// <returns></returns>
221
public string CreatePie()
222
{
223
Microsoft.Office.Interop.Owc11.ChartSpace objCSpace = new Microsoft.Office.Interop.Owc11.ChartSpaceClass();//创建ChartSpace对象来放置图表
224
Microsoft.Office.Interop.Owc11.ChChart objChart = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
225
226
227
//指定图表的类型
228
objChart.Type=Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie;
229
230
//指定图表是否需要图例
231
objChart.HasLegend = true;
232
233
//标题
234
objChart.HasTitle = true;
235
objChart.Title.Caption= _title;
236
237
238
//添加一个series
239
Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);
240
241
//给定series的名字
242
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
243
Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),SeriesName);
244
//给定分类
245
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
246
Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strCategory);
247
//给定值
248
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
249
Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strValue);
250
251
252
//表示系列或趋势线上的单个数据标志
253
Microsoft.Office.Interop.Owc11.ChDataLabels dl=objChart.SeriesCollection[0].DataLabelsCollection.Add();
254
dl.HasValue=true;
255
dl.HasPercentage=true;
256
//图表绘图区的图例放置在右侧。
257
// dl.Position=Microsoft.Office.Interop.Owc11.ChartDataLabelPositionEnum.chLabelPositionRight;
258
259
string filename=DateTime.Now.Ticks.ToString()+".gif";
260
string strAbsolutePath = _phaysicalimagepath + "\\"+filename;
261
objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _pichight);//输出成GIF文件.
262
263
return filename;
264
}
265
#endregion
266
267
输出曲线图#region 输出曲线图
268
/**//// <summary>
269
/// 曲线图
270
/// </summary>
271
/// <returns></returns>
272
public string CreateLine()
273
{
274
//创建ChartSpace对象来放置图表
275
Microsoft.Office.Interop.Owc11.ChartSpace objCSpace = new Microsoft.Office.Interop.Owc11.ChartSpaceClass();
276
277
//在ChartSpace对象中添加图表,Add方法返回chart对象
278
279
Microsoft.Office.Interop.Owc11.ChChart objChart = objCSpace.Charts.Add (0);
280
objChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeSmoothLine;
281
282
//指定图表是否需要图例
283
objChart.HasLegend = true;
284
285
286
//给定标题
287
objChart.HasTitle = true;
288
objChart.Title.Caption =this._title;
289
290
//给定x,y轴的图示说明
291
objChart.Axes[0].HasTitle = true;
292
objChart.Axes[0].Title.Caption = "X : " +this._xtitle;
293
objChart.Axes[1].HasTitle = true;
294
objChart.Axes[1].Title.Caption = "Y : " +this._ytitle;
295
296
297
//添加一个series
298
objChart.SeriesCollection.Add(0);
299
300
//给定series的名字
301
objChart.SeriesCollection[0].SetData (Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
302
+ (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, SeriesName);
303
304
//给定分类
305
objChart.SeriesCollection[0].SetData (Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
306
+ (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strCategory);
307
308
//给定值
309
objChart.SeriesCollection[0].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,(int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strValue);
310
//表示曲线上的单个数据标志
311
Microsoft.Office.Interop.Owc11.ChDataLabels dl=objChart.SeriesCollection[0].DataLabelsCollection.Add();
312
dl.HasValue=true;
313
314
string filename=DateTime.Now.ToString("yyyyMMddHHmmssff")+".gif";
315
string strAbsolutePath = _phaysicalimagepath + "\\"+filename;
316
objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _pichight);//输出成GIF文件.
317
return filename;
318
}
319
#endregion
320
321
调用说明及范例#region 调用说明及范例
322
// 在要显示统计图的页面代码直接调用,方法类似如下:
323
//
324
// ShowChart chart=new ShowChart(); 创建对象
325
// chart.Title="标题";
326
// chart.XTitle ="月份";
327
// chart.YTitle ="数量";
328
// chart.SeriesName="图例";
329
// string filepath=Server.MapPath(".")+"\\ChartImages";
330
// chart.PhaysicalImagePath=filepath;
331
// chart.PicHight=320;
332
// chart.PicWidth=500;
333
// chart.DataSource=GetData();//这是你的数据源
334
// Response.Write("<img src='" +filepath+"\\"+chart.CreateColumn()+"'/>");
335
// this.Image1.ImageUrl=filepath+"\\"+chart.CreateBar();//显示给图像控件。
336
#endregion
337
}
338
}
339
首先必须添加引用owc11:在“com选项卡”中选择“misrosoft office 11.0 object library”或直接选择路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\owc11的owc11;
接下来是自己写的对owc11操作的封装类,以便于在程序里调用。


1

2

3

4

5

6

7



8


9

10

11

12

13

14



15


16

17

18

19

20

21

22

23

24

25

26

27


28

29

30

31



32



33



34

35


36

37

38

39



40



41



42

43


44

45

46

47



48



49



50

51


52

53

54

55



56



57



58

59

60


61

62

63

64



65



66



67

68


69

70

71

72



73



74



75

76


77

78

79

80



81



82



83

84


85

86

87

88



89

90



91

92

93

94

95



96

97


98

99

100

101



102

103

104



105

106

107

108

109


110

111

112

113



114

115

116



117

118

119

120

121

122

123

124


125

126



127

128

129

130

131

132

133



134

135

136

137

138

139

140

141

142


143


144

145

146

147

148



149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165


166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216


217


218

219

220

221

222



223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267


268


269

270

271

272

273



274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321


322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

效果图展示:
1.柱形图
2.饼图
3.曲线图