首页 / 网页编程 / ASP.NET / asp.net利用OWC生成饼状图、柱状图
        
            asp.net利用OWC生成饼状图、柱状图2011-07-30OWC(Microsoft Office Web Components)是 Microsoft Office 使用的数据绑定 ActiveX 控件,安装office就有了,其实也可以单独安装的(google下载),使用OWC做了一个饼状图和柱状图,感觉比较简单,开发难度小,效率比较高,OWC的主要原理是按照所传入的数据生成相应的图片,然后开发人员将该图片加载到相应的位置即可实现效果,个人感觉生成的图比较粗糙,但是真的封装的很好,只需按照函数要求输入数据就OK了,可以设置图片的背景色,字体,大小等等。在vs2005/2008里面开发的时候先要右键--》添加引用--》Com-》Microsoft Office Web components 11(这是安装了office2007,2003好像直接叫Microsoft  OWc 什么的)饼状图:protected void pieBtn_Click(object sender, EventArgs e)
     {
         //创建X坐标的值,表示月份
         int[] month ={ 1, 2, 3 };
         //创建Y坐标的值,表示销售额
         double[] count ={ 120, 240, 220 };
         string strDataName = "";
         string strData = "";
         //创建图表空间
         ChartSpace mychartSpace = new ChartSpace();
         mychartSpace.Border.Color = "White";
         //在图表空间内添加一个图表对象
         ChChart mychart = mychartSpace.Charts.Add(0);
         //设置每块饼的数据
        for(int i=0;i<count.Length;i++)
         {
             strDataName += month[i].ToString()+ "	";
             strData += count[i].ToString()+ "	";
         }
         //设置图表类型,本例使用饼
         mychart.Type = ChartChartTypeEnum.chChartTypePie;
         //设置图表的一些属性
         //是否需要图例
         mychart.HasLegend = true;
         //是否需要主题
         mychart.HasTitle = true;
         //主题内容
         mychart.Title.Caption ="饼状图测试";
         mychart.Title.Font.Size = 10;
         mychart.Title.Font.Bold = false;
         mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
         mychart.Legend.Interior.Color = "f9f9f9";
         mychart.Legend.Font.Size = 9;
         mychart.Legend.Border.Color = "White";
         //添加图表块
         mychart.SeriesCollection.Add(0);
         //设置图表块的属性
         //分类属性
         mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,
         (int)ChartSpecialDataSourcesEnum.chDataLiteral, strDataName);
         //值属性
         mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues,
         (int)ChartSpecialDataSourcesEnum.chDataLiteral, strData);
         for (int j = 0; j < mychart.SeriesCollection[0].Points.Count; j++)
         {
             mychart.SeriesCollection[0].Points[j].Border.Color = "White";
         }
         //显示百分比
         ChDataLabels mytb = mychart.SeriesCollection[0].DataLabelsCollection.Add();
         mytb.HasPercentage = true;
         //mytb.Border.Color = "White";
         mytb.HasValue = true;
         //生成图片
         mychartSpace.ExportPicture(Server.MapPath(".") + @"stat.gif", "gif", 700, 400);
         //加载图片
         this.ig.ImageUrl = "stat.gif" + "?temp=" + new Random().Next(1, 100) + ""; ;
     }