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="" + data + "" alt='' border='0'/>";
$("#divStat").append(img);
});
});
// --></script>
<div id="divStat" style="100%; border:0; text-align:center"></div>
效果1:
效果2: