现在的位置: 首页 > 综合 > 正文

OWC控件的使用学习札记(二)

2014年02月05日 ⁄ 综合 ⁄ 共 3325字 ⁄ 字号 评论关闭

 

OWC(Microsoft Office Web Components)是 Microsoft Office 使用的数据绑定 ActiveX 控件,安装office就有了,其实也可以单独安装的(google下载),使用OWC做了一个饼状图和柱状图,感觉比较简单,开发难度小,效率比较高,OWC的主要原理是按照所传入的数据生成相应的图片,然后开发人员将该图片加载到相应的位置即可实现效果,个人感觉生成的图比较粗糙,但是真的封装的很好,只需按照函数要求输入数据就OK了,可以设置图片的背景色,字体,大小等等。。。。。

OWC提供面积图、柱状图、条形图、折线图、平滑曲线图、饼图、圆环图、股价图、散点图、雷达图。

在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()+ "\t";
            strData += count[i].ToString()+ "\t";
        }


        //设置图表类型,本例使用饼
        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) + ""; ;
    }
柱状图:

代码 
protected void columnarBtn_Click(object sender, EventArgs e)
    {
        int[] month ={ 1, 2, 3 };
        //创建Y坐标的值,表示销售额
        double[] count ={ 120, 240, 220 };

        //创建图表空间
        ChartSpace mychartSpace = new ChartSpace();

        //在图表空间内添加一个图表对象
        ChChart mychart = mychartSpace.Charts.Add(0);
        mychartSpace.Border.Color = "White";
        //设置图表类型,本例使用柱形
        mychart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;
        //设置图表的一些属性
        //是否需要图例
        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";
        //设置x,y坐标

        mychart.Axes[1].HasTitle = true;
        mychart.Axes[1].Title.Caption = "坐标";
        mychart.Axes[1].Title.Font.Size = 9;

        for (int i = 0; i < count.Length; i++)
        {
            mychart.SeriesCollection.Add(i);
            mychart.SeriesCollection[i].Caption = count[i].ToString();
            mychart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, month[i]);
            mychart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, count[i]);

        }
        mychartSpace.ExportPicture(Server.MapPath(".") + @"\stat1.gif", "gif", 700, 400);
        //加载图片
        this.ig.ImageUrl = "stat1.gif" + "?temp=" + new Random().Next(1, 100) + ""; ;
    }
这样子就已经OK了,可以按照自己的实际情况传入相应的数据即可,也可以按照自己的喜好来设置颜色字体等等 

抱歉!评论已关闭.