Java tutorial
/* * Dashboard * Copyright 2014 Christian Robert * * 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 de.perdian.apps.dashboard.support.chart; import java.awt.Font; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.codec.binary.Base64; import org.jfree.chart.ChartColor; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.TickUnits; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import de.perdian.apps.dashboard.DashboardException; /** * Helper class for creating chart output * * @author Christian Robert */ public class ChartCreator { private int width = 220; private int height = 220; private ChartColor color = new ChartColor(255, 255, 255); private String title = null; private String fontName = "Verdana"; private String rangeAxisLabel = null; private TickUnits domainTickUnits = null; private TickUnits rangeTickUnits = null; private List<ChartStrokeDefinition> strokeDefinitions = null; /** * Creates a new instance based upon the values made available in the given * request */ public ChartCreator(AbstractChartRequest request) { if (request.getImageColor() != null && request.getImageColor().length() > 0) { try { Matcher regexMatcher = Pattern.compile("rgb\\((.*?), (.*?), (.*?)\\)") .matcher(request.getImageColor()); if (regexMatcher.matches()) { this.setColor(new ChartColor(Integer.parseInt(regexMatcher.group(1).trim()), Integer.parseInt(regexMatcher.group(2).trim()), Integer.parseInt(regexMatcher.group(3).trim()))); } } catch (Exception e) { // Ignore any exception here - we simply use the color that was // assigned as default value } } this.setWidth(request.getImageWidth()); this.setHeight(request.getImageHeight()); this.setStrokeDefinitions(new ArrayList<ChartStrokeDefinition>()); } public String createChartAsImageContent(XYDataset dataset) { JFreeChart chart = this.createChart(dataset); try { ByteArrayOutputStream burndownImageStream = new ByteArrayOutputStream(); ChartUtilities.writeChartAsPNG(burndownImageStream, chart, this.getWidth(), this.getHeight()); return "data:image/png;base64," + Base64.encodeBase64String(burndownImageStream.toByteArray()); } catch (Exception e) { throw new DashboardException("Cannot create burndown chart", e); } } public JFreeChart createChart(XYDataset dataset) { JFreeChart chart = ChartFactory.createXYLineChart(this.getTitle(), null, null, dataset, PlotOrientation.VERTICAL, false, false, false); chart.setBackgroundPaint(null); chart.setBackgroundImageAlpha(0.0f); if (chart.getTitle() != null) { chart.getTitle().setFont(new Font(this.getFontName(), Font.BOLD, 14)); } XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(null); plot.setBackgroundAlpha(0.0f); plot.setOutlinePaint(this.getColor()); plot.getDomainAxis().setVisible(false); plot.getDomainAxis().setAxisLineVisible(false); plot.getDomainAxis().setMinorTickCount(10); plot.getDomainAxis().setTickMarksVisible(false); if (this.getDomainTickUnits() != null) { plot.getDomainAxis().setStandardTickUnits(this.getDomainTickUnits()); } plot.getRangeAxis().setVisible(true); plot.getRangeAxis().setAxisLineVisible(false); plot.getRangeAxis().setTickLabelFont(new Font(this.getFontName(), Font.PLAIN, 10)); plot.getRangeAxis().setTickLabelPaint(this.getColor()); plot.getRangeAxis().setTickMarksVisible(false); if (this.getRangeTickUnits() != null) { plot.getRangeAxis().setStandardTickUnits(this.getRangeTickUnits()); } plot.getRangeAxis().setAttributedLabel(this.getRangeAxisLabel()); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); for (int i = 0; i < this.getStrokeDefinitions().size(); i++) { ChartStrokeDefinition strokeDefinition = this.getStrokeDefinitions().get(i); strokeDefinition.applyTo(renderer, i); } return chart; } // ------------------------------------------------------------------------- // --- Property access methods --------------------------------------------- // ------------------------------------------------------------------------- public int getWidth() { return this.width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return this.height; } public void setHeight(int height) { this.height = height; } public ChartColor getColor() { return this.color; } public void setColor(ChartColor color) { this.color = color; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public String getFontName() { return this.fontName; } public void setFontName(String fontName) { this.fontName = fontName; } public TickUnits getDomainTickUnits() { return this.domainTickUnits; } public void setDomainTickUnits(TickUnits domainTickUnits) { this.domainTickUnits = domainTickUnits; } public TickUnits getRangeTickUnits() { return this.rangeTickUnits; } public void setRangeTickUnits(TickUnits rangeTickUnits) { this.rangeTickUnits = rangeTickUnits; } public void addStrokeDefinition(ChartStrokeDefinition strokeDefinition) { this.getStrokeDefinitions().add(strokeDefinition); } public List<ChartStrokeDefinition> getStrokeDefinitions() { return this.strokeDefinitions; } public void setStrokeDefinitions(List<ChartStrokeDefinition> strokeDefinitions) { this.strokeDefinitions = strokeDefinitions; } public String getRangeAxisLabel() { return this.rangeAxisLabel; } public void setRangeAxisLabel(String rangeAxisLabel) { this.rangeAxisLabel = rangeAxisLabel; } }