001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.server.charts.deprecated;
021
022 import org.jfree.chart.JFreeChart;
023 import org.jfree.chart.encoders.KeypointPNGEncoderAdapter;
024 import org.jfree.chart.title.LegendTitle;
025 import org.jfree.chart.title.TextTitle;
026 import org.jfree.ui.RectangleEdge;
027
028 import java.awt.*;
029 import java.awt.image.BufferedImage;
030 import java.io.ByteArrayOutputStream;
031 import java.io.IOException;
032 import java.io.OutputStream;
033
034 public abstract class BaseChart {
035
036 public static final Color BASE_COLOR = new Color(51, 51, 51);
037 public static final Color BASE_COLOR_LIGHT = new Color(204, 204, 204);
038 public static final Color SERIE_BORDER_COLOR = new Color(67, 119, 166);
039
040 public static final Color[] COLORS = {new Color(5, 141, 199), new Color(80,180,50),new Color(237,86,27), new Color(237,239,0), new Color(36,203,229), new Color(100,229,114), new Color(255,150,85)};
041
042 public static final int FONT_SIZE = 13;
043
044 private int width;
045 private int height;
046
047 protected BaseChart(int width, int height) {
048 this.width = width;
049 this.height = height;
050 }
051
052 public int getWidth() {
053 return width;
054 }
055
056 public int getHeight() {
057 return height;
058 }
059
060 public void setWidth(int width) {
061 this.width = width;
062 }
063
064 public void setHeight(int height) {
065 this.height = height;
066 }
067
068 protected Font getFont() {
069 return new Font("SansSerif", Font.PLAIN, FONT_SIZE);
070 }
071
072 protected void configureChart(JFreeChart chart, boolean displayLegend) {
073 if (displayLegend) {
074 configureChart(chart, RectangleEdge.BOTTOM);
075 } else {
076 configureChart(chart, null);
077 }
078 }
079
080 protected void configureChart(JFreeChart chart, RectangleEdge legendPosition) {
081 chart.setBackgroundPaint(new Color(255, 255, 255, 0));
082 chart.setBackgroundImageAlpha(0.0f);
083 chart.setBorderVisible(false);
084 chart.setAntiAlias(true);
085 chart.setTextAntiAlias(true);
086
087 chart.removeLegend();
088 if (legendPosition != null) {
089 LegendTitle legend = new LegendTitle(chart.getPlot());
090 legend.setPosition(legendPosition);
091 legend.setItemPaint(BASE_COLOR);
092 chart.addSubtitle(legend);
093 }
094 }
095
096 protected void configureChartTitle(JFreeChart chart, String title) {
097 if (title != null && title.length() > 0) {
098 TextTitle textTitle = new TextTitle(title);
099 chart.setTitle(textTitle);
100 }
101 }
102
103 protected abstract BufferedImage getChartImage() throws IOException;
104
105 protected BufferedImage getBufferedImage(JFreeChart chart) {
106 return chart.createBufferedImage(getWidth(), getHeight(), Transparency.BITMASK, null);
107 }
108
109 public void exportChartAsPNG(OutputStream out) throws IOException {
110 KeypointPNGEncoderAdapter encoder = new KeypointPNGEncoderAdapter();
111 encoder.setEncodingAlpha(true);
112 encoder.encode(getChartImage(), out);
113 }
114
115 public byte[] exportChartAsPNG() throws IOException {
116 ByteArrayOutputStream output = new ByteArrayOutputStream();
117 try {
118 exportChartAsPNG(output);
119 } finally {
120 output.close();
121 }
122 return output.toByteArray();
123 }
124
125 protected BasicStroke getDashedStroke() {
126 return getDashedStroke(1f);
127 }
128
129 protected BasicStroke getDashedStroke(float width) {
130 return new BasicStroke(width,
131 BasicStroke.CAP_BUTT,
132 BasicStroke.JOIN_MITER,
133 10.0f, new float[]{5.0f}, 0.0f);
134 }
135 }