using System; using System.Data; using System.Text; using System.Drawing;
namespace doHope.OWCC { /// <summary> /// 封装OWC做统计图 /// by cherish58 /// </summary> public class OWCChart { #region 属性
private string _phaysicalimagepath; private string _title; private string _seriesname; private int _picwidth; private int _pichight; private DataTable _datasource; private string _strdatasource; private string strCategory; private string strValue;
/// <summary> /// 图片存放路径 /// </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; } }
/// <summary> /// 传DataTable类型 /// </summary> public DataTable DataSource { set { _datasource = value; strCategory = GetColumnsStr(_datasource); strValue = GetValueStr(_datasource); } get { return _datasource; } }
/// <summary> /// 传字符串,分隔用分号(;) /// </summary> public string strDataSource { set { _strdatasource = value; strCategory = GetColumnsStr(_strdatasource); strValue = GetValueStr(_strdatasource); } get { return _strdatasource; } }
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 GetColumnsStr(string coldt) { return coldt == "" ? "" : coldt.Substring(0, coldt.IndexOf(";")); }
private string GetValueStr(DataTable dt) { StringBuilder strList = new StringBuilder(); foreach (DataRow r in dt.Rows) { strList.Append(r[1].ToString() + '/t'); } return strList.ToString(); }
private string GetValueStr(string rowdt) { return rowdt == "" ? "" : rowdt.Substring(rowdt.LastIndexOf(";") + 1); }
#endregion #region 构造函数
/// <summary> /// 0 /// </summary> public OWCChart() { // }
/// <summary> /// 1 /// </summary> /// <param name="PhaysicalImagePath"></param> /// <param name="Title"></param> /// <param name="SeriesName"></param> /// <param name="PicWidth"></param> /// <param name="PicHight"></param> public OWCChart(string PhaysicalImagePath, string Title, string SeriesName, int PicWidth, int PicHight) { _phaysicalimagepath = PhaysicalImagePath; _title = Title; _seriesname = SeriesName; _pichight = PicHight; _picwidth = PicWidth; }
/// <summary> /// 2 /// </summary> /// <param name="Title"></param> /// <param name="SeriesName"></param> /// <param name="PicWidth"></param> /// <param name="PicHight"></param> public OWCChart(string Title, string SeriesName, int PicWidth, int PicHight) { _title = Title; _seriesname = SeriesName; _pichight = PicHight; _picwidth = PicWidth; }
#endregion #region 柱形图
/// <summary> /// 柱形图 /// </summary> /// <param name="Xname">X轴描述</param> /// <param name="Yname">Y轴描述</param> /// <param name="IfExportToFile">若把图像输出到文件夹则为true,否则为false</param> /// <returns>若把图像输出到文件夹返回生成的图像名称,否则返回Bitmap类型</returns> public Object CreateColumn(string Xname, string Yname, bool IfExportToFile, bool If3D) { OWC11.ChartSpace objCSpace = new OWC11.ChartSpaceClass();//创建ChartSpace对象来放置图表 OWC11.ChChart objChart = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
//指定图表的类型。类型由OWC11.ChartChartTypeEnum枚举值得到 if (If3D == true) objChart.Type = OWC11.ChartChartTypeEnum.chChartTypeColumnClustered3D; else objChart.Type = OWC11.ChartChartTypeEnum.chChartTypeColumnClustered;
//指定图表是否需要图例 objChart.HasLegend = true;
//标题 objChart.HasTitle = true; objChart.Title.Caption = _title; objChart.Title.Font.Bold = true; // objChart.Title.Font.Color="blue"; #region 样式设置
//旋转 // objChart.Rotation = 145;//表示指定三维图表的旋转角度 // objChart.Inclination = 10;//表示指定三维图表的视图斜率。有效范围为 -90 到 90 // // //背景颜色 // objChart.PlotArea.Interior.Color = "red"; // // //底座颜色 // objChart.PlotArea.Floor.Interior.Color = "green"; // objChart.Overlap = 50;//单个类别中标志之间的重叠量
#endregion
//x,y轴的图示说明 objChart.Axes[0].HasTitle = true; objChart.Axes[0].Title.Caption = "X : " + Xname + ""; objChart.Axes[1].HasTitle = true; objChart.Axes[1].Title.Caption = "Y : " + Yname + "";
//添加一个series OWC11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);
//给定series的名字 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName); //给定分类 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimCategories, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory); //给定值 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimValues, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);
OWC11.ChDataLabels dl = objChart.SeriesCollection[0].DataLabelsCollection.Add(); dl.HasValue = true; // dl.Position=OWC11.ChartDataLabelPositionEnum.chLabelPositionOutsideEnd;
// string filename=DateTime.Now.ToString("yyyyMMddHHmmssff")+".gif";
if (IfExportToFile == true) { string filename = "temp.gif"; string strAbsolutePath = _phaysicalimagepath + "//" + filename; objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _pichight); //输出成GIF文件.
return filename; } else { string tmp = "Tmp"; objCSpace.ExportPicture(tmp, "GIF", _picwidth, _pichight); Bitmap bm = new Bitmap(tmp, true);
return bm; } }
#endregion #region 线性图
/// <summary> /// 线性图 /// </summary> /// <param name="Xname">X轴描述</param> /// <param name="Yname">Y轴描述</param> /// <param name="IfExportToFile">若把图像输出到文件夹则为true,否则为false</param> /// <returns>若把图像输出到文件夹返回生成的图像名称,否则返回Bitmap类型</returns> public Object CreateLine(string Xname, string Yname, bool IfExportToFile, bool If3D) { OWC11.ChartSpace objCSpace = new OWC11.ChartSpaceClass();//创建ChartSpace对象来放置图表 OWC11.ChChart objChart = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
//指定图表的类型。类型由OWC11.ChartChartTypeEnum枚举值得到 if (If3D == true) objChart.Type = OWC11.ChartChartTypeEnum.chChartTypeLine3D; else objChart.Type = OWC11.ChartChartTypeEnum.chChartTypeLine;
//指定图表是否需要图例 objChart.HasLegend = true;
//标题 objChart.HasTitle = true; objChart.Title.Caption = _title; objChart.Title.Font.Bold = true; // objChart.Title.Font.Color="blue"; #region 样式设置
//旋转 // objChart.Rotation = 145;//表示指定三维图表的旋转角度 // objChart.Inclination = 10;//表示指定三维图表的视图斜率。有效范围为 -90 到 90 // // //背景颜色 // objChart.PlotArea.Interior.Color = "red"; // // //底座颜色 // objChart.PlotArea.Floor.Interior.Color = "green"; // objChart.Overlap = 50;//单个类别中标志之间的重叠量
#endregion
//x,y轴的图示说明 objChart.Axes[0].HasTitle = true; objChart.Axes[0].Title.Caption = "X : " + Xname + ""; objChart.Axes[1].HasTitle = true; objChart.Axes[1].Title.Caption = "Y : " + Yname + "";
//添加一个series OWC11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);
//给定series的名字 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName); //给定分类 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimCategories, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory); //给定值 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimValues, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);
OWC11.ChDataLabels dl = objChart.SeriesCollection[0].DataLabelsCollection.Add(); dl.HasValue = true; // dl.Position=OWC11.ChartDataLabelPositionEnum.chLabelPositionOutsideEnd;
// string filename=DateTime.Now.ToString("yyyyMMddHHmmssff")+".gif";
if (IfExportToFile == true) { string filename = "temp.gif"; string strAbsolutePath = _phaysicalimagepath + "//" + filename; objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _pichight); //输出成GIF文件.
return filename; } else { string tmp = "Tmp"; objCSpace.ExportPicture(tmp, "GIF", _picwidth, _pichight); Bitmap bm = new Bitmap(tmp, true);
return bm; } }
#endregion #region 条形图(横向)
/// <summary> /// 条形图(横向) /// </summary> /// <param name="Xname"></param> /// <param name="Yname"></param> /// <param name="IfExportToFile"></param> /// <returns></returns> public Object CreateBar(string Xname, string Yname, bool IfExportToFile, bool If3D) { OWC11.ChartSpace objCSpace = new OWC11.ChartSpaceClass();//创建ChartSpace对象来放置图表 OWC11.ChChart objChart = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
//指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到 if (If3D == true) objChart.Type = OWC11.ChartChartTypeEnum.chChartTypeBarClustered3D; else objChart.Type = OWC11.ChartChartTypeEnum.chChartTypeBarClustered;
//指定图表是否需要图例 objChart.HasLegend = true;
//标题 objChart.HasTitle = true; objChart.Title.Caption = _title; objChart.Title.Font.Bold = true; // objChart.Title.Font.Color="blue"; #region 样式设置
//旋转 // objChart.Rotation = 145;//表示指定三维图表的旋转角度 // objChart.Inclination = 10;//表示指定三维图表的视图斜率。有效范围为 -90 到 90
//背景颜色 // objChart.PlotArea.Interior.Color = "red";
//底座颜色 // objChart.PlotArea.Floor.Interior.Color = "green"; // objChart.Overlap = 50;//单个类别中标志之间的重叠量
#endregion
//x,y轴的图示说明 objChart.Axes[0].HasTitle = true; objChart.Axes[0].Title.Caption = "X : " + Xname + ""; objChart.Axes[1].HasTitle = true; objChart.Axes[1].Title.Caption = "Y : " + Yname + "";
//添加一个series OWC11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);
//给定series的名字 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName); //给定分类 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimCategories, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory); //给定值 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimValues, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);
OWC11.ChDataLabels dl = objChart.SeriesCollection[0].DataLabelsCollection.Add(); dl.HasValue = true; // dl.Position=OWC11.ChartDataLabelPositionEnum.chLabelPositionOutsideEnd;
// string filename=DateTime.Now.ToString("yyyyMMddHHmmssff")+".gif";
if (IfExportToFile == true) { string filename = "temp.gif"; string strAbsolutePath = _phaysicalimagepath + "//" + filename; objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _pichight); //输出成GIF文件.
return filename; } else { string tmp = "Tmp"; objCSpace.ExportPicture(tmp, "GIF", _picwidth, _pichight); Bitmap bm = new Bitmap(tmp, true);
return bm; } }
#endregion #region 饼图
/// <summary> /// 饼图 /// </summary> /// <param name="IfExportToFile"></param> /// <returns></returns> public Object CreatePie(bool IfExportToFile, bool If3D) { OWC11.ChartSpace objCSpace = new OWC11.ChartSpaceClass();//创建ChartSpace对象来放置图表 OWC11.ChChart objChart = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
//指定图表的类型 if (If3D == true) objChart.Type = OWC11.ChartChartTypeEnum.chChartTypePie3D; else objChart.Type = OWC11.ChartChartTypeEnum.chChartTypePie;
//指定图表是否需要图例 objChart.HasLegend = true;
//标题 objChart.HasTitle = true; objChart.Title.Caption = _title; objChart.Title.Font.Bold = true;
//添加一个series OWC11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);
//给定series的名字 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName); //给定分类 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimCategories, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory); //给定值 ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimValues, OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);
//表示系列或趋势线上的单个数据标志 OWC11.ChDataLabels dl = objChart.SeriesCollection[0].DataLabelsCollection.Add(); dl.HasValue = true; dl.HasPercentage = true; //图表绘图区的图例放置在右侧。 // dl.Position=OWC11.ChartDataLabelPositionEnum.chLabelPositionRight;
// string filename=DateTime.Now.Ticks.ToString()+".gif";
if (IfExportToFile == true) { string filename = "temp.gif"; string strAbsolutePath = _phaysicalimagepath + "//" + filename; objCSpace.ExportPicture(strAbsolutePath, "GIF", _picwidth, _pichight); //输出成GIF文件.
return filename; } else { string tmp = "Tmp"; objCSpace.ExportPicture(tmp, "GIF", _picwidth, _pichight); Bitmap bm = new Bitmap(tmp, true);
return bm; } }
#endregion } }