ASP.NET网页画图之二-C# Web Components 组件画图

本文介绍如何在ASP.NET应用中利用Microsoft Office Web Components (OWC) 组件绘制柱状图。通过实例演示了从创建图表空间、定义图表类型到设置数据源的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面已经介绍了 C# 纯代码的方式画图 ..下面来看看 office组件画图,用Micosoft Office Web Components 组件要确保服务器要装微软office2000或以上,客户端用户也要装Office2000或以上才能使用

本例 假设 你已经装上了Office

 

 首先 打开 VS2005(建议不要用其他的编译器)在项目里添加引用(具体怎么添加见Google Com 选择项里 选择 Microsoft Office Web Comonents 11.0 确定 此时项目会自动添加Bin文件夹包含以下几个文件

 

OfficeWebToPaint.aspx Html

代码只要拖入个        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

容器控件即可

 

OfficeWebToPaint.aspx.cs 代码:

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
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  OWC11; // 画图必须

public   partial   class  System_OfficePaintTest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//创建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.Border.Color = "White";//图像的边框颜色


        
//指定图表是否需要图例标注
        InsertChart.HasLegend = true;
        InsertChart.Legend.Font.Size 
= 10;//字体
        InsertChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionRight;//图例标注的位置


        
//图表的标题
        InsertChart.HasTitle = true;//为图表添加标题
        InsertChart.Title.Caption = "开关管温度";//设置标的内容
        InsertChart.Title.Font.Size = 15;//设置标题字体大小
        InsertChart.Title.Font.Bold = true;//是否加粗

        
//为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 = "T 温度";

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

        
//给定series系列的名字
        InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, "计算节温");//文字也是标注的文字

        
//给定分类 的值
        InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, "第一个开关管 第二个开关管 第三个开关管");
        
//给定值 的值
        InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, "100 70 48 55 44");
        
//输出文件.


        
//给定series系列的名字
        InsertChart.SeriesCollection.Add(1);
        InsertChart.SeriesCollection[
1].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, "最高允许节温");//文字也是标注的文字

        
//给定分类 的值
        InsertChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, "第一个开关管 第二个开关管 第三个开关管");
        
//给定值 的值
        InsertChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, "33 55 33 22 88");
        
//输出文件.


        
string strAbsolutePath = (Server.MapPath(".")) + "/ShowData.gif";

        laySpace.ExportPicture(strAbsolutePath, 
"GIF"800350);

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

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

    }

}

以上代码经测试 都可以运行

一般在网页上画图·就是用上面两种方法:

 源码下载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值