Chart.java 17.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.graphics.Graph
 *  com.day.cq.graphics.chart.Axis
 *  com.day.cq.graphics.chart.BarChart
 *  com.day.cq.graphics.chart.Chart
 *  com.day.cq.graphics.chart.Data
 *  com.day.cq.graphics.chart.DataRow
 *  com.day.cq.graphics.chart.Grid
 *  com.day.cq.graphics.chart.LineChart
 *  com.day.cq.graphics.chart.PieChart
 *  com.day.cq.wcm.api.designer.Style
 *  com.day.image.Font
 *  com.day.image.Layer
 *  com.day.image.LineStyle
 *  com.day.image.font.AbstractFont
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.wcm.foundation;

import com.day.cq.graphics.Graph;
import com.day.cq.graphics.chart.Axis;
import com.day.cq.graphics.chart.BarChart;
import com.day.cq.graphics.chart.Data;
import com.day.cq.graphics.chart.DataRow;
import com.day.cq.graphics.chart.Grid;
import com.day.cq.graphics.chart.LineChart;
import com.day.cq.graphics.chart.PieChart;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.foundation.Table;
import com.day.image.Font;
import com.day.image.Layer;
import com.day.image.LineStyle;
import com.day.image.font.AbstractFont;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

public class Chart {
    public static final String PIE_CHART_TYPE = "pie";
    public static final String BAR_CHART_TYPE = "bar";
    public static final String LINE_CHART_TYPE = "line";
    public static final String PN_TITLE = "jcr:title";
    public static final String PN_CHART_TYPE = "chartType";
    public static final String PN_ALT = "chartAlt";
    public static final String PN_DATA = "chartData";
    public static final String PN_WIDTH = "chartWidth";
    public static final String PN_HEIGHT = "chartHeight";
    public static final String PN_PC_START_ANGLE = "pieStartAngle";
    public static final String PN_LC_LINE_WIDTH = "lineLineWidth";
    public static final String PN_BC_BAR_WIDTH = "barBarWidth";
    public static final String PN_BC_BAR_SPACING = "barBarSpacing";
    public static final String PN_BG_COLOR = "bgColor";
    public static final String PN_LABEL_COLOR = "labelColor";
    public static final String PN_LINE_COLOR = "lineColor";
    public static final String PN_DATA_COLORS = "dataColors";
    public static final Color DEFAULT_DATA_COLOR = Color.BLUE;
    private final ValueMap properties;
    private Map<String, Object> overlaid = new HashMap<String, Object>();
    private Map<String, Object> defaults = new HashMap<String, Object>();
    private Table table;
    private Color bgColor;
    private Color labelColor;
    private Color lineColor;
    private Font labelFont;
    private Font legendFont;
    private Font legendFontBold;
    private LineStyle lineStyle;
    private LineStyle gridLineStyle;
    private Double labelLsDef;
    private int labelLs;
    private double labelCs;
    private Double legendLsDef;
    private int legendLs;
    private double legendCs;
    ArrayList<String> dataColors = new ArrayList();
    private String[] labels;
    private String[] legends;
    private double[][] samples;
    private double maxValue = 0.0;

    public Chart(Resource resource) {
        this.properties = resource == null ? ValueMap.EMPTY : (ValueMap)resource.adaptTo(ValueMap.class);
        this.defaults.put("chartWidth", 400);
        this.defaults.put("chartHeight", 200);
        this.defaults.put("lineColor", "808080");
        this.defaults.put("labelColor", "808080");
        this.defaults.put("chartType", "pie");
        this.defaults.put("pieStartAngle", 0.0);
        this.defaults.put("lineLineWidth", Float.valueOf(2.0f));
        this.defaults.put("barBarWidth", 20);
        this.defaults.put("barBarSpacing", 0);
        this.initChartData();
    }

    public boolean hasData() {
        boolean hasData = false;
        if (this.table != null) {
            for (Table.Row row : this.table.getRows()) {
                for (Table.Cell cell : row.getCells()) {
                    if (cell.getText() == null || cell.getText().equals("")) continue;
                    hasData = true;
                }
            }
        }
        return hasData;
    }

    public String getAlt() {
        return this.getString("chartAlt");
    }

