`

java图形报表实例

阅读更多

在上两个关于图形化分析中!有朋友反映说java和FusionChart到底有什么关系!下面就这一问题做一个java实例!进行讲解!在讲解之前先笼统的说一下!起始FusionChart就是利用flash作为模板!当然这些模板是官方已经写好的!只需要去官方下载!其次我们只需要利用java生成相应的xml文档!在用特定的方法调用就可

/**
 * 该类是生成FusionCharts图形的主要方法【生成图形时主要调用该类中的createChart这个静态的方法】
 * @author SkyWen
 *
 */
public class FusionChartsCreator {

    /**
     * Adds additional string to the url to and encodes the parameters,<br>
     * so as to disable caching of data.<br> 
     * @param strDataURL -
     *                dataURL to be fed to chart
     * @param addNoCacheStr -
     *                Whether to add aditional string to URL to disable
     *                caching of data
     * @return cachedURL - URL with the additional string added
     */

    public static String addCacheToDataURL(String strDataURL) {
 String cachedURL = strDataURL;
 // Add the no-cache string if required

 // We add ?FCCurrTime=xxyyzz
 // If the dataURL already contains a ?, we add &FCCurrTime=xxyyzz
 // We replace : with _, as FusionCharts cannot handle : in URLs
 Calendar nowCal = Calendar.getInstance();
 Date now = nowCal.getTime();
 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH_mm_ss a");
 String strNow = sdf.format(now);

 try {

     if (strDataURL.indexOf("?") > 0) {
  cachedURL = strDataURL + "&FCCurrTime="
  + URLEncoder.encode(strNow, "UTF-8");
     } else {
  cachedURL = strDataURL + "?FCCurrTime="
  + URLEncoder.encode(strNow, "UTF-8");
     }

 } catch (UnsupportedEncodingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     cachedURL = strDataURL + "?FCCurrTime=" + strNow;
 }

 return cachedURL;
    }

    /**
     * Creates the JavaScript + HTML code required to embed a chart.<br>
     * Uses the javascript FusionCharts class to create the chart by supplying <br>
     * the required parameters to it.<br>
     * Note: Only one of the parameters strURL or strXML has to be not null for this<br>
     * method to work. If both the parameters are provided then strURL is used for further processing.<br>
     *
     * @param chartSWF -
     *                SWF File Name (and Path) of the chart which you intend
     *                to plot
     * @param strURL -
     *                If you intend to use dataURL method for this chart,
     *                pass the URL as this parameter. Else, set it to "" (in
     *                case of dataXML method)
     * @param strXML -
     *                If you intend to use dataXML method for this chart,
     *                pass the XML data as this parameter. Else, set it to ""
     *                (in case of dataURL method)
     * @param chartId -
     *                Id for the chart, using which it will be recognized in
     *                the HTML page. Each chart on the page needs to have a
     *                unique Id.
     * @param chartWidth -
     *                Intended width for the chart (in pixels)
     * @param chartHeight -
     *                Intended height for the chart (in pixels)
     * @param debugMode -
     *                Whether to start the chart in debug mode
     * @param registerWithJS -
     *                Whether to ask chart to register itself with
     *                JavaScript
     */
    public static String createChart(String chartSWF, String strURL,
     String strXML, String chartId, int chartWidth, int chartHeight,
     boolean debugMode, boolean registerWithJS) {
 StringBuffer strBuf = new StringBuffer();
 // First we create a new DIV for each chart. We specify the name of DIV
 // as "chartId"Div.
 // DIV names are case-sensitive.

 strBuf.append("\t\t<!-- START Script Block for Chart-->\n");
 strBuf.append("\t\t<script LANGUAGE='Javascript' SRC='/FusionCharts/FusionCharts.js'></script>\n");
 strBuf.append("\t\t<div id='" + chartId + "Div' align='center'>\n");
 strBuf.append("\t\t\t\tChart.\n");

 /*
  * The above text "Chart" is shown to users before the chart has started
  * loading (if there is a lag in relaying SWF from server). This text is
  * also shown to users who do not have Flash Player installed. You can
  * configure it as per your needs.
  */

 strBuf.append("\t\t</div>\n");

 /*
  * Now, we create the chart using FusionCharts js class. Each chart's
  * instance (JavaScript) Id is named as chart_"chartId".
  */

 strBuf.append("\t\t<script type='text/javascript'>\n");
 // Instantiate the Chart
 Boolean registerWithJSBool = new Boolean(registerWithJS);
 Boolean debugModeBool = new Boolean(debugMode);
 int regWithJSInt = boolToNum(registerWithJSBool);
 int debugModeInt = boolToNum(debugModeBool);

 strBuf.append("\t\t\t\tvar chart_" + chartId + " = new FusionCharts('"
  + chartSWF + "', '" + chartId + "', '" + chartWidth + "', '"
  + chartHeight + "', '" + debugModeInt + "', '" + regWithJSInt
  + "');\n");
 // Check whether we've to provide data using dataXML method or dataURL
 // method
 if (strXML.equals("")) {
     strBuf.append("\t\t\t\t// Set the dataURL of the chart\n");
     strBuf.append("\t\t\t\tchart_" + chartId + ".setDataURL(\"" + strURL
      + "\");\n");
 } else {
     strBuf.append("\t\t\t\t// Provide entire XML data using dataXML method\n");
     strBuf.append("\t\t\t\tchart_" + chartId + ".setDataXML(\"" + strXML
      + "\");\n");
 }
 strBuf.append("\t\t\t\t// Finally, render the chart.\n");
 strBuf.append("\t\t\t\tchart_" + chartId + ".render(\"" + chartId + "Div\");\n");
 strBuf.append("\t\t</script>\n");
 strBuf.append("\t\t<!--END Script Block for Chart-->\n");
 System.out.println(strBuf);
 return strBuf.substring(0);
    }

    /**
     * Creates the object tag required to embed a chart.
     * Generates the object tag to embed the swf directly into the html page.<br>
     * Note: Only one of the parameters strURL or strXML has to be not null for this<br>
     * method to work. If both the parameters are provided then strURL is used for further processing.<br>
     * 
     * @param chartSWF -
     *                SWF File Name (and Path) of the chart which you intend
     *                to plot
     * @param strURL -
     *                If you intend to use dataURL method for this chart,
     *                pass the URL as this parameter. Else, set it to "" (in
     *                case of dataXML method)
     * @param strXML -
     *                If you intend to use dataXML method for this chart,
     *                pass the XML data as this parameter. Else, set it to ""
     *                (in case of dataURL method)
     * @param chartId -
     *                Id for the chart, using which it will be recognized in
     *                the HTML page. Each chart on the page needs to have a
     *                unique Id.
     * @param chartWidth -
     *                Intended width for the chart (in pixels)
     * @param chartHeight -
     *                Intended height for the chart (in pixels)
     * @param debugMode -
     *                Whether to start the chart in debug mode
     */
    public static String createChartHTML(String chartSWF, String strURL,
     String strXML, String chartId, int chartWidth, int chartHeight,
     boolean debugMode) { /*
      * Generate the FlashVars string based
      * on whether dataURL has been provided
      * or dataXML.
      */
 String strFlashVars = "";
 Boolean debugModeBool = new Boolean(debugMode);

 if (strXML.equals("")) {
     // DataURL Mode
     strFlashVars = "chartWidth=" + chartWidth + "&chartHeight="
     + chartHeight + "&debugMode=" + boolToNum(debugModeBool)
     + "&dataURL=" + strURL + "";
 } else {
     // DataXML Mode
     strFlashVars = "chartWidth=" + chartWidth + "&chartHeight="
     + chartHeight + "&debugMode=" + boolToNum(debugModeBool)
     + "&dataXML=" + strXML + "";
 }
 StringBuffer strBuf = new StringBuffer();

 strBuf.append("\t\t<!--START Code Block for Chart-->\n");
 strBuf
 .append("\t\t<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"
  + chartWidth
  + "' height='"
  + chartHeight
  + "' id='"
  + chartId + "'>\n");
 strBuf.append("\t\t\t\t<param name='allowScriptAccess' value='always' />\n");
 strBuf.append("\t\t\t\t<param name='movie' value='" + chartSWF + "'/>\n");
 strBuf.append("\t\t\t\t<param name='FlashVars' value=\"" + strFlashVars
  + "\" />\n");
 strBuf.append("\t\t\t\t<param name='quality' value='high' />\n");
 strBuf
 .append("\t\t\t\t<embed src='"
  + chartSWF
  + "' FlashVars=\""
  + strFlashVars
  + "\" quality='high' width='"
  + chartWidth
  + "' height='"
  + chartHeight
  + "' name='"
  + chartId
  + "' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />\n");
 strBuf.append("\t\t</object>\n");
 strBuf.append("\t\t<!--END Code Block for Chart-->\n");
 return strBuf.substring(0);
    }

    /**
     * Converts a Boolean value to int value<br>
     *
     * @param bool Boolean value which needs to be converted to int value
     * @return int value correspoding to the boolean : 1 for true and 0 for false
     */
   public static int boolToNum(Boolean bool) {
 int num = 0;
 if (bool.booleanValue()) {
     num = 1;
 }
 return num;
    }
}

  注意 strBuf.append("\t\t<script LANGUAGE='Javascript' SRC='/FusionCharts/FusionCharts.js'></script>\n");

说明在你的根目录下请放置一个文件夹FusionCharts 下面有这样一个js

FusionCharts.js内容是

/**
 * FusionCharts: Flash Player detection and Chart embed
 *
 * Morphed from SWFObject (http://blog.deconcept.com/swfobject/) under MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
if(typeof infosoftglobal == "undefined") var infosoftglobal = new Object();
if(typeof infosoftglobal.FusionChartsUtil == "undefined") infosoftglobal.FusionChartsUtil = new Object();
infosoftglobal.FusionCharts = function(swf, id, w, h, debugMode, registerWithJS, c, scaleMode, lang){
 if (!document.getElementById) { return; }
 
 //Flag to see whether data has been set initially
 this.initialDataSet = false;
 
 //Create container objects
 this.params = new Object();
 this.variables = new Object();
 this.attributes = new Array();
 
 //Set attributes for the SWF
 if(swf) { this.setAttribute('swf', swf); }
 if(id) { this.setAttribute('id', id); }
 if(w) { this.setAttribute('width', w); }
 if(h) { this.setAttribute('height', h); }
 
 //Set background color
 if(c) { this.addParam('bgcolor', c); }
 
 //Set Quality 
 this.addParam('quality', 'high');
 
 //Add scripting access parameter
 this.addParam('allowScriptAccess', 'always');
 
 //Pass width and height to be appended as chartWidth and chartHeight
 this.addVariable('chartWidth', w);
 this.addVariable('chartHeight', h);

 //Whether in debug mode
 debugMode = debugMode ? debugMode : 0;
 this.addVariable('debugMode', debugMode);
 //Pass DOM ID to Chart
 this.addVariable('DOMId', id);
 //Whether to registed with JavaScript
 registerWithJS = registerWithJS ? registerWithJS : 0;
 this.addVariable('registerWithJS', registerWithJS);
 
 //Scale Mode of chart
 scaleMode = scaleMode ? scaleMode : 'noScale';
 this.addVariable('scaleMode', scaleMode);
 //Application Message Language
 lang = lang ? lang : 'EN';
 this.addVariable('lang', lang);
}

infosoftglobal.FusionCharts.prototype = {
 setAttribute: function(name, value){
  this.attributes[name] = value;
 },
 getAttribute: function(name){
  return this.attributes[name];
 },
 addParam: function(name, value){
  this.params[name] = value;
 },
 getParams: function(){
  return this.params;
 },
 addVariable: function(name, value){
  this.variables[name] = value;
 },
 getVariable: function(name){
  return this.variables[name];
 },
 getVariables: function(){
  return this.variables;
 },
 getVariablePairs: function(){
  var variablePairs = new Array();
  var key;
  var variables = this.getVariables();
  for(key in variables){
   variablePairs.push(key +"="+ variables[key]);
  }
  return variablePairs;
 },
 getSWFHTML: function() {
  var swfNode = "";
  if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
   // netscape plugin architecture   
   swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"  ';
   swfNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
   var params = this.getParams();
    for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
   var pairs = this.getVariablePairs().join("&");
    if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
   swfNode += '/>';
  } else { // PC IE   
   swfNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
   swfNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
   var params = this.getParams();
   for(var key in params) {
    swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
   }
   var pairs = this.getVariablePairs().join("&");   
   if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
   swfNode += "</object>";
  }
  return swfNode;
 },
 setDataURL: function(strDataURL){
  //This method sets the data URL for the chart.
  //If being set initially
  if (this.initialDataSet==false){
   this.addVariable('dataURL',strDataURL);
   //Update flag
   this.initialDataSet = true;
  }else{
   //Else, we update the chart data using External Interface
   //Get reference to chart object
   var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
   chartObj.setDataURL(strDataURL);
  }
 },
 setDataXML: function(strDataXML){
  //If being set initially
  if (this.initialDataSet==false){
   //This method sets the data XML for the chart INITIALLY.
   this.addVariable('dataXML',strDataXML);
   //Update flag
   this.initialDataSet = true;
  }else{
   //Else, we update the chart data using External Interface
   //Get reference to chart object
   var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
   chartObj.setDataXML(strDataXML);
  }
 },
 render: function(elementId){
  var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
  n.innerHTML = this.getSWFHTML();
  return true;  
 }
}

// ------------ Fix for Out of Memory Bug in IE in FP9 ---------------//
/* Fix for video streaming bug */
infosoftglobal.FusionChartsUtil.cleanupSWFs = function() {
 if (window.opera || !document.all) return;
 var objects = document.getElementsByTagName("OBJECT");
 for (var i=0; i < objects.length; i++) {
  objects[i].style.display = 'none';
  for (var x in objects[i]) {
   if (typeof objects[i][x] == 'function') {
    objects[i][x] = function(){};
   }
  }
 }
}
// Fixes bug in fp9
infosoftglobal.FusionChartsUtil.prepUnload = function() {
 __flash_unloadHandler = function(){};
 __flash_savedUnloadHandler = function(){};
 if (typeof window.onunload == 'function') {
  var oldUnload = window.onunload;
  window.onunload = function() {
   infosoftglobal.FusionChartsUtil.cleanupSWFs();
   oldUnload();
  }
 } else {
  window.onunload = infosoftglobal.FusionChartsUtil.cleanupSWFs;
 }
}
if (typeof window.onbeforeunload == 'function') {
 var oldBeforeUnload = window.onbeforeunload;
 window.onbeforeunload = function() {
  infosoftglobal.FusionChartsUtil.prepUnload();
  oldBeforeUnload();
 }
} else {
 window.onbeforeunload = infosoftglobal.FusionChartsUtil.prepUnload;
}

/* Add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* Function to return Flash Object from ID */
infosoftglobal.FusionChartsUtil.getChartObject = function(id)
{
  if (window.document[id]) {
      return window.document[id];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    if (document.embeds && document.embeds[id])
      return document.embeds[id];
  } else {
    return document.getElementById(id);
  }
}
/* Aliases for easy usage */
var getChartFromId = infosoftglobal.FusionChartsUtil.getChartObject;
var FusionCharts = infosoftglobal.FusionCharts;

 

下面就createChart这个静态的方法进行解读!

 createChart(String chartSWF, String strURL,String strXML, String chartId, int chartWidth,     inchartHeight,
 boolean debugMode, boolean registerWithJS)

  注意该方法需要传递的参数!

 String chartSWF就是一个flash也就是所谓的模板

 String strURL可以传递空字符串

 String strXML 就是我们用java语言写的一个特定的xml文档[注意该文档需要一定的格式!将在下面讲解]

String chartId 就是整个图形的标题[就是说明要显示图形的意图]; int chartWidth就是生成图形的宽度;

  int chartHeight就是生成图形的高度!

   boolean debugMode是否为调试模式 一般为false;

 boolean registerWithJS 是否将js一起发布一般为了保证代码的绝对安全 为false

 

具体的做法已经完成 下面就讲一下调用 在action或者servlet中  我们只需要

  String  strChartCode=FusionChartsCreator.createChart("模板", "","用java生成的xml文档","标题", 600, 500, false, false);

 PrintWriter out = response.getWriter();

out.print(strChartCode);

return null;

一个falsh图形就可以出现在你的面前了!


 

分享到:
评论

相关推荐

    java图形报表详解

    在Java编程领域,图形报表是一种重要的数据可视化工具,它能够将复杂的数据转化为易于理解的图形,帮助用户快速洞察数据背后的模式和趋势...在实际开发中,结合源码和实例,将有助于快速上手并精通Java图形报表的开发。

    JAVA 插件 jfree图形报表实例

    在这个"JAVA 插件 jfree图形报表实例"中,我们将探讨如何利用JFreeChart在Web应用中生成动态图表。 首先,JFreeChart库不仅支持基本的图表类型,还允许自定义颜色、样式和标签,以满足各种需求。在实际应用中,我们...

    java图形报表开发

    Java图形报表开发是一种在Java应用程序中创建、展示和分析数据可视化的方法。这通常涉及到使用特定的库或框架,如JFreeChart、JavaFX、Swing等,来生成各种类型的图表,如柱状图、饼图、线图和热力图等。在本专题中...

    Java中图形化报表实例

    总之,Java中图形化报表实例是学习数据可视化的宝贵资源,它涵盖了从数据获取、处理到前端展示的全过程,对于提升JavaWeb开发能力具有很大的帮助。通过深入理解和动手实践,你将能掌握这项技能,并在实际工作中...

    JFreeChartDemo java图形报表

    描述中提到的“java图形报表”,可能是指JFreeChartDemo展示了如何将数据组织成报表形式的图表,以便于理解和分析。报表通常需要清晰、直观地展示大量信息,JFreeChart能够很好地满足这一需求,它支持动态更新图表...

    java生成图形报表组件

    Java生成图形报表组件是一种强大的工具,它主要用于在Java应用程序中创建和展示各种类型的图表,如曲线图、饼状图、柱状图以及时序图。这个组件通常包含一系列的类和方法,允许开发者通过编程方式定义数据源、样式和...

    java和ireport制作报表实例

    本实例主要探讨如何使用Java与iReport工具来创建一个报表系统。iReport是一款强大的开源报表设计工具,它可以与Java应用程序紧密结合,提供丰富的报表功能。 首先,我们需要了解Java在报表生成中的角色。Java作为一...

    java报表项目demo

    【描述】提到“java图形报表例子”,意味着这个项目包含了创建和展示图形化报表的功能。图形报表有助于以视觉方式呈现复杂的数据,便于理解和分析。它还指出这个项目可以直接导入到MyEclipse环境中运行,MyEclipse是...

    java水晶报表开发

    你可以动态设置参数,以便根据用户的输入或程序逻辑生成不同的报表实例。 6. **报表展示**:填充完毕后,可以选择将报表渲染为各种格式,如HTML、PDF、Excel等,供用户查看或下载。这可以通过ExportOptions和...

    报表工具做图形报表要用到

    Highcharts是一款强大的JavaScript图表库,专门用于在Web页面上创建各种各样的图形报表。它以其易用性、灵活性和丰富的功能而备受青睐。在“报表工具做图形报表要用到”的场景下,Highcharts是一个理想的解决方案。 ...

    jspGraphic(浅析web图形报表的实现)

    总之,"jspGraphic(浅析web图形报表的实现)"这个项目提供了从数据获取、处理到图表渲染的完整流程,对于学习和实践JSP与Java图形报表结合是一个很好的起点。通过深入研究,开发者可以掌握创建高效、动态的Web图形...

    Java源码包100个设计实例.zip

    Java24点游戏逼真图形版代码.rar JavaScript万年历.rar Java二进制IO类与文件复制操作实例.rar Java从压缩包中提取文件.rar Java从网络取得文件.rar Java仓库管理系统,Access数据库.rar Java仿Vista界面风格的登录...

    java水晶报表教程

    此外,报表还可以包含地理地图和图形,使得信息的展示更加直观和生动。 对于应用程序开发者而言,将Crystal Reports集成到他们的Java应用中是一个显著的优势。这样不仅可以缩短开发周期,还能更好地满足用户对定制...

    java源码包---java 源码 大量 实例

     Java波浪文字,一个利用Java处理字符的实例,可以设置运动方向参数,显示文本的字符数组,高速文本颜色,显示字体的 FontMetrics对象,得到Graphics实例,得到Image实例,填充颜色数组数据,初始化颜色数组。...

    java生成报表JFreeChart

    Java生成报表是一个重要的任务,特别是在企业级应用中,数据可视化对于理解和分析业务至关重要。JFreeChart库正是这样一个强大的工具,它允许开发人员在Java应用程序中创建各种图表,如柱状图、饼图和折线图等。这个...

    FusionCharts报表 图形实例

    **FusionCharts报表图形实例详解** 在现代的Web开发中,数据可视化扮演着至关重要的角色。FusionCharts是一款强大的图表库,它结合了JavaScript和Flash技术,为JAVA Web应用程序提供了丰富的图形解决方案。本实例...

    java jfreechart 生成图形报表 饼图,条形图,曲线图

    Java JFreeChart 是一个强大的开源库,用于在Java应用程序或Web应用中生成高质量的图形报表。JFreeChart 支持多种图表类型,包括饼图、条形图和曲线图,这些图表在数据可视化中有着广泛的应用。在Java开发中,...

    birt图形报、报表引擎、3D报表实例,绝对震撼,看后绝对有收获

    3. **3D报表实例**:3D报表是BIRT的一个高级特性,它能为报表添加立体效果,使数据展示更具视觉冲击力。这些3D效果包括旋转、缩放、透视等,使得数据的多层次关系更加清晰。在压缩包中的`BankDemoManual[1].pdf`可能...

Global site tag (gtag.js) - Google Analytics