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.plot.PiePlot; 024 import org.jfree.chart.title.TextTitle; 025 import org.jfree.data.general.DefaultPieDataset; 026 import org.jfree.ui.RectangleInsets; 027 028 import java.awt.*; 029 import java.awt.image.BufferedImage; 030 import java.io.IOException; 031 import java.util.Map; 032 import java.util.StringTokenizer; 033 034 public class PieChart extends BaseChartWeb implements DeprecatedChart { 035 036 private DefaultPieDataset dataset = null; 037 038 public PieChart(Map<String, String> params) { 039 super(params); 040 jfreechart = new JFreeChart(null, TextTitle.DEFAULT_FONT, new PiePlot(), false); 041 } 042 043 @Override 044 protected BufferedImage getChartImage() throws IOException { 045 configure(); 046 return getBufferedImage(jfreechart); 047 } 048 049 private void configure() { 050 configureChart(jfreechart, false); 051 configureDataset(); 052 configurePlot(); 053 applyParams(); 054 } 055 056 private void configureDataset() { 057 dataset = new DefaultPieDataset(); 058 } 059 060 private void configurePlot() { 061 PiePlot plot = (PiePlot) jfreechart.getPlot(); 062 plot.setNoDataMessage(DEFAULT_MESSAGE_NODATA); 063 plot.setInsets(RectangleInsets.ZERO_INSETS); 064 plot.setDataset(dataset); 065 plot.setBackgroundAlpha(0.0f); 066 plot.setCircular(true); 067 plot.setLabelGenerator(null); 068 plot.setIgnoreNullValues(true); 069 plot.setIgnoreZeroValues(true); 070 plot.setShadowPaint(null); 071 plot.setLabelLinkMargin(0.0); 072 plot.setInteriorGap(0.02); 073 plot.setMaximumLabelWidth(0.10); 074 } 075 076 private void configureColors(String colors) { 077 try { 078 if (colors != null && colors.length() > 0) { 079 StringTokenizer stringTokenizer = new StringTokenizer(colors, ","); 080 int i = 0; 081 while (stringTokenizer.hasMoreTokens()) { 082 ((PiePlot) jfreechart.getPlot()).setSectionPaint(Integer.toString(i), Color.decode("0x" + stringTokenizer.nextToken())); 083 i++; 084 } 085 } else { 086 configureDefaultColors(); 087 } 088 } 089 catch (Exception e) { 090 configureDefaultColors(); 091 } 092 } 093 094 private void configureDefaultColors() { 095 PiePlot plot = (PiePlot) jfreechart.getPlot(); 096 for (int i=0 ; i<COLORS.length ; i++) { 097 plot.setSectionPaint("" + i, COLORS[i]); 098 } 099 } 100 101 private void applyParams() { 102 applyCommonParams(); 103 104 configureColors(params.get(CHART_PARAM_COLORS)); 105 addMeasures(params.get(CHART_PARAM_VALUES)); 106 107 // -- Plot 108 PiePlot plot = (PiePlot) jfreechart.getPlot(); 109 plot.setOutlineVisible(isParamValueValid(params.get(CHART_PARAM_OUTLINE_VISIBLE)) && Boolean.getBoolean(params.get(CHART_PARAM_OUTLINE_VISIBLE))); 110 } 111 112 private void addMeasures(String values) { 113 if (values != null && values.length() > 0) { 114 StringTokenizer st = new StringTokenizer(values, ","); 115 int i = 0; 116 while (st.hasMoreTokens()) { 117 double measure = 0; 118 try { 119 measure = Double.parseDouble(st.nextToken()); 120 } 121 catch (NumberFormatException e) { 122 } 123 dataset.setValue(Integer.toString(i), measure); 124 i++; 125 } 126 } 127 } 128 129 }