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.renderer.AbstractRenderer;
024
025 import java.awt.*;
026 import java.util.Map;
027 import java.util.StringTokenizer;
028
029 public abstract class BaseChartWeb extends BaseChart {
030
031 // Chart types
032 public static final String BAR_CHART_HORIZONTAL = "hb";
033 public static final String BAR_CHART_VERTICAL = "vb";
034 public static final String BAR_CHART_VERTICAL_CUSTOM = "cvb";
035 public static final String STACKED_BAR_CHART = "sb";
036 public static final String PIE_CHART = "p";
037 public static final String SPARKLINES_CHART = "sl";
038
039 // Chart params
040 public static final String CHART_PARAM_TYPE = "cht";
041 public static final String CHART_PARAM_VALUES = "chv";
042 public static final String CHART_PARAM_COLORS = "chc";
043 public static final String CHART_PARAM_RANGEMAX = "chrm";
044 public static final String CHART_PARAM_TITLE = "chti";
045 public static final String CHART_PARAM_DIMENSIONS = "chdi";
046 public static final String CHART_PARAM_RANGEAXIS_VISIBLE = "chrav";
047 public static final String CHART_PARAM_CATEGORIES = "chca";
048 public static final String CHART_PARAM_CATEGORIES_AXISMARGIN_VISIBLE = "chcav";
049 public static final String CHART_PARAM_CATEGORIES_AXISMARGIN_UPPER = "chcaamu";
050 public static final String CHART_PARAM_CATEGORIES_AXISMARGIN_LOWER = "chcaaml";
051 public static final String CHART_PARAM_SERIES = "chse";
052 public static final String CHART_PARAM_SERIES_AXISMARGIN_UPPER = "chseamu";
053 public static final String CHART_PARAM_SERIES_AXISMARGIN_LOWER = "chseaml";
054 public static final String CHART_PARAM_SERIES_AXISMARGIN_TICKUNIT = "chsetu";
055 public static final String CHART_PARAM_INSETS = "chins";
056 public static final String CHART_PARAM_OUTLINE_VISIBLE = "chov";
057 public static final String CHART_PARAM_OUTLINE_RANGEGRIDLINES_VISIBLE = "chorgv";
058
059 // Default labels
060 public static final String DEFAULT_NAME_CATEGORY = "category";
061 public static final String DEFAULT_NAME_SERIE = "serie";
062 public static final String DEFAULT_MESSAGE_NODATA = "No data available";
063
064 // Default values
065 public static final double DEFAULT_CATEGORIES_AXISMARGIN = 0.0;
066 public static final double DEFAULT_SERIES_AXISMARGIN = 0.0;
067
068 // Default dimensions
069 public static final int DEFAULT_WIDTH = 60;
070 public static final int DEFAULT_HEIGHT = 20;
071
072 // Default font
073 public static final Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 13);
074
075 protected JFreeChart jfreechart = null;
076 protected Map<String, String> params = null;
077
078 public BaseChartWeb(Map<String, String> params) {
079 super(DEFAULT_WIDTH, DEFAULT_HEIGHT);
080 this.params = params;
081 }
082
083 protected boolean isParamValueValid(String paramValue) {
084 return paramValue != null && paramValue.length() > 0;
085 }
086
087 protected double convertParamToDouble(String paramValue) {
088 return convertParamToDouble(paramValue, 0.0);
089 }
090
091 protected void configureColors(String colors, AbstractRenderer renderer) {
092 try {
093 if (colors != null && colors.length() > 0) {
094 StringTokenizer stringTokenizer = new StringTokenizer(colors, ",");
095 int i = 0;
096 while (stringTokenizer.hasMoreTokens()) {
097 renderer.setSeriesPaint(i, Color.decode("0x" + stringTokenizer.nextToken()));
098 i++;
099 }
100 } else {
101 configureDefaultColors(renderer);
102 }
103 }
104 catch (Exception e) {
105 configureDefaultColors(renderer);
106 }
107 }
108
109 protected void configureDefaultColors(AbstractRenderer renderer) {
110 for (int i=0 ; i<COLORS.length ; i++) {
111 renderer.setSeriesPaint(i, COLORS[i]);
112 }
113 }
114
115 protected double convertParamToDouble(String paramValue, double paramDefault) {
116 double result = paramDefault;
117 if (isParamValueValid(paramValue)) {
118 try {
119 result = Double.parseDouble(paramValue);
120 }
121 catch (NumberFormatException e) {
122 // ignore
123 }
124 }
125 return result;
126 }
127
128
129 protected void configureDimensions(String dimensions) {
130 try {
131 if (dimensions == null || dimensions.length() == 0) {
132 // Do nothing, default dimensions are already setted
133 } else if (dimensions.indexOf('x') == -1) {
134 int iDim = Integer.parseInt(dimensions);
135 setWidth(iDim);
136 setHeight(iDim);
137 } else {
138 StringTokenizer st = new StringTokenizer(dimensions, "x");
139 int iWidth = Integer.parseInt(st.nextToken());
140 int iHeight = iWidth;
141 if (st.hasMoreTokens()) {
142 iHeight = Integer.parseInt(st.nextToken());
143 }
144 setWidth(iWidth);
145 setHeight(iHeight);
146 }
147 }
148 catch (NumberFormatException e) {
149 // Do nothing, default dimensions are already setted
150 }
151 }
152
153 protected void applyCommonParams() {
154 configureChartTitle(jfreechart, params.get(BaseChartWeb.CHART_PARAM_TITLE));
155 configureDimensions(params.get(BaseChartWeb.CHART_PARAM_DIMENSIONS));
156 }
157
158 }