--------------------------------------------- ASP. NET edition -----------------------------
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using Microsoft. Office. InterOP. owc11;
Using system. Data. oledb;
Using system. collections;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Dropdownlist1.items. Add ("chcharttypecolumnclustered ");
Dropdownlist1.items. Add ("chcharttypecolumn3d ");
Dropdownlist1.items. Add ("chcharttypebarclustered ");
Dropdownlist1.items. Add ("chcharttypebar3d ");
Dropdownlist1.items. Add ("chcharttypearea ");
Dropdownlist1.items. Add ("chcharttypearea3d ");
Dropdownlist1.items. Add ("chcharttypedoughnut ");
Dropdownlist1.items. Add ("chcharttypelinestacked ");
Dropdownlist1.items. Add ("chcharttypeline3d ");
Dropdownlist1.items. Add ("chcharttypelinemarkers ");
Dropdownlist1.items. Add ("chcharttypepie ");
Dropdownlist1.items. Add ("chcharttypepie3d ");
Dropdownlist1.items. Add ("chcharttyperadarsmoothline ");
Dropdownlist1.items. Add ("chcharttypesmoothline ");
// Dropdownlist1.selectedindex = 0;
Int num = dropdownlist1.selectedindex;
Oledbdatareader DR = getlist ("select top 10 orderdate, shipvia from orders order by orderid ");
Data_chart (DR, 0, server. mappath ("test.gif"), 800,600 );
Imagemap1.imageurl = "~ /Test.gif ";
}
/// <Summary>
/// Generate a chart
/// </Summary>
/// <Param name = "SDR"> data source (sqldatareader) </param>
/// <Param name = "intcharttypes"> image type </param>
/// <Param name = "filepath"> image path </param>
/// <Param name = "chartwidth"> image width </param>
/// <Param name = "chartheight"> image height </param>
/// <Returns> relative path of the image </returns>
Public String data_chart (oledbdatareader SDR, int intcharttypes, string filepath, int chartwidth, int chartheight)
{
Arraylist [] data_al = new arraylist [SDR. fieldcount];
For (INT I = 0; I <SDR. fieldcount; I ++)
{
Data_al [I] = new arraylist ();
}
While (SDR. Read ())
{
For (INT I = 0; I <SDR. fieldcount; I ++)
{
Data_al [I]. Add (SDR. getvalue (I). tostring ());
}
}
SDR. Close ();
Chartspace objcspace = new chartspaceclass ();
// Add a chart to the chartspace object. The add method returns the chart object.
Chchart objchart = objcspace. charts. Add (0 );
// Specifies whether a legend is required for a chart.
Objchart. haslegend = true;
Objchart. hastitle = true;
Objchart. Title. Caption = "Analysis Chart ";
// Specify the chart type. The type is from owc. chartcharttypeenum
Chartcharttypeenum [] charttypes = new chartcharttypeenum [] {chartcharttypeenum. chcharttypecolumnclustered,
Chartcharttypeenum. chcharttypecolumn3d,
Chartcharttypeenum. chcharttypebarclustered,
Chartcharttypeenum. chcharttypebar3d,
Chartcharttypeenum. chcharttypearea,
Chartcharttypeenum. chcharttypearea3d,
Chartcharttypeenum. chcharttypedoughnut,
Chartcharttypeenum. chcharttypelinestacked,
Chartcharttypeenum. chcharttypeline3d,
Chartcharttypeenum. chcharttypelinemarkers,
Chartcharttypeenum. chcharttypepie,
Chartcharttypeenum. chcharttypepie3d,
Chartcharttypeenum. chcharttyperadarsmoothline,
Chartcharttypeenum. chcharttypesmoothline };
Objchart. type = charttypes [intcharttypes];
// String [] charttypesch = new string [] {"vertical bar chart", "3D vertical bar chart", "Horizontal Bar Chart", "3D horizontal bar chart ", "area Statistical Chart", "3D area Statistical Chart", "Hollow pie chart", "line Statistical Chart", "3D line Statistical Chart", "line Statistical Chart with points", "Pie Chart ", "3D pie chart", "mesh Statistical Chart", "arc Statistical Chart "};
For (INT I = 0; I <data_al.length-1; I ++)
{
// Add a chart to the chartspace object. The add method returns the chart object.
Objchart. seriescollection. Add (I );
Objchart. seriescollection [I]. datalabelscollection. Add ();
String strseriesname = "" + (I + 1 );
// Specify the name of a series.
Objchart. seriescollection [I]. setdata (chartdimensionsenum. chdimseriesnames,
(INT) chartspecialperformancesenum. chdataliteral, strseriesname );
// Specify a category
Objchart. seriescollection [I]. setdata (chartdimensionsenum. chdimcategories,
(INT) chartspecialperformancesenum. chdataliteral, data_al [0]. toarray ());
// Specify the value
Objchart. seriescollection [I]. setdata
(Chartdimensionsenum. chdimvalues,
(INT) chartspecialperformancesenum. chdataliteral, data_al [I + 1]. toarray ());
Objchart. seriescollection [I]. Caption = "example ";
}
// Output as a GIF file.
// String strabsolutepath = filepath + "\ temp \ test.gif ";
Objcspace. exportpicture (filepath, "GIF", chartwidth, chartheight );
// Create the relative path of the GIF file.
String strrelativepath;
// Strrelativepath = @ ".. \ analyse \ temp \ test.gif ";
Strrelativepath = filepath;
Return strrelativepath;
}
Protected void button2_click (Object sender, eventargs E)
{
Int num = dropdownlist1.selectedindex;
Oledbdatareader DR = getlist ("select top 10 orderdate, shipvia from orders order by orderid ");
Data_chart (DR, num, server. mappath ("test.gifs"), 800,600 );
Imagemap1.imageurl = "~ /Test.gif ";
}
Public oledbdatareader getlist (string SQL)
{
Oledbconnection myconnection = new oledbconnection ("provider = sqloledb; server = 192.168.0.15; uid = sa; Pwd = sa; database = northwind ;");
Oledbcommand mycommand = new oledbcommand (SQL, myconnection );
Myconnection. open ();
Oledbdatareader result = mycommand. executereader (commandbehavior. closeconnection );
Return result;
}
}
-------------------------------------- C # edition ----------------------------
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using Microsoft. Office. InterOP. owc11;
Using system. Data. oledb;
Using system. collections;
Namespace owc
{
Public partial class adochart: Form
{
Public adochart ()
{
Initializecomponent ();
// String [] charttypesch = new string [] {"vertical bar chart", "3D vertical bar chart", "Horizontal Bar Chart", "3D horizontal bar chart ", "area Statistical Chart", "3D area Statistical Chart", "Hollow pie chart", "line Statistical Chart", "3D line Statistical Chart", "line Statistical Chart with points", "Pie Chart ", "3D pie chart", "mesh Statistical Chart", "arc Statistical Chart "};
Combobox1.items. Add ("vertical bar chart ");
Combobox1.items. Add ("3D vertical bar chart ");
Combobox1.items. Add ("Horizontal Bar Chart ");
Combobox1.items. Add ("3D horizontal bar chart ");
Combobox1.items. Add ("Regional Statistical Chart ");
Combobox1.items. Add ("3D area Statistical Chart ");
Combobox1.items. Add ("Hollow pie chart ");
Combobox1.items. Add ("line chart ");
Combobox1.items. Add ("3D line chart ");
Combobox1.items. Add ("line with dot Statistical Chart ");
Combobox1.items. Add ("Pie Chart ");
Combobox1.items. Add ("3D pie chart ");
Combobox1.items. Add ("mesh Statistical Chart ");
Combobox1.items. Add ("arc Statistical Chart ");
Combobox1.selectedindex = 0;
}
Private void button#click (Object sender, eventargs E)
{
Int num = combobox1.selectedindex;
Oledbdatareader DR = getlist ("select top 10 orderdate, shipvia from orders order by orderid ");
Data_chart (DR, num, application. startuppath + "test.gif", 800,600 );
MessageBox. Show ("finished ");
}
Public oledbdatareader getlist (string SQL)
{
Oledbconnection myconnection = new oledbconnection ("provider = sqloledb; server = 192.168.0.15; uid = sa; Pwd = sa; database = northwind ;");
Oledbcommand mycommand = new oledbcommand (SQL, myconnection );
Myconnection. open ();
Oledbdatareader result = mycommand. executereader (commandbehavior. closeconnection );
Return result;
}
Public String data_chart (oledbdatareader SDR, int intcharttypes, string filepath, int chartwidth, int chartheight)
{
Arraylist [] data_al = new arraylist [SDR. fieldcount];
For (INT I = 0; I <SDR. fieldcount; I ++)
{
Data_al [I] = new arraylist ();
}
While (SDR. Read ())
{
For (INT I = 0; I <SDR. fieldcount; I ++)
{
Data_al [I]. Add (SDR. getvalue (I). tostring ());
}
}
SDR. Close ();
// Clear the previous legend
Axchartspace1.clear ();
Chartspace objcspace = new chartspaceclass ();
// Add a chart to the chartspace object. The add method returns the chart object.
// Chchart objchart = objcspace. charts. Add (0); // used to save the image
Chchart objchart = axchartspace1.charts. Add (0); // used on the Interface
// Specifies whether a legend is required for a chart.
Objchart. haslegend = true;
Objchart. hastitle = true;
Objchart. Title. Caption = "Analysis Chart ";
// XY axis description
Objchart. Axes [0]. hastitle = true;
Objchart. Axes [0]. Title. Caption = "orderdate ";
Objchart. Axes [1]. hastitle = true;
Objchart. Axes [1]. Title. Caption = "shipvia ";
// Specify the chart type. The type is from owc. chartcharttypeenum
Chartcharttypeenum [] charttypes = new chartcharttypeenum [] {chartcharttypeenum. chcharttypecolumnclustered,
Chartcharttypeenum. chcharttypecolumn3d,
Chartcharttypeenum. chcharttypebarclustered,
Chartcharttypeenum. chcharttypebar3d,
Chartcharttypeenum. chcharttypearea,
Chartcharttypeenum. chcharttypearea3d,
Chartcharttypeenum. chcharttypedoughnut,
Chartcharttypeenum. chcharttypelinestacked,
Chartcharttypeenum. chcharttypeline3d,
Chartcharttypeenum. chcharttypelinemarkers,
Chartcharttypeenum. chcharttypepie,
Chartcharttypeenum. chcharttypepie3d,
Chartcharttypeenum. chcharttyperadarsmoothline,
Chartcharttypeenum. chcharttypesmoothline };
Objchart. type = charttypes [intcharttypes]; // obtain the legend type.
For (INT I = 0; I <data_al.length-1; I ++)
{
// Add a chart to the chartspace object. The add method returns the chart object.
Objchart. seriescollection. Add (I );
Objchart. seriescollection [I]. datalabelscollection. Add ();
String strseriesname = "" + (I + 1 );
// Specify the name of a series.
Objchart. seriescollection [I]. setdata (chartdimensionsenum. chdimseriesnames,
(INT) chartspecialperformancesenum. chdataliteral, strseriesname );
// Specify a category
Objchart. seriescollection [I]. setdata (chartdimensionsenum. chdimcategories,
(INT) chartspecialperformancesenum. chdataliteral, data_al [0]. toarray ());
// Specify the value
Objchart. seriescollection [I]. setdata
(Chartdimensionsenum. chdimvalues,
(INT) chartspecialperformancesenum. chdataliteral, data_al [I + 1]. toarray ());
Objchart. seriescollection [I]. Caption = "example ";
}
// Output as a GIF file.
Objcspace. exportpicture (filepath, "GIF", chartwidth, chartheight );
// Create the relative path of the GIF file.
String strrelativepath;
Strrelativepath = filepath;
Return strrelativepath;
}
}
}