Summary of statistical charts using owc

Source: Internet
Author: User
In a recent project, we need to perform statistical analysis on the Data. Therefore, we first thought of Microsoft's owc and encapsulated the owc operations. In Program Is much more convenient.

Let's take a look at it first:

Before using this class, you must first reference the owc DLL!

Publish source code:

Using system;
Using system. Data;
Using system. text;

Using Microsoft. Office. InterOP;

Namespace owccharttest
{
/// <Summary>
/// Encapsulation class that uses owc11 for statistical graph.
/// Li Tianping
/// </Summary>
Public class owcchart11
{

# Region attributes
Private string _ phaysicalimagepath;
Private string _ title;
Private string _ seriesname;
Private int _ picwidth;
Private int _ pichight;
Private able _ datasource;
Private string strcategory;
Private string strvalue;

/// <Summary>
/// Image storage path
/// </Summary>
Public String phaysicalimagepath
{
Set {_ phaysicalimagepath = value ;}
Get {return _ phaysicalimagepath ;}
}
Public String title
{
Set {_ Title = value ;}
Get {return _ title ;}
}
Public String seriesname
{
Set {_ seriesname = value ;}
Get {return _ seriesname ;}
}

Public int picwidth
{
Set {_ picwidth = value ;}
Get {return _ picwidth ;}
}

Public int pichight
{
Set {_ pichight = value ;}
Get {return _ pichight ;}
}
Public datatable datasource
{
Set
{
_ Datasource = value;
Strcategory = getcolumnsstr (_ datasource );
Strvalue = getvaluestr (_ datasource );
}
Get {return _ datasource ;}
}

Private string getcolumnsstr (datatable DT)
{
Stringbuilder strlist = new stringbuilder ();
Foreach (datarow R in DT. Rows)
{
Strlist. append (R [0]. tostring () + '\ t ');
}
Return strlist. tostring ();
}
Private string getvaluestr (datatable DT)
{
Stringbuilder strlist = new stringbuilder ();
Foreach (datarow R in DT. Rows)
{
Strlist. append (R [1]. tostring () + '\ t ');
}
Return strlist. tostring ();
}

# Endregion

Public owcchart11 ()
{
}
Public owcchart11 (string phaysicalimagepath, String title, string seriesname)
{
_ Phaysicalimagepath = phaysicalimagepath;
_ Title = title;
_ Seriesname = seriesname;
}


/// <Summary>
/// Column chart
/// </Summary>
/// <Returns> </returns>
Public String createcolumn ()
{
Microsoft. Office. InterOP. owc11.chartspace objcspace = new Microsoft. Office. InterOP. owc11.chartspaceclass (); // create a chartspace object to place charts
Microsoft. Office. InterOP. owc11.chchart objchart = objcspace. charts. Add (0); // Add a chart to the chartspace object. The add method returns the chart object.

// Specify the chart type. The type is from owc. chartcharttypeenum to the enumerated value // Microsoft. Office. InterOP. owc. chartcharttypeenum
Objchart. type = Microsoft. Office. InterOP. owc11.chartcharttypeenum. chcharttypecolumnclustered;

// Specifies whether a legend is required for a chart.
Objchart. haslegend = true;

// Title
Objchart. hastitle = true;
Objchart. Title. Caption = _ title;
// Objchart. Title. Font. Bold = true;
// Objchart. Title. Font. color = "blue ";

# Region style settings

// Rotate
// Objchart. Rotation = 360; // specifies the Rotation Angle of a three-dimensional chart.
// Objchart. Inclination = 10; // specifies the view slope of a three-dimensional chart. Valid range:-90 to 90

// Background color
// Objchart. plotarea. Interior. color = "red ";

// Base color
// Objchart. plotarea. Floor. Interior. color = "green ";
//
// Objchart. Overlap = 50; // The number of overlapping labels in a single category

# Endregion

// Graph description of the X and Y axes
Objchart. Axes [0]. hastitle = true;
Objchart. Axes [0]. Title. Caption = "X: Category ";
Objchart. Axes [1]. hastitle = true;
Objchart. Axes [1]. Title. Caption = "Y: quantity ";

// Add a series
Microsoft. Office. InterOP. owc11.chseries thischseries = objchart. seriescollection. Add (0 );

// Specify the name of a series.
Thischseries. setdata (Microsoft. Office. InterOP. owc11.chartdimensionsenum. chdimseriesnames,
Microsoft. Office. InterOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), seriesname );
// Specify a category
Thischseries. setdata (Microsoft. Office. InterOP. owc11.chartdimensionsenum. chdimcategories,
Microsoft. Office. InterOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), strcategory );
// Specify the value
Thischseries. setdata (Microsoft. Office. InterOP. owc11.chartdimensionsenum. chdimvalues,
Microsoft. Office. InterOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), strvalue );

Microsoft. Office. InterOP. owc11.chdatalabels DL = objchart. seriescollection [0]. datalabelscollection. Add ();
DL. hasvalue = true;
// DL. Position = Microsoft. Office. InterOP. owc11.chartdatalabelpositionenum. chlabelpositionoutsideend;


String filename = datetime. Now. tostring ("yyyymmddhhmmssff") + ". GIF ";
String strabsolutepath = _ phaysicalimagepath + "\" + filename;
Objcspace. exportpicture (strabsolutepath, "GIF", _ picwidth, _ pichight); // output the image to a GIF file.

Return filename;

}