    public Object get(String name) {
        Object value = this.overlaid.get(name);
        if (value == null) {
            value = this.properties.get((Object)name);
        }
        if (value == null) {
            value = this.defaults.get(name);
        }
        return value;
    }

    public String getString(String name) {
        Object value = this.get(name);
        if (value != null) {
            return (String)value;
        }
        return null;
    }

    public Integer getInt(String name) {
        Object value = this.get(name);
        if (value != null) {
            if (value instanceof String) {
                return Integer.valueOf((String)value);
            }
            return (Integer)value;
        }
        return null;
    }

    public Double getDouble(String name) {
        Object value = this.get(name);
        if (value != null) {
            if (value instanceof String) {
                return Double.valueOf((String)value);
            }
            return (Double)value;
        }
        return null;
    }

    public Float getFloat(String name) {
        Object value = this.get(name);
        if (value != null) {
            if (value instanceof String) {
                return Float.valueOf((String)value);
            }
            return (Float)value;
        }
        return null;
    }

    public Color getColor(String name) {
        String value = this.getString(name);
        Color color = null;
        if (value != null && !value.equals("")) {
            if (!value.startsWith("#")) {
                value = "#" + value;
            }
            color = Color.decode(value);
        }
        return color;
    }

    public void loadStyleData(Style style) {
        if (style != null) {
            this.overlaid.put("lineColor", style.get((Object)"lineColor"));
            this.overlaid.put("labelColor", style.get((Object)"labelColor"));
            this.overlaid.put("bgColor", style.get((Object)"bgColor"));
            this.overlaid.put("dataColors", style.get((Object)"dataColors"));
            this.overlaid.put("pieStartAngle", style.get((Object)"pieStartAngle"));
            this.overlaid.put("lineLineWidth", style.get((Object)"lineLineWidth"));
            this.overlaid.put("barBarWidth", style.get((Object)"barBarWidth"));
            this.overlaid.put("barBarSpacing", style.get((Object)"barBarSpacing"));
        }
        this.initStyle();
    }

    public Layer getChartLayer() throws IOException, RepositoryException {
        Graph graph = this.getGraph();
        this.setDataColors(graph);
        Layer layer = graph.draw(true);
        layer.setTransparency(new Color(-1, true));
        return layer;
    }

    public Layer getLegendLayer() throws IOException, RepositoryException {
        Graph graph = this.getGraph();
        this.setDataColors(graph);
        try {
            graph.draw(false);
        }
        catch (Exception e) {
            // empty catch block
        }
        Layer layer = new Layer(1, 1, (Paint)this.bgColor);
        layer.setPaint((Paint)this.labelColor);
        if (this.get("chartType").equals("pie")) {
            int numlines = 0;
            String txt = this.labels[0];
            if (txt != "") {
                numlines = layer.drawText(0, 0, 165, 0, txt, (AbstractFont)this.labelFont, 65568, this.labelCs, this.labelLs);
            }
            layer.drawText(0, 0, 300, 0, "% of Total", (AbstractFont)this.labelFont, 65568, this.labelCs, this.labelLs);
            int y = numlines * (this.legendLs / 16) + this.legendLs / 32;
            ArrayList<Integer> linePos = new ArrayList<Integer>();
            linePos.add(y - 3);
            double sum = 0.0;
            for (int i = 0; i < this.legends.length; ++i) {
                txt = this.legends[i];
                numlines = layer.drawText(17, y, 185, 0, txt, (AbstractFont)this.legendFont, 65536, this.legendCs, this.legendLs);
                layer.drawText(0, y, 165, 0, String.valueOf(this.samples[i][0]), (AbstractFont)this.legendFont, 65568, this.legendCs, this.legendLs);
                txt = String.format("%.02f%%", graph.getYAxis().getTickSample(i));
                layer.drawText(0, y, 300, 0, txt, (AbstractFont)this.legendFont, 65568, this.legendCs, this.legendLs);
                Rectangle rect = new Rectangle(0, y, 15, 11);
                layer.setPaint((Paint)this.getDataColor(i));
                layer.fillRect((Rectangle2D)rect);
                layer.setPaint((Paint)this.labelColor);
                linePos.add(y + 12);
                y += numlines * (this.legendLs / 16) + this.legendLs / 32;
                sum += this.samples[i][0];
            }
            int heigth = layer.getHeight() + 7;
            layer.drawText(0, heigth, 0, 0, "Total", (AbstractFont)this.legendFontBold, 65536, this.labelCs, this.labelLs);
            txt = String.format("%s", sum);
            layer.drawText(0, heigth, 165, 0, txt, (AbstractFont)this.legendFontBold, 65568, this.labelCs, this.labelLs);
            layer.setPaint((Paint)this.lineColor);
            for (Integer ypos : linePos) {
                layer.drawLine(0.0f, (float)ypos.intValue(), 300.0f, (float)ypos.intValue());
            }
        } else {
            int y = 0;
            for (int i = 0; i < this.samples.length; ++i) {
                String txt = this.legends[i];
                int numlines = layer.drawText(20, y, 0, 0, txt, (AbstractFont)this.legendFont, 65536, this.legendCs, this.legendLs);
                Rectangle rect = new Rectangle(0, y, 15, 11);
                layer.setPaint((Paint)this.getDataColor(i));
                layer.fillRect((Rectangle2D)rect);
                layer.setPaint((Paint)this.labelColor);
                y += numlines * (this.legendLs / 16) + this.legendLs / 32;
            }
        }
        layer.setTransparency(new Color(-1, true));
        return layer;
    }

