• OWC控件生成统计图



    DrawStat.ashx


    using System;
    using System.Web;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using OWC11 = Microsoft.Office.Interop.Owc11;
    //引用com组件:Micorsoft Office Web Components 11.0
    /// <summary>
    /// DrawStat 的摘要说明
    /// </summary>
    public class DrawStat : IHttpHandler
    {
    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType
    = "text/plain";
    int chartType = 0;
    string chartTitle = "统计图";
    string chartData = "";
    if (!string.IsNullOrEmpty(context.Request.Params["chartType"]))
    int.TryParse(context.Request.Params["chartType"], out chartType);
    if (!string.IsNullOrEmpty(context.Request.Params["chartTitle"]))
    chartTitle
    = context.Request.Params["chartTitle"];
    if (!string.IsNullOrEmpty(context.Request.Params["chartData"]))
    chartData
    = context.Request.Params["chartData"];
    if (chartData.Length > 0)
    {
    List
    <string> datanameList = new List<string>();
    List
    <int> dataList = new List<int>();
    string[] ss = chartData.Split('|');
    if (ss.Length > 0)
    {
    foreach (string s in ss)
    {
    string[] arr = s.Split(',');
    datanameList.Add(arr[
    0]);
    dataList.Add(Convert.ToInt32(arr[
    1]));
    }
    //删除之前生成的文件
    string file = context.Server.MapPath("~/Upload/") + "tmpFile.gif";
    if (System.IO.File.Exists(file))
    System.IO.File.Delete(file);
    int datasum = 0;
    dataList.ForEach(i
    => datasum += i);
    string strdataname = "";
    string strdata = "";
    for (int j = 0; j < dataList.Count; j++)
    {
    double percentdata = 100.0 * dataList[j] / datasum;
    strdataname
    += datanameList[j] + percentdata.ToString("0.00") + "%" + '\t';
    strdata
    += dataList[j].ToString() + '\t';
    }

    //创建一个图形容器对象,可以包含多个图形对象,最多16个
    OWC11.ChartSpace objchartspace = new OWC11.ChartSpaceClass();
    //在图形容器中增加一个图形对象ChChart。一个柱状图、饼状图、散点图等都是一个图形对象,多个图形对象构成ChCharts集合。
    OWC11.ChChart objchart = objchartspace.Charts.Add(0);
    //图形类型
    switch (chartType)
    {
    case 0:
    objchart.Type
    = OWC11.ChartChartTypeEnum.chChartTypePie; //圆饼图
    break;
    case 1:
    objchart.Type
    = OWC11.ChartChartTypeEnum.chChartTypeColumnClustered3D;//柱状图
    break;
    case 2:
    objchart.Type
    = OWC11.ChartChartTypeEnum.chChartTypeBar3D;//横道图3D
    break;
    }
    //设置图形容器的边框颜色
    objchart.Border.Color = "White";
    //显示标题
    objchart.HasTitle = true;
    //设置标题
    objchart.Title.Caption = chartTitle;
    //设置标题字体的大小
    objchart.Title.Font.Size = 10;
    //显示图例
    objchart.HasLegend = true;
    //设置图例字体大小
    objchart.Legend.Font.Size = 10;
    //设置图例位置为右侧
    objchart.Legend.Position = OWC11.ChartLegendPositionEnum.chLegendPositionRight;
    //在图形对象中添加一个系列
    objchart.SeriesCollection.Add(0);
    //给定系列的名字
    objchart.SeriesCollection[0].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories, (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strdataname);
    //给定值
    objchart.SeriesCollection[0].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues, (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strdata);
    //创建gif文件
    objchartspace.ExportPicture(context.Server.MapPath("~/Upload/") + "tmpFile.gif", "GIF", 600, 400);
    string path = "/Upload/tmpFile.gif";
    context.Response.Write(path);
    }
    }
    }
    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }


    ============================================================
    .aspx

    <script type="text/javascript"><!--
    $(
    function () {
    jQuery.post(
    "/Ajax/DrawStat.ashx?chartTitle=" + escape("gdjlc统计图示") + "&chartType=0&chartData=" + escape("文本框,2|复选框,3|下拉框,1"),
    function (data, textStatus) {
    var img = "<img src="" + data + "" mce_src="&quot; + data + &quot;" alt='' border='0'/>";
    $(
    "#divStat").append(img);
    });
    });
    // --></script>
    <div id="divStat" style="100%; border:0; text-align:center"></div>





    效果1:


    效果2:

  • 相关阅读:
    Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'
    在终端中文本编辑器中显示行数:
    java环境配置:
    TypeError: 'MongoClient' object is not callable
    MongoDB安装
    多线程 -- 实现秒抓
    自动化测试淘宝美食
    自动化淘宝美食(在后台执行,不打开浏览器)
    新浪 抓取详情页
    WINDOWS 2003系统时间24小时制与12小时显示格式不一致问题与解决
  • 原文地址:https://www.cnblogs.com/gdjlc/p/2086869.html
Copyright © 2020-2023  润新知