ASP.NET用OWC绘图控件画统计图表(首先向清清月儿致敬)

 

效果图
What is Office Web Components


前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OWCdrawing.aspx.cs" Inherits="OWCdrawing" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test
</title>
</head>
<body>
    <form id="form1" runat="server">
   <div style="text-align: left">
        <table style="width: 600px">
            <tr>
                <td colspan="3" style="height: 20px">
                    <strong>怎么样在ASP.NET2.0中使用OWC组件画图</strong></td>
            </tr>
            <tr>
                <td colspan="3" rowspan="2" style="height: 21px">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                </td>
            </tr>
            <tr>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

手把手教程:

第一步:
右键点击网站根目录引用。如图所示:

OWC Drawing tutorial

第二步:
点击“添加引用”后弹出一个窗口,添加OWC的引用。如图所示:
OWC绘图控件教程
点“确定”。

第三步:
代码中引用Microsoft.Office.Interop.Owc11。

全部代码
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;   //添加数据操作引用
using Microsoft.Office.Interop.Owc11;//添加Office组件引用

public partial class OWCdrawing : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //连接数据库并获取特定字符串
        string strSeriesName = "图例1";
        string ConnectString = "Server=(local);DataBase=web;Uid=sa;Pwd=sa";
        string Sql = "SELECT month,Allcount FROM Chart";
        SqlConnection myConn = new SqlConnection(ConnectString);
        myConn.Open();
        SqlDataAdapter Da = new SqlDataAdapter(Sql, myConn);
        DataSet ds = new DataSet();
        Da.Fill(ds);

        //存放月
        string[] MonNum = new string[12];
        //存放数据
        string[] MonCount = new string[12];
        //为数组赋值
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            MonNum[i] = ds.Tables[0].Rows[i][0].ToString();
            MonCount[i] = ds.Tables[0].Rows[i][1].ToString();
        }
        //为x轴指定特定字符串,以便显示数据
        string strXdata = String.Empty;
        foreach (string strData in MonNum)
        {
            strXdata += strData + "/t";
        }
        string strYdata = String.Empty;
        //为y轴指定特定的字符串,以便与x轴相对应
        foreach (string strValue in MonCount)
        {
            strYdata += strValue + "/t";
        }

        //创建ChartSpace对象来放置图表
        ChartSpace laySpace = new ChartSpaceClass();

        //在ChartSpace对象中添加图表
        ChChart InsertChart = laySpace.Charts.Add(0);

        //指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
        //InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
        //InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
        //InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
        InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图

 

        //指定图表是否需要图例标注
        InsertChart.HasLegend = false;

       
        InsertChart.HasTitle = true;//为图表添加标题
        InsertChart.Title.Caption = "2006年清清月儿每个月花销流水账";//标题名称

        //为x,y轴添加图示说明
        InsertChart.Axes[0].HasTitle = true;
        InsertChart.Axes[0].Title.Caption = "";//月份
        InsertChart.Axes[1].HasTitle = true;
        InsertChart.Axes[1].Scaling.SplitMinimum = 200;
        InsertChart.Axes[1].Title.Caption = "数量";

        //添加一个series系列
        InsertChart.SeriesCollection.Add(0);

        //给定series系列的名字
        InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);

        //给定分类
        InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);

        //给定值
        InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
        //输出文件.
        string strAbsolutePath = (Server.MapPath(".")) + "
//ShowData.gif";
        laySpace.ExportPicture(strAbsolutePath, "GIF", 400, 250);

        //创建GIF文件的相对路径.
        string strRelativePath = "./ShowData.gif";

        //把图片添加到placeholder中,并在页面上显示
        string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
        this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
    }
}

数据库SQL脚本:
USE [web]
GO
/****** 对象: Table [dbo].[Chart] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Chart](
[id] [int] IDENTITY(1,1) NOT NULL,
[month] [smallint] NULL,
[Allcount] [int] NULL
) ON [PRIMARY]


在数据库建好表以后要自己手动假想有12条数据,手动添加,最终结果类似下图:

ASP.NET2.0绘制图表

后台程序说明:
最关键就是InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;

你可以在ChartChartTypeEnum后点出其他方法。如图所示:

ASP.NET2.0统计图表

下面列出的是其他类型图:

折线图:
OWC绘图教程

面积图:
ASP.NET2.0使用OWC

条形图:
ASP.NET

OWC什么图形都可以画,还能画立体的,请大家自己尝试。

可以参考OWC手册,具体位置:
C:/Program Files/Common Files/Microsoft Shared/Web Components/11/2052/OWCVBA11.CHM

 

之所以向清清月儿致敬是因为她写的确实很不错给了我最初的思路。

我后来解决的几个问题:

1。大家很容易发现他这个图标是断开的,在数据“200”处,为什么呢?我见有提问的,可是没有回答的。

InsertChart.Axes[1].Scaling.SplitMinimum = 200;这个就是实现分割效果的语句,注释掉就显示完整的图片了。

2.我的目的是用饼形图显示。可是直接在InsertChart.Type = ChartChartTypeEnum.chChartTypePie;处改成饼形却不能正确显示,上帝呀!!!

解决方法:将这部分代码注释掉就OK了!

        为x,y轴添加图示说明
        //InsertChart.Axes[0].HasTitle = true;
        //InsertChart.Axes[0].Title.Caption = "月份";//月份
        //InsertChart.Axes[1].HasTitle = true;
        //分割效果(这个注释是我自己加的,呵呵)
        InsertChart.Axes[1].Scaling.SplitMinimum = 148;
        //InsertChart.Axes[1].Title.Caption = "数量";

3.饼形部分分离效果

        将第三个扇区抽离出来
        //InsertChart.SeriesCollection[0].Points[2].Explosion = 45;

4.在iframe中使用OWC,并且刷新显示多个图片(这个也就是iframe中动态显示图片的刷新问题)

如果不用IFrame,在IE中图片刷新也是没问题的。图片的链接地址如果不变,在ie中是不会给你刷新的
每次都是iframe.aspx(我是用.net的),所以是从缓存中取的。

解决方法:给图片后面加上参数就OK了

        Random rdm = new Random();
        //把图片添加到placeholder中,并在页面上显示

//呵呵,在后面生成了个随机参数,再让你从缓存读取
        string strImageTag = "<IMG SRC='" + strRelativePath + "" + "?rdm=" + "" + rdm.Next(0, 100) + "'/>";
        this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));

见有用显示完直接删除生成图片的,我还没研究透,也不知道能不能解决这里的问题。有遇到的分享下昂,呵呵,独乐乐不如众乐乐!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值