    private void initStyle() {
        this.bgColor = this.getColor("bgColor");
        this.labelColor = this.getColor("labelColor");
        this.lineColor = this.getColor("lineColor");
        this.labelFont = new Font("Myriad Pro", 9);
        this.legendFont = new Font("Myriad Pro", 9);
        this.legendFontBold = new Font("Myriad Pro", 9, 1);
        this.lineStyle = new LineStyle((Paint)this.lineColor);
        this.gridLineStyle = new LineStyle((Paint)this.lineColor, 3.0f, 2.0f, 0.2f);
        this.labelLsDef = new Double(8.0) * 16.0 * 1.3;
        this.labelLs = this.labelLsDef.intValue();
        this.labelCs = 0.0;
        this.legendLsDef = new Double(8.0) * 16.0 * 1.3;
        this.legendLs = this.legendLsDef.intValue();
        this.legendCs = 0.0;
        String dataColorString = this.getString("dataColors");
        if (dataColorString != null) {
            String[] dataColorValues;
            for (String dataColor : dataColorValues = dataColorString.split(":")) {
                this.dataColors.add("#" + dataColor);
            }
        }
    }

    private Color getDataColor(int index) {
        if (this.dataColors.size() == 0) {
            return DEFAULT_DATA_COLOR;
        }
        return Color.decode(this.dataColors.get(index % this.dataColors.size()));
    }

    private void setDataColors(Graph graph) {
        Data data = graph.getData();
        for (int row = 0; row < data.getNumrows(); ++row) {
            data.getDataRow(row).setColor(this.getDataColor(row));
        }
    }

    private Graph getGraph() {
        String chartType = this.getString("chartType");
        Graph graph = new Graph(this.getInt("chartWidth").intValue(), this.getInt("chartHeight").intValue(), chartType);
        graph.initGraphSamples(this.labels, this.legends, this.samples);
        graph.setBgColor(this.bgColor);
        graph.setGrid(new Grid(graph, this.gridLineStyle));
        int yaWidth = ("" + this.maxValue).length() * 7;
        int xaHeight = 20;
        if (this.get("chartType").equals("pie")) {
            ((PieChart)graph.getChart()).setStart(this.getDouble("pieStartAngle").doubleValue());
            ((PieChart)graph.getChart()).setHeight(this.getInt("chartHeight").intValue());
            if (this.properties.get((Object)"chartWidth") == null) {
                ((PieChart)graph.getChart()).setWidth(this.getInt("chartHeight").intValue());
            } else {
                ((PieChart)graph.getChart()).setWidth(this.getInt("chartWidth").intValue());
            }
        } else if (this.get("chartType").equals("line")) {
            ((LineChart)graph.getChart()).setLinewidth(this.getFloat("lineLineWidth").floatValue());
            ((LineChart)graph.getChart()).setNumlines(this.getInt("chartHeight") / 10);
            this.initYAxis(graph, yaWidth, xaHeight);
            int xaWidth = this.initXAxis(graph, yaWidth, xaHeight);
            int xaTickDist = xaWidth / (this.table.getNumCols() - 2);
            graph.getXAxis().setTickdistance(xaTickDist);
        } else if (this.get("chartType").equals("bar")) {
            ((BarChart)graph.getChart()).setBarwidth(this.getInt("barBarWidth").intValue());
            ((BarChart)graph.getChart()).setBarspacing(this.getInt("barBarSpacing").intValue());
            ((BarChart)graph.getChart()).setStyle(1);
            ((BarChart)graph.getChart()).setNumlines(this.getInt("chartHeight") / 10);
            this.initYAxis(graph, yaWidth, xaHeight);
            int xaWidth = this.initXAxis(graph, yaWidth, xaHeight);
            int xaTickDist = xaWidth / (this.table.getNumCols() - 1);
            graph.getXAxis().setTickdistance(xaTickDist);
            graph.getXAxis().setRangetype(4);
        }
        return graph;
    }

