PageRenderTime 29ms CodeModel.GetById 9ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://charts4j.googlecode.com/
Java | 158 lines | 40 code | 16 blank | 102 comment | 0 complexity | a92f5759e863406f8aeb6758c9d03704 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 static com.googlecode.charts4j.collect.Preconditions.*;
 29
 30/**
 31 * Slice of pie for a {@link PieChart}. If the number of colors specified is
 32 * less than the number of slices, then colors are interpolated. If no colors
 33 * are specified, pie segment colors are interpolated from dark orange to pale
 34 * yellow.
 35 *
 36 * If slices do not add up to 100, slices will be proportional to the total of
 37 * all slices.
 38 *
 39 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 40 *
 41 * @see PieChart
 42 * @see GCharts
 43 */
 44public class Slice {
 45
 46    /** Percent of pie. **/
 47    private final int    percent;
 48
 49    /** Label for pie slice. **/
 50    private final String label;
 51
 52    /** Legend for pie slice. **/
 53    private final String legend;
 54
 55    /** Color for pie slice. **/
 56    private final Color  color;
 57
 58    /**
 59     * @see #newSlice(int, Color, String)
 60     */
 61    Slice(final int percent, final Color color, final String sliceLabel, final String sliceLegend) {
 62        this.percent = percent;
 63        this.color = color;
 64        this.label = sliceLabel;
 65        this.legend = sliceLegend;
 66    }
 67
 68    /**
 69     * Get the percentage for this slice of the pie.
 70     *
 71     * @return percentage that this slice will take of the pie.
 72     */
 73    public final int getPercentage() {
 74        return percent;
 75    }
 76
 77    /**
 78     * Get the color for this slice of the pie.
 79     *
 80     * @return color of pie slice.
 81     *
 82     */
 83    public final Color getColor() {
 84        return color;
 85    }
 86
 87    /**
 88     * Get the pie slice label.
 89     *
 90     * @return label of pie slice.
 91     */
 92    public final String getLabel() {
 93        return label;
 94    }
 95
 96    /**
 97     * Get the pie slice legend.
 98     *
 99     * @return legend of pie slice.
100     */
101    public final String getLegend() {
102        return legend;
103    }
104
105    /**
106     * Create a pie slice.
107     *
108     * @param percent
109     *            percent of pie. Must be >= 0.
110     * @param color
111     *            color of slice.
112     * @param sliceLabel
113     *            label associated with slice.
114     * @return a slice of pie.
115     */
116    public static Slice newSlice(final int percent, final Color color, final String sliceLabel) {
117        return newSlice(percent, color, sliceLabel, null);
118    }
119
120    /**
121     * Create a pie slice.
122     *
123     * @param percent
124     *            percent of pie. Must be >= 0.
125     * @param color
126     *            color of slice.
127     * @param sliceLabel
128     *            label associated with slice.
129     * @param sliceLegend
130     *            legend associated with slice.
131     * @return a slice of pie.
132     */
133    public static Slice newSlice(final int percent, final Color color, final String sliceLabel, final String sliceLegend) {
134        checkArgument(percent >= 0, "value must be between >= 0: %s", percent);
135        return new Slice(percent, color, sliceLabel, sliceLegend);
136    }
137
138    /**
139     * Use {@link #newSlice(int, Color, String, String)} instead.
140     */
141    @Deprecated //Since version 1.1. Going away in a future release.
142    public static Slice newSlice(final int percent, final Color color) {
143        return newSlice(percent, color, null, null);
144    }
145
146    /**
147     * Create a pie slice.
148     *
149     * @param percent
150     *            percent of pie. Must be >= 0.
151     * @param sliceLabel
152     *            label associated with slice.
153     * @return a slice of pie.
154     */
155    public static Slice newSlice(final int percent, final String sliceLabel) {
156        return newSlice(percent, null, sliceLabel, null);
157    }
158}