PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/googlecode/charts4j/PieChart.java

http://charts4j.googlecode.com/
Java | 133 lines | 50 code | 12 blank | 71 comment | 10 complexity | 3c6d23a37f9bc985987fff2fcdedf302 MD5 | raw file
  1/**
  2 *
  3 * The MIT License
  4 *
  5 * Copyright (c) 2011 the original author or authors.
  6 *
  7 * Permission is hereby granted, free of charge, to any person obtaining a copy
  8 * of this software and associated documentation files (the "Software"), to deal
  9 * in the Software without restriction, including without limitation the rights
 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11 * copies of the Software, and to permit persons to whom the Software is
 12 * furnished to do so, subject to the following conditions:
 13
 14 * The above copyright notice and this permission notice shall be included in
 15 * all copies or substantial portions of the Software.
 16
 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23 * THE SOFTWARE.
 24 */
 25
 26package com.googlecode.charts4j;
 27
 28import com.googlecode.charts4j.collect.ImmutableList;
 29import com.googlecode.charts4j.collect.Lists;
 30import com.googlecode.charts4j.parameters.ChartType;
 31
 32/**
 33 * Pie chart constructed with the {@link GCharts} static factory class.
 34 *
 35 * If the number of colors specified in the {@link Slice} objects is less than
 36 * the number of slices, then colors are interpolated. If no colors are
 37 * specified, pie segment colors are interpolated from dark orange to pale
 38 * yellow. If slices do not add up to 100, slices will be proportional to the
 39 * total of all slices.
 40 *
 41 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 42 *
 43 * @see Slice
 44 * @see GCharts
 45 */
 46public class PieChart extends AbstractGraphChart {
 47
 48    /** Is this a 3D pie chart. */
 49    private boolean                    threeD = false;
 50
 51    /** An immutable list of slices for this pie.  */
 52    private final ImmutableList<Slice> slices;
 53
 54    /** The pie chart orientation radians.  */
 55    private double orientation = Double.NaN;
 56
 57    /**
 58     * Set the pie chart orientation in radians. Positive values will rotate the
 59     * chart clockwise.
 60     *
 61     * @param radians
 62     *            the orientation to set in radians
 63     */
 64    public void setOrientation(final double radians) {
 65        this.orientation = radians;
 66    }
 67
 68    /**
 69     * Create a pie chart with the given slices. If slices do not add up to 100,
 70     * slices will be proportional to the total of all slices.
 71     *
 72     * @param slices
 73     *            list of data slices of the pie chart.
 74     *
 75     * @see Slice
 76     */
 77    PieChart(final ImmutableList<? extends Slice> slices) {
 78        super();
 79        this.slices = Lists.copyOf(slices);
 80    }
 81
 82    /**
 83     * Is this a 3D chart.
 84     *
 85     * @return the threeD
 86     */
 87    public final boolean isThreeD() {
 88        return threeD;
 89    }
 90
 91    /**
 92     * If you want the chart to be 3D, set to true.
 93     *
 94     * @param threeD
 95     *            boolean to determine if pie chart is rendered in 3D.
 96     */
 97    public final void setThreeD(final boolean threeD) {
 98        this.threeD = threeD;
 99    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    protected void prepareData() {
106        super.prepareData();
107        final double[] d = new double[slices.size()];
108        int i = 0;
109        boolean hasLegend = false;
110        double sum = 0;
111        for (Slice slice : slices) {
112            if (slice.getLegend() != null ){
113                hasLegend |= true;
114            }
115            sum += slice.getPercentage();
116        }
117
118        for (Slice slice : slices) {
119            d[i++] = (slice.getPercentage()/sum)*100;
120            parameterManager.addPieChartAndGoogleOMeterLegend(slice.getLabel() != null ? slice.getLabel() : "");
121            if (hasLegend)
122                parameterManager.addLegend(slice.getLegend() != null ? slice.getLegend() : "");
123            if (slice.getColor() != null) {
124                parameterManager.addColor(slice.getColor());
125            }
126        }
127        if (!Double.isNaN(orientation)) {
128            parameterManager.addPieChartOrientation(orientation);
129        }
130        parameterManager.addData(Data.newData(d));
131        parameterManager.setChartTypeParameter(threeD ? ChartType.THREE_D_PIE_CHART : ChartType.PIE_CHART);
132    }
133}