    private int initXAxis(Graph graph, int yaWidth, int xaHeight) {
        int xaWidth = this.getInt("chartWidth") - yaWidth;
        graph.getXAxis().setWidth(xaWidth);
        graph.getXAxis().setHeight(xaHeight);
        graph.getXAxis().setStyle(this.lineStyle);
        graph.getXAxis().setTickstyle(this.lineStyle);
        graph.getXAxis().setTickfrom(-4);
        graph.getXAxis().setTickto(0);
        graph.getXAxis().setLabelalign(32);
        graph.getXAxis().setLabelfont(this.labelFont);
        graph.getXAxis().setLabelcolor(this.labelColor);
        graph.getXAxis().setLabeltype(0);
        graph.getXAxis().setLabeldx(0);
        graph.getXAxis().setLabeldy(-4);
        return xaWidth;
    }

    private int initYAxis(Graph graph, int yaWidth, int xaHeight) {
        int yaHeight = this.getInt("chartHeight") - xaHeight;
        graph.getYAxis().setWidth(yaWidth + 5);
        graph.getYAxis().setStyle(this.lineStyle);
        int yaTickDist = this.getInt("chartHeight") / 10;
        graph.getYAxis().setTickdistance(yaTickDist);
        graph.getYAxis().setTickstyle(this.lineStyle);
        graph.getYAxis().setLabelalign(1);
        graph.getYAxis().setLabelfont(this.labelFont);
        graph.getYAxis().setLabelcolor(this.labelColor);
        graph.getYAxis().setLabeldx(- yaWidth);
        graph.getYAxis().setLabeldy(0);
        return yaHeight;
    }

    private void initChartData() {
        String data = this.getString("chartData");
        if (data != null && !data.equals("")) {
            ArrayList<String> labels = new ArrayList<String>();
            ArrayList<String> legends = new ArrayList<String>();
            ArrayList<Double[]> samples = new ArrayList<Double[]>();
            this.table = Table.fromCSV(data, ',');
            List<Table.Row> rows = this.table.getRows();
            boolean firstRow = true;
            for (int i = 0; i < rows.size(); ++i) {
                List<Table.Cell> cells = rows.get(i).getCells();
                if (firstRow) {
                    firstRow = false;
                    for (int j = 0; j < cells.size(); ++j) {
                        if (cells.get(j).getText() != null) {
                            labels.add(cells.get(j).getText());
                            continue;
                        }
                        labels.add(" ");
                    }
                    continue;
                }
                legends.add(cells.get(0).getText());
                ArrayList<Double> dataRow = new ArrayList<Double>();
                for (int j = 1; j < cells.size(); ++j) {
                    String txt = cells.get(j).getText() != null ? cells.get(j).getText() : "0.0";
                    dataRow.add(Double.valueOf(txt));
                }
                samples.add(dataRow.toArray(new Double[0]));
            }
            int width = 0;
            int height = 0;
            for (Double[] row : samples) {
                if (row.length > width) {
                    width = row.length;
                }
                ++height;
            }
            this.labels = labels.toArray(new String[0]);
            this.legends = legends.toArray(new String[0]);
            this.samples = new double[height][width];
            for (int i2 = 0; i2 < samples.size(); ++i2) {
                Double[] row2 = (Double[])samples.get(i2);
                for (int j = 0; j < row2.length; ++j) {
                    this.samples[i2][j] = row2[j];
                    if (row2[j] <= this.maxValue) continue;
                    this.maxValue = row2[j];
                }
            }
        }
    }
}