001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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 = {
041 new Color(5, 141, 199),
042 new Color(80, 180, 50),
043 new Color(237, 86, 27),
044 new Color(237, 239, 0),
045 new Color(36, 203, 229),
046 new Color(100, 229, 114),
047 new Color(255, 150, 85)
048 };
049
050 public static final int FONT_SIZE = 13;
051
052 private int width;
053 private int height;
054
055 protected BaseChart(int width, int height) {
056 this.width = width;
057 this.height = height;
058 }
059
060 public int getWidth() {
061 return width;
062 }
063
064 public int getHeight() {
065 return height;
066 }
067
068 public void setWidth(int width) {
069 this.width = width;
070 }
071
072 public void setHeight(int height) {
073 this.height = height;
074 }
075
076 protected Font getFont() {
077 return new Font("SansSerif", Font.PLAIN, FONT_SIZE);
078 }
079
080 protected void configureChart(JFreeChart chart, boolean displayLegend) {
081 if (displayLegend) {
082 configureChart(chart, RectangleEdge.BOTTOM);
083 } else {
084 configureChart(chart, null);
085 }
086 }
087
088 protected void configureChart(JFreeChart chart, RectangleEdge legendPosition) {
089 chart.setBackgroundPaint(new Color(255, 255, 255, 0));
090 chart.setBackgroundImageAlpha(0.0f);
091 chart.setBorderVisible(false);
092 chart.setAntiAlias(true);
093 chart.setTextAntiAlias(true);
094
095 chart.removeLegend();
096 if (legendPosition != null) {
097 LegendTitle legend = new LegendTitle(chart.getPlot());
098 legend.setPosition(legendPosition);
099 legend.setItemPaint(BASE_COLOR);
100 chart.addSubtitle(legend);
101 }
102 }
103
104 protected void configureChartTitle(JFreeChart chart, String title) {
105 if (title != null && title.length() > 0) {
106 TextTitle textTitle = new TextTitle(title);
107 chart.setTitle(textTitle);
108 }
109 }
110
111 protected abstract BufferedImage getChartImage() throws IOException;
112
113 protected BufferedImage getBufferedImage(JFreeChart chart) {
114 return chart.createBufferedImage(getWidth(), getHeight(), Transparency.BITMASK, null);
115 }
116
117 public void exportChartAsPNG(OutputStream out) throws IOException {
118 KeypointPNGEncoderAdapter encoder = new KeypointPNGEncoderAdapter();
119 encoder.setEncodingAlpha(true);
120 encoder.encode(getChartImage(), out);
121 }
122
123 public byte[] exportChartAsPNG() throws IOException {
124 ByteArrayOutputStream output = new ByteArrayOutputStream();
125 try {
126 exportChartAsPNG(output);
127 } finally {
128 output.close();
129 }
130 return output.toByteArray();
131 }
132
133 protected BasicStroke getDashedStroke() {
134 return getDashedStroke(1f);
135 }
136
137 protected BasicStroke getDashedStroke(float width) {
138 return new BasicStroke(width,
139 BasicStroke.CAP_BUTT,
140 BasicStroke.JOIN_MITER,
141 10.0f, new float[] { 5.0f }, 0.0f);
142 }
143 }