using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Office.Interop;
namespace JWD.Common
{
/// <summary>
/// OWC11统计图封装
/// 金伟达 2006-04-26
/// </summary>
public class OWCChart11
{
#region 属性
/// <summary>
/// 属性
/// </summary>
private string _title;
private string _imagepath;
private int _imgwidth;
private int _imgheight;
private string _seriesname;
private string strCatory;
private string strValue;
private DataTable _datasource;
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public DataTable DataSource
{
get { return _datasource; }
set
{
_datasource = value;
strCatory = GetColumnsStr(_datasource);
strValue = GetValueStr(_datasource);
}
}
public string Title
{
get{return _title;}
set{ _title = value;}
}
public string ImagePath
{
get{return _imagepath;}
set{_imagepath = value;}
}
public int ImgWidth
{
get{return _imgwidth;}
set{_imgwidth = value;}
}
public int ImgHeight
{
get { return _imgheight; }
set { _imgheight = value; }
}
public string SeriesName
{
get { return _seriesname; }
set { _seriesname = value; }
}
#endregion
public OWCChart11()//构造函数
{
}
public OWCChart11(string ImagePath, string Title, string SeriesName)
{
_imagepath = ImagePath;
_title = Title;
_seriesname = SeriesName;
}
public string GetColumnsStr(DataTable dt)
{
StringBuilder datalist = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
datalist.Append(dr["loginfrom"].ToString() + "/t");
}
return datalist.ToString();
}
public string GetValueStr(DataTable dt)
{
StringBuilder datalist = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
datalist.Append(dr["count"].ToString() + "/t");
}
return datalist.ToString();
}
//饼图
public string CreatePie()
{
Microsoft.Office.Interop.Owc11.ChartSpace ObjChartPie = new Microsoft.Office.Interop.Owc11.ChartSpaceClass();//创建ChartSpace对象放置图表
Microsoft.Office.Interop.Owc11.ChChart ObjChart = ObjChartPie.Charts.Add(0);//在ChartSpace对象中添加图标,add方法chart对象
ObjChartPie.Border.Color = "White";//设置图形容器边框
ObjChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie3D;//指定图表类型
ObjChart.HasLegend = true;//图表是否需要图列
ObjChart.Legend.Position = Microsoft.Office.Interop.Owc11.ChartLegendPositionEnum.chLegendPositionRight;//设置图例在右侧显示
ObjChart.HasTitle = true;//图表是否需要标题
ObjChart.Title.Caption = _title;//设置标题
ObjChart.Title.Font.Bold = true;//设置标题是否加粗
ObjChart.Title.Position = Microsoft.Office.Interop.Owc11.ChartTitlePositionEnum.chTitlePositionBottom;//设置标题对齐方式
ObjChart.Title.Font.Color = "red";//设置字体颜色
//添加一个Series
Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = ObjChart.SeriesCollection.Add(0);
//给定名字
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames, Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName);
//给定分类
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strCatory);
//给定值
ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues, Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);
//表示系列或趋势线上的单个数据标志
Microsoft.Office.Interop.Owc11.ChDataLabels cdl = ObjChart.SeriesCollection[0].DataLabelsCollection.Add();
cdl.HasValue = false;
cdl.HasPercentage = true;
cdl.HasCategoryName = true;
cdl.Interior.Color = "red";
//Point:代表图中的一部分,比如柱图的一条柱,饼图的一个扇区
ObjChart.SeriesCollection[0].Points[0].Interior.Color = "#E4FFE3";//设置第一扇区颜色
ObjChart.SeriesCollection[0].Points[1].Interior.Color = "#7DBD7B";//设置第二扇区颜色
ObjChart.SeriesCollection[0].Points[2].Interior.Color = "#51C94F";//设置第三扇区颜色
//Explosion:返回或设置指定饼图或圆环图扇面的分离程度值。有效范围为 0 到 1000。分离程度值等于图表半径的百分比
ObjChart.SeriesCollection[0].Points[0].Explosion = 45;
ObjChart.SeriesCollection[0].Points[1].Explosion = 45;
ObjChart.SeriesCollection[0].Points[2].Explosion = 45;
//给第二个扇区设置从中心向四周辐射的单色渐变
ObjChart.SeriesCollection[0].Points[1].Interior.SetOneColorGradient(Microsoft.Office.Interop.Owc11.ChartGradientStyleEnum.chGradientFromCenter, Microsoft.Office.Interop.Owc11.ChartGradientVariantEnum.chGradientVariantStart, 0.3, "LightGreen");
//给第三个扇区设置倾斜双色渐变
ObjChart.SeriesCollection[0].Points[2].Interior.SetTwoColorGradient(Microsoft.Office.Interop.Owc11.ChartGradientStyleEnum.chGradientDiagonalDown, Microsoft.Office.Interop.Owc11.ChartGradientVariantEnum.chGradientVariantCenter, "Green", "Red");
//给第1个扇区设置OWC预设的纹理,并设置纹理的背景色为淡绿色,前景色为红色
//OWC提供了很多纹理,这是其中一种
//具体的纹理样式可以参看帮助中的ChartPatternTypeEnum枚举
ObjChart.SeriesCollection[0].Points[0].Interior.SetPatterned(Microsoft.Office.Interop.Owc11.ChartPatternTypeEnum.chPatternDiagonalBrick, "Red", "LightGreen");
//给第2个扇区设置OWC预设的倾斜,
//OWC提供了许多种的倾斜,这是其中一种
//具体的倾斜可以参看帮助中的ChartPresetGradientTypeEnum枚举
ObjChart.SeriesCollection[0].Points[1].Interior.SetPresetGradient(Microsoft.Office.Interop.Owc11.ChartGradientStyleEnum.chGradientDiagonalUp, Microsoft.Office.Interop.Owc11.ChartGradientVariantEnum.chGradientVariantEdges, Microsoft.Office.Interop.Owc11.ChartPresetGradientTypeEnum.chGradientGoldII);
string filename = Name + ".gif";
string strAbsolutPath = _imagepath + "//" + filename;
ObjChartPie.ExportPicture(strAbsolutPath, "GIF", _imgwidth, _imgheight);//输出成GIF
return filename;
}
// 在要显示统计图的页面代码直接调用就可以了
//OWCChart11 chart=new OWCChart11();
//chart.Title="标题";
//chart.SeriesName="图例";
//string filepath=Server.MapPath(".")+"//ChartImages";
//chart.PhaysicalImagePath=filepath;
//chart.PicHight=320;
//chart.PicWidth=500;
//chart.DataSource=GetData();//这是你的数据源