All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.janeluo.jfinalplus.render.chart.funshion.FusionCharts Maven / Gradle / Ivy

There is a newer version: 2.2.0.r3
Show newest version
/**
 * Copyright (c) 2011-2013, kidzhou 周磊 ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.janeluo.jfinalplus.render.chart.funshion;

import javax.servlet.http.HttpServletResponse;

public class FusionCharts {
    /**
     * Encodes the dataURL before it's served to FusionCharts. If you have parameters in your dataURL, you necessarily need to encode it.
     *
     * @param strDataURL
     *            - dataURL to be fed to chart
     * @param addNoCacheStr
     *            - Whether to add aditional string to URL to disable caching of data
     * @return
     */

    public String encodeDataURL(String strDataURL, String addNoCacheStr, HttpServletResponse response) {
        String encodedURL = strDataURL;
        // Add the no-cache string if required
        if (addNoCacheStr.equals("true")) {
            /*
             * We add ?FCCurrTime=xxyyzz If the dataURL already contains a ?, we add &FCCurrTime=xxyyzz We send the date separated with '_',
             * instead of the usual ':' as FusionCharts cannot handle : in URLs
             */
            java.util.Calendar nowCal = java.util.Calendar.getInstance();
            java.util.Date now = nowCal.getTime();
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM/dd/yyyy HH_mm_ss a");
            String strNow = sdf.format(now);
            if (strDataURL.indexOf("?") > 0) {
                encodedURL = strDataURL + "&FCCurrTime=" + strNow;
            } else {
                strDataURL = strDataURL + "?FCCurrTime=" + strNow;
            }
            encodedURL = response.encodeURL(strDataURL);

        }
        return encodedURL;
    }

    /**
     * Creates the Chart HTML+Javascript to create the FusionCharts object with the given parameters. This method uses JavaScript to
     * overcome the IE browser problem with SWF wherein you have to 'Click to activate' the control
     *
     * @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 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("\n");
        strBuf.append("\t\t
\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
\n"); /* * Now, we render the chart using FusionCharts Class. Each chart's instance (JavaScript) Id is named as chart_"chartId". */ strBuf.append("\t\t\n"); strBuf.append("\t\t\n"); return strBuf.substring(0); } /** * Creates the Chart HTML to embed the swf object with the given parameters * * @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 String createChartHTML(String chartSWF, String strURL, String strXML, String chartId, String chartWidth, String 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\n"); strBuf.append("\t\t\t\t\n"); strBuf.append("\t\t\t\t \n"); strBuf.append("\t\t\t\t \n"); strBuf.append("\t\t\t\t \n"); strBuf.append("\t\t\t\t \n"); strBuf.append("\t\t\t\t \n"); strBuf.append("\t\t\n"); // END Code Block for Chart strBuf.append("\t\t\n"); return strBuf.substring(0); } private int boolToNum(Boolean bool) { int num = 0; if (bool.booleanValue()) { num = 1; } return num; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy