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

com.sun.mojarra.scales.component.YuiChart Maven / Gradle / Ivy

Go to download

This is the core for Mojarra Scales. It has everything that Scales offers minus the multi-file upload component dependencies due to their size.

The newest version!
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 * 
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License. You can obtain
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the GPL Version 2 section of the License file that
 * accompanied this code.  If applicable, add the following below the License
 * Header, with the fields enclosed by brackets [] replaced by your own
 * identifying information: "Portions Copyrighted [year]
 * [name of copyright owner]"
 * 
 * Contributor(s):
 * 
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don\"t indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
package com.sun.mojarra.scales.component;

import com.sun.mojarra.scales.util.RenderingHelper;
import com.sun.mojarra.scales.util.ScalesUtil;
import com.sun.mojarra.scales.util.YuiConstants;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 * @author Jason Lee
 *
 */
public class YuiChart extends UIOutput {

    public static final String COMPONENT_FAMILY = "com.sun.mojarra.scales.YuiChart";
    public static final String RENDERER_TYPE = COMPONENT_FAMILY;
    private Object[] _state;
    protected String type = "line";
    protected String horizontalField;
    protected String verticalField;
    protected String horizontalAxis = "";
    protected String verticalAxis = "";
    protected String labelFunction = "";
    protected String categoryField;
    protected String dataField;
    protected String seriesDef = "[{}]";
    protected String width = "500px";
    protected String height = "250px";
    protected String style = "";

    public YuiChart() {
        setRendererType(RENDERER_TYPE);

        FacesContext context = FacesContext.getCurrentInstance();
        if (context != null) {
            String baseUrl = context.getExternalContext().getRequestContextPath();
            Links.addScript(Links.EXTERNAL_SCRIPTS, YuiConstants.JS_DATASOURCE);
            Links.addScript(Links.EXTERNAL_SCRIPTS, YuiConstants.JS_JSON);
            Links.addScript(Links.EXTERNAL_SCRIPTS, YuiConstants.JS_CHARTS);
            Links.addScript(Links.DOM_READY_SCRIPTS, "YAHOO.widget.Chart.SWFURL = '" +
                    baseUrl + ScalesUtil.createResourceUrl(context, YuiConstants.OTHER_CHART_FLASH) + "';");
        }
    }

    @Override
    public String getFamily() {
        return COMPONENT_FAMILY;
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        super.encodeBegin(context);

        // suppress rendering if "rendered" property on the component is false.
        if (!isRendered()) {
            return;
        }

        ResponseWriter writer = context.getResponseWriter();
        String clientId = getClientId(context);
        writer.startElement("div", this);
        writer.writeAttribute("id", clientId, "id");
        writer.writeAttribute("style", "height: " + getAttributes().get("height") + "; width: " + getAttributes().get("width") + " " +
                getAttributes().get("style"), "style");
        writer.write("Unable to load Flash content. The chart Control requires Flash Player 9.0.45 or higher.");
        writer.endElement("div");
        RenderingHelper.writeOnDomReady(writer, "scales.createChart('" + clientId + "', '" +
                ((String)getAttributes().get("type")).toLowerCase() + "', " + buildConfig() + ");");
    }

    public String getCategoryField() {
        return categoryField;
    }

    public void setCategoryField(String categoryField) {
        this.categoryField = categoryField;
    }

    public String getDataField() {
        return dataField;
    }

    public void setDataField(String dataField) {
        this.dataField = dataField;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getHorizontalAxis() {
        return horizontalAxis;
    }

    public void setHorizontalAxis(String horizontalAxis) {
        this.horizontalAxis = horizontalAxis;
    }

    public String getHorizontalField() {
        return horizontalField;
    }

    public void setHorizontalField(String horizontalField) {
        this.horizontalField = horizontalField;
    }

    public String getLabelFunction() {
        return labelFunction;
    }

    public void setLabelFunction(String labelFunction) {
        this.labelFunction = labelFunction;
    }

    public String getSeriesDef() {
        return seriesDef;
    }

    public void setSeriesDef(String seriesDef) {
        this.seriesDef = seriesDef;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getVerticalAxis() {
        return verticalAxis;
    }

    public void setVerticalAxis(String verticalAxis) {
        this.verticalAxis = verticalAxis;
    }

    public String getVerticalField() {
        return verticalField;
    }

    public void setVerticalField(String verticalField) {
        this.verticalField = verticalField;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    protected String buildConfig() {
        Object chartValue = getValue();
        StringBuilder data = new StringBuilder();
        String sep = "";
        Set keys = null;

        if (chartValue instanceof List) {
            List list = (List) chartValue;
            if (list.get(0) instanceof Map) {
                List mapList = (List) list;
                for (Map map : mapList) {
                    keys = map.keySet();
                    String sep2 = "";

                    Iterator iter = keys.iterator();

                    data.append(sep).append("{");
                    while (iter.hasNext()) {
                        Object key = iter.next();
                        String value = (String) map.get(key);
                        String quote = Character.isDigit(value.charAt(0)) ? "" : "\"";
                        data.append(sep2).append(key.toString()).append(": ").append(quote).append(map.get(key)).append(quote);
                        sep2 = ",";
                    }
                    data.append("}");
                    sep = ",";
                }
            } else {
                throw new IllegalArgumentException(
                        "The Chart value must be a List of Map! Found List of: " + ((list.get(0) == null) ? "(null)" : list.get(0).getClass().getName()));
            }
        } else {
            throw new IllegalArgumentException(
                    "The Chart value must be a List of Map! Found: " + ((chartValue == null) ? "(null)" : chartValue.getClass().getName()));
        }

        StringBuilder params = new StringBuilder();
        params.append("{").append(prepareChartConfig()) // config
                .append("}, [").append(data.toString()) //data
                .append("],[").append(buildSeriesDef(keys)) //schema
                .append("]");

        return params.toString();
    }

    private String prepareChartConfig() {
        StringBuilder config = new StringBuilder();
        Map attrs = getAttributes();
        String chartType = ((String)attrs.get("type")).toLowerCase();

        if ("column".equals(chartType)) {
            config.append("xField: \"").append(attrs.get("horizontalField")).append("\", yField:\"").append(attrs.get("verticalField")).append("\"");
        } else if ("stackedcolumn".equals(chartType)) {
            config.append("xField: \"").append(attrs.get("horizontalField")).append("\", yField:\"").append(attrs.get("verticalField")).append("\"");
        } else if ("line".equals(chartType)) {
            config.append("xField: \"").append(attrs.get("horizontalField")).append("\"");
        } else if ("pie".equals(chartType)) {
            config.append("categoryField:\"").append(attrs.get("categoryField")).append("\", dataField:\"").append(attrs.get("dataField")).append("\"");
            appendField(config, (String)attrs.get("labelFunction"), "labelFunction");
        } else if ("bar".equals(chartType)) {
            config.append("yField:\"").append(attrs.get("verticalField")).append("\"");
        } else if ("stackedbar".equals(chartType)) {
            config.append("yField:\"").append(attrs.get("verticalField")).append("\"");
        }

        appendField(config, (String)attrs.get("seriesDef"), "series");
        appendField(config, (String)attrs.get("horizontalAxis"), "xAxis");
        appendField(config, (String)attrs.get("verticalAxis"), "yAxis");

        return config.toString();
    }

    protected void appendField(StringBuilder sb, String field, String fieldName) {
        if ((field != null) && !"".equals(field)) {
            sb.append(", ").append(fieldName).append(": ").append(field);
        }
    }

    protected String buildSeriesDef(Set keys) {
        StringBuilder sb = new StringBuilder();
        String sep = "";

        if (keys != null) {
            Iterator iter = keys.iterator();
            while (iter.hasNext()) {
                Object key = iter.next();
                sb.append(sep).append("\"").append(key.toString()).append("\"");
                sep = ",";
            }
        }

        return sb.toString();
    }

    @Override
    public void restoreState(FacesContext _context, Object _state) {
        this._state = (Object[]) _state;
        super.restoreState(_context, this._state[0]);
        type = (String) this._state[1];
        horizontalField = (String) this._state[1];
        verticalField = (String) this._state[2];
        horizontalAxis = (String) this._state[3];
        verticalAxis = (String) this._state[4];
        labelFunction = (String) this._state[5];
        categoryField = (String) this._state[6];
        dataField = (String) this._state[7];
        seriesDef = (String) this._state[8];
        width = (String) this._state[9];
        height = (String) this._state[10];
        style = (String) this._state[11];
    }

    @Override
    public Object saveState(FacesContext _context) {
        if (_state == null) {
            _state = new Object[12];
        }
        _state[0] = super.saveState(_context);
        _state[1] = horizontalField;
        _state[2] = verticalField;
        _state[3] = horizontalAxis;
        _state[4] = verticalAxis;
        _state[5] = labelFunction;
        _state[6] = categoryField;
        _state[7] = dataField;
        _state[8] = seriesDef;
        _state[9] = width;
        _state[10] = height;
        _state[11] = style;

        return _state;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy