基本原理:
通过html页面传递一些参数到.aspx.cs执行,然后得到返回的图片显示在html页。
需引用 Interop.OWC11.dll
1、html页代码:
<img id="ImgZZT" src="images/chartlaoding.gif" />
<script type="text/javascript">
function ShowImage(url, imgid) {
document.getElementById("" + imgid + "").src = url;
}
var title = "";
var fxsj = "百分比(%)";
var tblx = "pie";//图的类型,pie表示饼图,bar表示柱状图
var rdm = Math.random();
var xfjgbh=<%=hdXfjgbh.Value%>;
var xfdwjb=<%=hdXfdwjb.Value%>;
var url = encodeURI("Graph_jdpg.aspx?xfjgbh=" + xfjgbh + "&xfdwjb=" + xfdwjb + "&tblx=" + tblx + "&fxsj=" + fxsj + "&title=" + title + "&rdm=" + rdm);
ShowImage(url, "ImgZZT");
</script>
2、Graph_jdpg.aspx.cs代码
DataSet ds = null;
string sql = string.Empty;
string xfdwjb = string.Empty;
string xfjgbh = string.Empty;
if (Request.QueryString["xfdwjb"] != null && Request.QueryString["xfjgbh"] != null)
{
xfdwjb = Request.QueryString["xfdwjb"].ToString().Trim();
xfjgbh = Request.QueryString["xfjgbh"].ToString().Trim();
DAL.XFJG d_xfjg = new DAL.XFJG();
ds = d_xfjg.GetXFJGListProc(xfdwjb, xfjgbh, "getGraph_jdpg");//获取要显示的数据
}
if (ds.Tables[0].Rows.Count > 0)
{
object objPic = MyChart.GetPic(ds, Request.QueryString["tblx"].ToString(), Request.QueryString["title"].ToString(), Request.QueryString["fxsj"].ToString(), 230, 150, "#ffffff", "#ffffff");//这是画图的真正核心方法
if (objPic == null)
Response.End();
Array arr = (Array)objPic;
Response.Expires = 0;
Response.Buffer = true;
Response.Clear();
MemoryStream memStream = new MemoryStream((byte[])arr);
memStream.WriteTo(Response.OutputStream);
memStream.Close();
Response.ContentType = "image/gif";
Response.StatusCode = 200;
Response.End();
}
3、MyChart.cs
using OWC11;
/// <summary>
///
/// </summary>
/// <param name="ds">含有显示数据的dataset</param>
/// <param name="chartType">pie,bar,line</param>
/// <param name="chartTitle"></param>
/// <param name="axesTitle">图的标题</param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="strbackcolor">背景颜色,比如:#e8efed</param>
/// <param name="strBoderColor">边框颜色,比如:#ffffff</param>
/// <returns></returns>
public static object GetPic(DataSet ds,string chartType,string chartTitle,string axesTitle,int width,int height,string strbackcolor,string strBoderColor)
{
string strTitles = "",strValues = "";
DsToStr(ref strTitles,ref strValues,ds.Tables[0]);
ChartSpace chartSpace = new ChartSpaceClass();//创建ChartSpace对象来放置图表
ChChart chart = chartSpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回Chart对象
chart.Border.Color = strBoderColor; //设置边框颜色
chart.Interior.Color = strbackcolor;
if(chartTitle != "")
{
chart.HasTitle = true;
chart.Title.Caption = chartTitle;
chart.Title.Font.Size = 12;
chart.Title.Font.Bold = true;
chart.Title.Font.Name = "宋体";
}
if(chartType == "bar")
{
chart.Type = OWC11.ChartChartTypeEnum.chChartTypeColumnClustered3D;//.chChartTypeColumnStacked3D;,如果要平面柱状图,则为chChartTypeColumnClustered 20100701 yg
chart.PlotArea.BackWall.Interior.Color = "#FFFFFF";//背景墙的颜色
chart.PlotArea.BackWall.Thickness = 0;//背景墙的厚度
chart.PlotArea.SideWall.Interior.Color = "#FFFFFF";//边墙的颜色
chart.PlotArea.SideWall.Thickness = 0;//边墙的厚度
chart.PlotArea.Floor.Interior.Color = "#FFFFFF"; // 底墙的底色
chart.PlotArea.Floor.Thickness = 0;//设置底墙的厚度
chart.ProjectionMode = OWC11.ChartProjectionModeEnum.chProjectionModeOrthographic; //设置投影方式
chart.Inclination = 8;//设置倾角,指定三维图表的视图斜率(-90 - 90)
chart.ChartDepth = 15;//设置深度比,指定三维图表相对于宽度的深度(0-500)
chart.ExtrudeAngle = 55;//延伸方向(0-360),仅在chProjectionModeOrthographic时有效
chart.Axes[1].Scaling.Minimum = 0;
if(axesTitle != "")
{
chart.Axes[1].HasTitle = true;
chart.Axes[1].Title.Caption = axesTitle;//设置数据轴标题,即X轴
chart.Axes[1].Title.Font.Size = 10;
}
chart.Axes[1].Orientation = (int)ChartLabelOrientationEnum.chLabelOrientationHorizontal;//设置分类标签的字体方向,X轴
chart.Axes[0].Font.Size = 9;
chart.Axes[0].Font.Name = "宋体";
chart.Axes[0].Orientation = (int)ChartLabelOrientationEnum.chLabelOrientationHorizontal;//设置分类标签的字体方向,Y轴
chart.Axes[1].HasMajorGridlines = false;//去掉水平网线
chart.Axes[1].HasMinorGridlines = false;//去掉水平网线
ChSeries objSeries = chart.SeriesCollection.Add(0); //添加一个series
ChDataLabels objDataLabels = objSeries.DataLabelsCollection.Add();
objDataLabels.HasValue = true; // 柱子上面的数字是否显示
//objDataLabels.HasPercentage = true;
objDataLabels.Font.Color = "Black"; // 柱子上面的数字颜色是黑色的
objSeries.SetData( OWC11.ChartDimensionsEnum.chDimCategories,(int)OWC11.ChartSpecialDataSourcesEnum.chDataLiteral,strTitles );//给定series的分类
objSeries.SetData( OWC11.ChartDimensionsEnum.chDimValues,(int)OWC11.ChartSpecialDataSourcesEnum.chDataLiteral,strValues );//给定具体值
for(int i=0;i<objSeries.Points.Count;i++)
{
if(i>14)
break;
objSeries.Points[i].Interior.Color = colorArr[i];
objSeries.Points[i].Border.Color = colorArr[i];
}
}
else if(chartType == "line")
{
chart.Type = OWC11.ChartChartTypeEnum.chChartTypeSmoothLineMarkers;//曲线
chart.PlotArea.Interior.Color = "#FAFAFA";//背景墙的颜色
if(axesTitle != "")
{
chart.Axes[1].HasTitle = true;
chart.Axes[1].Title.Caption = axesTitle;//设置数据轴标题
chart.Axes[1].Title.Font.Size = 10;
}
chart.Axes[1].Scaling.Minimum = 0;
chart.Axes[0].Font.Size = 10;
chart.Axes[0].Font.Name = "宋体";
chart.Axes[0].Orientation = (int)ChartLabelOrientationEnum.chLabelOrientationHorizontal;//设置分类标签的字体方向
ChSeries objSeries = chart.SeriesCollection.Add(0); //添加一个series
ChDataLabels objDataLabels = objSeries.DataLabelsCollection.Add();
objDataLabels.HasValue = true; // 柱子上面的数字是否显示
objDataLabels.Font.Color = "Black"; // 柱子上面的数字颜色是黑色的
objSeries.SetData( OWC11.ChartDimensionsEnum.chDimCategories,(int)OWC11.ChartSpecialDataSourcesEnum.chDataLiteral,strTitles );//给定series的分类
objSeries.SetData( OWC11.ChartDimensionsEnum.chDimValues,(int)OWC11.ChartSpecialDataSourcesEnum.chDataLiteral,strValues );//给定具体值
objSeries.Line.Color = colorArr[0];
objSeries.Interior.Color = colorArr[3];
}
else if(chartType == "pie")
{
// chart.HasTitle = true;
// chart.Title.Caption = axesTitle.Substring(0,axesTitle.IndexOf("("));
// chart.Title.Font.Size = 10;
// chart.Title.Font.Bold = true;
// chart.Title.Font.Name = "宋体";
chart.Type = OWC11.ChartChartTypeEnum.chChartTypePieExploded3D;
chart.ProjectionMode = OWC11.ChartProjectionModeEnum.chProjectionModePerspective; //设置投影方式,透视
chart.Perspective = 15;//透视系数
chart.Inclination = 64;
chart.HasLegend = true;
chart.Legend.Position = ChartLegendPositionEnum.chLegendPositionRight;//图例在图片右侧
chart.Legend.Font.Size = 9;//设置图例字体
chart.Legend.Font.Name = "宋体";
ChSeries objSeries = chart.SeriesCollection.Add(0); //添加一个series
objSeries.SetData( OWC11.ChartDimensionsEnum.chDimCategories,(int)OWC11.ChartSpecialDataSourcesEnum.chDataLiteral,strTitles );//给定series的分类
objSeries.SetData( OWC11.ChartDimensionsEnum.chDimValues,(int)OWC11.ChartSpecialDataSourcesEnum.chDataLiteral,strValues );//给定具体值
ChDataLabels objDataLabels = objSeries.DataLabelsCollection.Add();
objDataLabels.HasValue = false; //显示各部分的数值
objDataLabels.HasPercentage = true; //显示各部分的百分比
objDataLabels.Font.Size = 10; //设置饼状图中字体的大小
objDataLabels.Font.Color = "Black";
for(int i=0;i<objSeries.Points.Count;i++)
{
if(i>14)
break;
objSeries.Points[i].Interior.Color = colorArr[i];
objSeries.Points[i].Border.Color = colorArr[i];
}
}
else
{
return null;
}
object objPic = chartSpace.GetPicture("GIF", width, height);
return objPic;
}
/// <summary>
/// 转换DataTable为OWC特定格式字符串
/// </summary>
/// <param name="strTitles">组标签字符串</param>
/// <param name="strValues">数据字符串</param>
/// <param name="dt">DataTable数据源</param>
private static void DsToStr(ref string strTitles,ref string strValues,DataTable dt)
{
string[] titles = new string[dt.Rows.Count];
string[] values = new string[dt.Rows.Count];
for(int i=0;i<dt.Rows.Count;i++)
{
titles[i] = dt.Rows[i][0].ToString() == "" ? "未知" : dt.Rows[i]["mc"].ToString();
values[i]= dt.Rows[i][1].ToString() == "" ? "0" : dt.Rows[i]["val"].ToString();
}
strTitles = String.Join("\t",titles);
strValues = String.Join("\t",values);
}
增值服务哈:office自带的关于owc的帮助文档:
C:\Program Files\Common Files\Microsoft Shared\Web Components\11\2052\OWCVBA11.CHM