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

com.janeluo.jfinalplus.render.chart.amchart.Creater 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.amchart;

import com.janeluo.jfinalplus.kit.KeyLabel;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

import java.util.List;

public class Creater {

    public static boolean isFormat = true;
    protected static final Log LOG = Log.getLog(Creater.class);

    private Creater() {
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static String createMultipleChart(GraphChart chart) {
        StringBuffer strXML = new StringBuffer("").append(newLine())
                .append("").append(newLine());
        strXML.append(newLine());
        strXML = appendSeries(strXML, chart.getSeriesNames());
        List value = chart.getValues();
        if (value != null && value.size() > 0) {
            if (value.get(0) instanceof String) {
                strXML = appendSimpleGraphs(strXML, value);
            } else if (value.get(0) instanceof List) {
                strXML = appendMultipleGraphs(strXML, value);
            }
        }
        strXML.append(newLine()).append("");
        return strXML.toString();
    }

    public static String createPieChart(PieChart chart) {
        StringBuffer strXML = new StringBuffer("").append(newLine());
        strXML.append("").append(newLine());
        List pies = chart.getPies();
        for (KeyLabel pie : pies) {
            String label = pie.getLabel();
            if (StrKit.isBlank(label)) {
                label = "0";
            }
            strXML.append(space(1)).append("").append(pie.getLabel())
                    .append("").append(newLine());
        }
        strXML.append("");
        return strXML.toString();
    }

    private static StringBuffer appendMultipleGraphs(StringBuffer strXML, List> values) {
        if (values == null) {
            return strXML;
        }
        strXML.append(space(1)).append("");
        for (int i = 0, size = values.size(); i < size; i++) {
            strXML.append(newLine()).append(space(2)).append("");
            List value = values.get(i);
            for (int j = 0; j < value.size(); j++) {
                String val = value.get(j);
                if (StrKit.isBlank(val)) {
                    val = "0";
                }
                strXML.append(newLine()).append(space(3)).append("").append(val)
                        .append("");
            }
            strXML.append(newLine()).append(space(2)).append("");
        }
        strXML.append(newLine()).append(space(1)).append("");
        return strXML;
    }

    /**
     * 生成x轴描述
     *
     * @param strXML
     * @param seriesNames
     *            描述的list
     * @return
     */
    private static StringBuffer appendSeries(StringBuffer strXML, List seriesNames) {
        if (seriesNames == null) {
            return strXML;
        }
        strXML.append(space(1)).append("").append(newLine());
        for (int i = 0, size = seriesNames.size(); i < size; i++) {
            String str = seriesNames.get(i);
            strXML.append(space(2)).append("").append(str).append("")
                    .append(newLine());
        }
        strXML.append(space(1)).append("").append(newLine());
        return strXML;
    }

    /**
     * 生成报表图形元素
     *
     * @param strXML
     * @param list
     * @return
     */
    private static StringBuffer appendSimpleGraphs(StringBuffer strXML, List list) {
        if (list == null) {
            return strXML;
        }
        strXML.append(space(1)).append("");
        strXML.append(newLine()).append(space(2)).append("");
        for (int i = 0, size = list.size(); i < size; i++) {
            String value = list.get(i);
            if (StrKit.isBlank(value)) {
                value = "0";
            }
            strXML.append(newLine()).append(space(3)).append("").append(value)
                    .append("");
        }
        strXML.append(newLine()).append(space(2)).append("");
        strXML.append(newLine()).append(space(1)).append("");
        return strXML;
    }

    /**
     * 生成缩进
     *
     * @param level
     *            缩进的级别(1级4个空格)
     * @return
     *
     */
    private static StringBuffer space(int level) {
        StringBuffer sb = new StringBuffer();
        if (!isFormat) {
            return sb;
        }
        while (level-- > 0) {
            sb.append("    ");
        }
        return sb;
    }

    /**
     * 生成换行符 Description: 
* * @return */ private static StringBuffer newLine() { StringBuffer sb = new StringBuffer(); if (!isFormat) { return sb; } return sb; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy