西沉_NET

NET技术
posts - 4, comments - 2, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2005年12月19日

 最近做项目要对数据进行统计分析,所以做一些统计图,首先想到了微软的owc,同时自己把owc的操作封装了一下。在程序里调用里方便的多。

先看一下效果图:
o_2005083116363353.gif
o_2005090510095092.gif
o_2005090616060239.gif

在使用该类前当然首先要引用一下owc的dll啦!

公布源码:


using System;
using System.Data;
using System.Text;

using Microsoft.Office.Interop;

namespace OWCChartTest
{
 
/// <summary>
 
/// 利用OWC11进行作统计图的封装类。
 
/// 李天平 2005-8-31
 
/// </summary>
 public class OWCChart11
 {

  
#region 属性
  
private string _phaysicalimagepath;
  
private string _title;
  
private string _seriesname;
  
private int _picwidth;
  
private int _pichight;
  
private DataTable _datasource;
  
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;}
  }
  
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>
  
/// 柱形图
  
/// </summary>
  
/// <returns></returns>
  public string CreateColumn()
  { 
   Microsoft.Office.Interop.Owc11.ChartSpace objCSpace 
= new Microsoft.Office.Interop.Owc11.ChartSpaceClass();//创建ChartSpace对象来放置图表   
   Microsoft.Office.Interop.Owc11.ChChart objChart  = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
           
   
//指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到//Microsoft.Office.Interop.OWC.ChartChartTypeEnum
   objChart.Type=Microsoft.Office.Interop.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  = 360;//表示指定三维图表的旋转角度
//   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 : 类别";
   objChart.Axes[
1].HasTitle = true;
   objChart.Axes[
1].Title.Caption = "Y : 数量";
   

   
//添加一个series
   Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);


   
//给定series的名字
           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(),strCategory);
           
//给定值
           ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
      Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.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);//输出成GIF文件.

   
return filename;
   
  }


        
/// <summary>
        
/// 饼图
        
/// </summary>
        
/// <returns></returns>
  public string CreatePie()
  {
   Microsoft.Office.Interop.Owc11.ChartSpace objCSpace 
= new Microsoft.Office.Interop.Owc11.ChartSpaceClass();//创建ChartSpace对象来放置图表   
   Microsoft.Office.Interop.Owc11.ChChart objChart  = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
       
      
   
//指定图表的类型
   objChart.Type=Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie;
   
   
//指定图表是否需要图例
   objChart.HasLegend = true;
   
   
//标题
   objChart.HasTitle = true;
   objChart.Title.Caption
= _title;
     
         
   
//添加一个series
   Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);

   
//给定series的名字
   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(),strCategory);
   
//给定值
   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
    Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strValue);
      

   
//表示系列或趋势线上的单个数据标志
   Microsoft.Office.Interop.Owc11.ChDataLabels dl=objChart.SeriesCollection[0].DataLabelsCollection.Add();   
   dl.HasValue
=true;
   dl.HasPercentage
=true;   
   
//图表绘图区的图例放置在右侧。
//   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);//输出成GIF文件.

   
return filename;
  }

  
/// <summary>
  
/// 条形图
  
/// </summary>
  
/// <returns></returns>
  public string CreateBar()
  { 
   Microsoft.Office.Interop.Owc11.ChartSpace objCSpace 
= new Microsoft.Office.Interop.Owc11.ChartSpaceClass();//创建ChartSpace对象来放置图表   
   Microsoft.Office.Interop.Owc11.ChChart objChart  = objCSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
           
   
//指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到//Microsoft.Office.Interop.OWC.ChartChartTypeEnum
   objChart.Type=Microsoft.Office.Interop.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  = 360;//表示指定三维图表的旋转角度
//   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 : 类别";
   objChart.Axes[
1].HasTitle = true;
   objChart.Axes[
1].Title.Caption = "Y : 数量";
   

   
//添加一个series
   Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = objChart.SeriesCollection.Add(0);


   
//给定series的名字
   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(),strCategory);
   
//给定值
   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
    Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.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);//输出成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();//这是你的数据源
   
   
this.Image1.ImageUrl=filepath+"\\"+chart.CreateBar();//显示给图像控件。

posted @ 2005-12-19 11:43 西沉 阅读(600) | 评论 (1)编辑 收藏

2005年12月13日

   继续总结项目里的一些小TIP,我们平常用file文件控件上传文件,要预览有时会刷新页面,结果file控件被清空,好多人问能能让他不清空或重新赋值,因为处于安全性的考虑,这是不可能的。那怎么进行无刷新预览呢?这里我把我所使用的方法写一下。

选择页面:

<script language="javascript">
function checkData()
{
  var fileName=document.getElementById("FileUp").value;
  if(fileName=="")
   return;
  //检查文件类型
  var exName=fileName.substr(fileName.lastIndexOf(".")+1).toUpperCase()    
  if(exName=="JPG"||exName=="BMP"||exName=="GIF")
  {
   //document.getElementById("myimg").src=fileName;
   document.getElementById("previewImage").innerHTML='<img src=\''+fileName+'\' width=100 height=100 >';
  }
  else
  if(exName=="SWF")
  {
   document.getElementById("previewImage").innerHTML='<embed src=\''+fileName+'\' width=\'100\' height=\'100\' quality=\'high\' bgcolor=\'#f5f5f5\' ></embed>';     
  }
  else
  if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
  {
   var strcode='<embed src=\''+fileName+'\' border=\'0\' width=\'100\' height=\'100\' quality=\'high\' ';
   strcode+=' autoStart=\'1\' playCount=\'0\' enableContextMenu=\'0\' type=\'application/x-mplayer2\'></embed>';
   document.getElementById("previewImage").innerHTML=strcode;
  }
  else    
  {
   alert("请选择正确的图片文件");
   document.getElementById("FileUp").value="";
  } 
}
function openwin()
{    
 window.open("addPreview.aspx","","height=300,width=345,top=100,left=100");  
}

</script>

HTML代码:

<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" ID="Table1">
 <tr>
  <td width="255" height="100%" valign="middle">
   <INPUT id="FileUp" style="WIDTH: 253px; HEIGHT: 22px" type="file" size="23" name="File1"
    runat="server" onchange="checkData()"><br>
   &nbsp;&nbsp;注:这里可以是图片(jpg或gif格式),flash动画(swf)及视频文件(wmv,mpeg,asf,avi)。大小限定在1M以内。
  </td>
  <td>
   <div id="previewImage">当前页预览</div>
  </td>
 </tr>
</table>

弹出预览页面:

<script language="javascript">
function getstr()
{   
 var strcode=""; 
 var width=100;
 var high=100;
 if(self.opener.document.getElementById("FileUp")!=null)
 {
  //strcode=self.opener.document.getElementById("previewImage").innerHTML;
  width=self.opener.document.getElementById("lblWidth").innerText;
  high=self.opener.document.getElementById("lblHigh").innerText;
  
  var fileName=self.opener.document.getElementById("FileUp").value;
  var exName=fileName.substr(fileName.lastIndexOf(".")+1).toUpperCase()    
  if(exName=="JPG"||exName=="BMP"||exName=="GIF")
  {
   //document.getElementById("myimg").src=fileName;
   strcode='<img src=\''+fileName+'\' width='+width+' height='+high+' >';
  }
  else
  if(exName=="SWF")
  {
   strcode='<embed src=\''+fileName+'\' width=\''+width+'\' height=\''+high+'\' quality=\'high\' ></embed>';     
  }
  else
  if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
  {
  strcode='<embed src=\''+fileName+'\' border=\'0\' width=\''+width+'\' height=\''+high+'\' quality=\'high\' ';
  strcode+=' autoStart=\'1\' playCount=\'0\' enableContextMenu=\'0\' type=\'application/x-mplayer2\'></embed>';
  }
  
 }    
 if(self.opener.document.getElementById("txtADCode")!=null)
 {
  strcode=self.opener.document.getElementById("txtADCode").innerHTML; 
 }    
 if(strcode!="")
 {
  //window.alert(fileName);
  document.getElementById("showimg").innerHTML=strcode;
 }   
}

</script>

显示:

<div id="showimg"></div>

来自:http://ltp.cnblogs.com/archive/2005/12/03/289688.html

posted @ 2005-12-13 17:11 西沉 阅读(256) | 评论 (0)编辑 收藏

     摘要:      前一段时间,有人问我在.NET里如何进行TDD开发.这个问题促使我想对NUnit做一个详细的介绍.因为我们大家都知道NUnit是在.NET进行TDD的利器.        如果你已经知道很多关于NUnit的应用,请指出我的不对之处和提出一些建议,使本文更加完善.如果你对NUnit还不是...  阅读全文

posted @ 2005-12-13 16:55 西沉 阅读(273) | 评论 (1)编辑 收藏

2005年12月12日

 

 1/// <summary>
 2/// 返回应用程序的绝对路径
 3/// </summary>
 4/// <returns></returns>

 5  public static string root()
 6  {
 7   string serverpath=HttpContext.Current.Request.ServerVariables["HTTPS"];
 8   string pro=(serverpath=="on")?"https://":"http://";
 9
10   string servername=HttpContext.Current.Request.ServerVariables["Server_Name"];
11   string appname=HttpContext.Current.Request.ApplicationPath;
12
13   string path=pro+servername+appname;
14   return path;
15  }

16 }
17

posted @ 2005-12-12 17:48 西沉 阅读(210) | 评论 (0)编辑 收藏