1. For details and ideas, refer to the previous article.
Http://www.cnblogs.com/XuebinDing/archive/2012/03/29/2422980.html
2. Front-end code
3. Background code
3.1 add and reference Microsoft Office Web Components 11.0 to the project.
3.2 reference in the file as follows
Using Microsoft. Office. InterOP. owc11; // owc component
Using system. Data. SQL;
Using system. Data. sqlclient; using system. configuration;
Using system. Data;
3.3 specific program code
1 namespace generatecharts
2 {
3 Public partial class pie: system. Web. UI. Page
4 {
5 protected void page_load (Object sender, eventargs E)
6 {
7 // create a chart Space
8 chartspace MySpace = new chartspace ();
9 // Add a chart object
10 chchart MyChart = MySpace. charts. Add (0 );
11 MyChart. type = chartcharttypeenum. chcharttypepie; // pie
12 // MyChart. type = chartcharttypeenum. chcharttypebarclustered; // bar chart
13 // MyChart. type = chartcharttypeenum. chcharttypeline; // line chart
14 // MyChart. type = chartcharttypeenum. chcharttypearea; // Area Chart
15 // MyChart. type = chartcharttypeenum. chcharttypearea3d; // 3D Area Chart
16 // MyChart. type = chartcharttypeenum. chcharttypebar3d; // 3D bar chart
17 // MyChart. type = chartcharttypeenum. chcharttypecolumn3d; // 3D Column Layout
18 //...
19
20 // set related table attributes
21 MyChart. haslegend = true;
22 MyChart. hastitle = true;
23 MyChart. Title. Caption = "employee information Chart ";
24
25 // connect to and open the database
26 sqlconnection sqlcon = new sqlconnection (configurationmanager. connectionstrings ["connection"]. connectionstring );
27 sqlcon. open ();
28 string strsqls = "select nationality, count (nationality) as number from tb_ygxx group by nationality ";
29 sqldataadapter adsa = new sqldataadapter (strsqls, sqlcon );
30 dataset adds = new dataset ();
31 adsa. Fill (ADDs );
32
33 If (adds. Tables [0]. Rows. Count> 0)
34 {
35 string strdataname = "";
36 string strdata = "";
37 // Add a chart Block
38 MyChart. seriescollection. Add (0 );
39 // Add chart data
40 for (Int J = 0; j <adds. Tables [0]. Rows. Count; j ++)
41 {
42 if (j = adds. Tables [0]. Rows. Count-1)
43 {
44 strdataname + = adds. Tables [0]. Rows [J] [0]. tostring ();
45}
46 else
47 {
48 strdataname + = adds. Tables [0]. Rows [J] [0]. tostring () + "\ t ";
49}
50 strdata + = adds. Tables [0]. Rows [J] [1]. tostring () + "\ t ";
51}
52 // set chart block attributes
53 MyChart. seriescollection [0]. setdata (chartdimensionsenum. chdimcategories, (INT) chartspecialperformancesenum. chdataliteral, strdataname );
54 MyChart. seriescollection [0]. setdata (chartdimensionsenum. chdimvalues, (INT) chartspecialperformancesenum. chdataliteral, strdata );
55 // set the percentage
56 chdatalabels labels = MyChart. seriescollection [0]. datalabelscollection. Add ();
57 labels. haspercentage = true;
58}
59 sqlcon. Close ();
60 // generate a chart
61 MySpace. exportpicture (server. mappath (".") + @ "\ temp.gif", "GIF", 600,450 );
62}
63}
64}
4.