/// <Summary>
/// Pie chart
/// </Summary>
/// <Returns> </returns>
Public String createpie ()
{
Microsoft. Office. InterOP. owc11.chartspace objcspace = new Microsoft. Office. InterOP. owc11.chartspaceclass (); // create a chartspace object to place charts
Microsoft. Office. InterOP. owc11.chchart objchart = objcspace. charts. Add (0); // Add a chart to the chartspace object. The add method returns the chart object.


// Specify the chart type
Objchart. type = Microsoft. Office. InterOP. owc11.chartcharttypeenum. chcharttypepie;

// Specifies whether a legend is required for a chart.
Objchart. haslegend = true;

// Title
Objchart. hastitle = true;
Objchart. Title. Caption = _ title;


// Add a series
Microsoft. Office. InterOP. owc11.chseries thischseries = objchart. seriescollection. Add (0 );

// specify the name of a Series
thischseries. setdata (Microsoft. office. interOP. owc11.chartdimensionsenum. chdimseriesnames,
Microsoft. office. interOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), seriesname);
// specify a Category
thischseries. setdata (Microsoft. office. interOP. owc11.chartdimensionsenum. chdimcategories,
Microsoft. office. interOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), strcategory);
// set value
thischseries. setdata (Microsoft. office. interOP. owc11.chartdimensionsenum. chdimvalues,
Microsoft. office. interOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), strvalue);

// Represents a single data mark of a series or trend line
Microsoft. Office. InterOP. owc11.chdatalabels DL = objchart. seriescollection [0]. datalabelscollection. Add ();
DL. hasvalue = true;
DL. haspercentage = true;
// Place the legend in the graphic area on the right.
// DL. Position = Microsoft. Office. InterOP. owc11.chartdatalabelpositionenum. chlabelpositionright;

String filename = datetime. Now. ticks. tostring () + ". GIF ";
String strabsolutepath = _ phaysicalimagepath + "\" + filename;
Objcspace. exportpicture (strabsolutepath, "GIF", _ picwidth, _ pichight); // output the image to a GIF file.

Return filename;
}

/// <Summary>
/// Bar chart
/// </Summary>
/// <Returns> </returns>
Public String createbar ()
{
Microsoft. Office. InterOP. owc11.chartspace objcspace = new Microsoft. Office. InterOP. owc11.chartspaceclass (); // create a chartspace object to place charts
Microsoft. Office. InterOP. owc11.chchart objchart = objcspace. charts. Add (0); // Add a chart to the chartspace object. The add method returns the chart object.

// Specify the chart type. The type is from owc. chartcharttypeenum to the enumerated value // Microsoft. Office. InterOP. owc. chartcharttypeenum
Objchart. type = Microsoft. Office. InterOP. owc11.chartcharttypeenum. chcharttypebarclustered;

// Specifies whether a legend is required for a chart.
Objchart. haslegend = true;

// Title
Objchart. hastitle = true;
Objchart. Title. Caption = _ title;
// Objchart. Title. Font. Bold = true;
// Objchart. Title. Font. color = "blue ";

# Region style settings

/// Rotate
// Objchart. Rotation = 360; // specifies the Rotation Angle of a three-dimensional chart.
// Objchart. Inclination = 10; // specifies the view slope of a three-dimensional chart. Valid range:-90 to 90

// Background color
// Objchart. plotarea. Interior. color = "red ";

// Base color
// Objchart. plotarea. Floor. Interior. color = "green ";
//
// Objchart. Overlap = 50; // The number of overlapping labels in a single category

# Endregion

// Graph description of the X and Y axes
Objchart. Axes [0]. hastitle = true;
Objchart. Axes [0]. Title. Caption = "X: Category ";
Objchart. Axes [1]. hastitle = true;
Objchart. Axes [1]. Title. Caption = "Y: quantity ";

// Add a series
Microsoft. Office. InterOP. owc11.chseries thischseries = objchart. seriescollection. Add (0 );

// specify the name of a Series
thischseries. setdata (Microsoft. office. interOP. owc11.chartdimensionsenum. chdimseriesnames,
Microsoft. office. interOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), seriesname);
// specify a Category
thischseries. setdata (Microsoft. office. interOP. owc11.chartdimensionsenum. chdimcategories,
Microsoft. office. interOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), strcategory);
// set value
thischseries. setdata (Microsoft. office. interOP. owc11.chartdimensionsenum. chdimvalues,
Microsoft. office. interOP. owc11.chartspecialperformancesenum. chdataliteral. gethashcode (), strvalue);

Microsoft. Office. InterOP. owc11.chdatalabels DL = objchart. seriescollection [0]. datalabelscollection. Add ();
DL. hasvalue = true;
// DL. Position = Microsoft. Office. InterOP. owc11.chartdatalabelpositionenum. chlabelpositionoutsideend;


String filename = datetime. Now. tostring ("yyyymmddhhmmssff") + ". GIF ";
String strabsolutepath = _ phaysicalimagepath + "\" + filename;
Objcspace. exportpicture (strabsolutepath, "GIF", _ picwidth, _ pichight); // output the image to a GIF file.

Return filename;

}

}
}

On the page where you want to display the Statistical ChartCodeYou can call it directly.

owcchart11 chart = new owcchart11 ();
chart. title = "title";
chart. seriesname = "legend";
string filepath = server. mappath (". ") +" \ chartimages ";
chart. phaysicalimagepath = filepath;
chart. pichight = 320;
chart. picwidth = 500;
chart. datasource = getdata (); // This is your data source
This. image1.imageurl = filepath + "\" + chart. createbar (); // display it to the image control.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.