PageRenderTime 43ms CodeModel.GetById 16ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/googlecode/charts4j/parameters/ChartFillsParameter.java

http://charts4j.googlecode.com/
Java | 235 lines | 83 code | 30 blank | 122 comment | 3 complexity | ce882fb287551898763c021ca5c8b5b6 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.parameters;
 27
 28import java.util.List;
 29
 30import com.googlecode.charts4j.Color;
 31import com.googlecode.charts4j.collect.ImmutableList;
 32import com.googlecode.charts4j.collect.Lists;
 33
 34/**
 35 * Class for building chart fills parameter string for the Google Chart API.
 36 *
 37 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 38 */
 39final class ChartFillsParameter extends AbstractParameter {
 40
 41    /** The Google Chart API chart fills parameter. */
 42    private static final String URL_PARAMETER_KEY = "chf";
 43
 44    /** The fills. */
 45    private final List<Fill>    fills             = Lists.newLinkedList();
 46
 47    /**
 48     * Adds the solid fill.
 49     *
 50     * @param solidFillType
 51     *            the solid fill type
 52     * @param color
 53     *            the color
 54     */
 55    void addSolidFill(final SolidFillType solidFillType, final Color color) {
 56        fills.add(new Solid(solidFillType, color));
 57    }
 58
 59    /**
 60     * Adds the linear gradient fill.
 61     *
 62     * @param fillType
 63     *            the fill type
 64     * @param angle
 65     *            the angle
 66     * @param colorAndOffsets
 67     *            the color and offsets
 68     */
 69    void addLinearGradientFill(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndOffset> colorAndOffsets) {
 70        fills.add(new LinearGradient(fillType, angle, colorAndOffsets));
 71    }
 72
 73    /**
 74     * Adds the linear stripe fill.
 75     *
 76     * @param fillType
 77     *            the fill type
 78     * @param angle
 79     *            the angle
 80     * @param colorAndWidths
 81     *            the color and widths
 82     */
 83    void addLinearStripeFill(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndWidth> colorAndWidths) {
 84        fills.add(new LinearStripes(fillType, angle, colorAndWidths));
 85    }
 86
 87    /**
 88     * {@inheritDoc}
 89     */
 90    @Override
 91    public String getKey() {
 92        return URL_PARAMETER_KEY;
 93    }
 94
 95    /**
 96     * {@inheritDoc}
 97     */
 98    @Override
 99    public String getValue() {
100        final StringBuilder sb = new StringBuilder();
101        int cnt = 0;
102        for (Fill f : fills) {
103            sb.append(cnt++ > 0 ? "|" : "").append(f);
104        }
105        return !fills.isEmpty() ? sb.toString() : "";
106    }
107
108    /**
109     * The Interface Fill.
110     */
111    private interface Fill {
112    }
113
114    /**
115     * The Class Solid.
116     */
117    private static final class Solid implements Fill {
118
119        /** The solid fill type. */
120        private final SolidFillType solidFillType;
121
122        /** The color. */
123        private final Color         color;
124
125        /**
126         * Instantiates a new solid.
127         *
128         * @param solidFillType
129         *            the solid fill type
130         * @param color
131         *            the color
132         */
133        Solid(final SolidFillType solidFillType, final Color color) {
134            this.solidFillType = solidFillType;
135            this.color = color;
136        }
137
138        /**
139         * {@inheritDoc}
140         */
141        @Override
142        public String toString() {
143            return solidFillType + ",s," + color;
144        }
145    }
146
147    /**
148     * The Class LinearGradient.
149     */
150    private static final class LinearGradient implements Fill {
151
152        /** The fill type. */
153        private final FillType             fillType;
154
155        /** The angle. */
156        private final int                  angle;
157
158        /** The color and offsets. */
159        private final ImmutableList<ColorAndOffset> colorAndOffsets;
160
161        /**
162         * Instantiates a new linear gradient.
163         *
164         * @param fillType
165         *            the fill type
166         * @param angle
167         *            the angle
168         * @param colorAndOffsets
169         *            the color and offsets
170         */
171        private LinearGradient(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndOffset> colorAndOffsets) {
172            this.fillType = fillType;
173            this.angle = angle;
174            this.colorAndOffsets = Lists.copyOf(colorAndOffsets);
175        }
176
177        /**
178         * {@inheritDoc}
179         */
180        @Override
181        public String toString() {
182            final StringBuilder sb = new StringBuilder();
183            int cnt = 0;
184            for (ColorAndOffset co : colorAndOffsets) {
185                sb.append(cnt++ > 0 ? "," : "").append(co);
186            }
187            return fillType + ",lg," + angle + "," + sb.toString();
188        }
189    }
190
191    /**
192     * The Class LinearStripes.
193     */
194    private static final class LinearStripes implements Fill {
195
196        /** The fill type. */
197        private final FillType            fillType;
198
199        /** The angle. */
200        private final int                 angle;
201
202        /** The color and widths. */
203        private final ImmutableList<ColorAndWidth> colorAndWidths;
204
205        /**
206         * Instantiates a new linear stripes.
207         *
208         * @param fillType
209         *            the fill type
210         * @param angle
211         *            the angle
212         * @param colorAndWidths
213         *            the color and widths
214         */
215        private LinearStripes(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndWidth> colorAndWidths) {
216            this.fillType = fillType;
217            this.angle = angle;
218            this.colorAndWidths = Lists.copyOf(colorAndWidths);
219        }
220
221        /**
222         * {@inheritDoc}
223         */
224        @Override
225        public String toString() {
226            final StringBuilder sb = new StringBuilder();
227            int cnt = 0;
228            for (ColorAndWidth cw : colorAndWidths) {
229                sb.append(cnt++ > 0 ? ",": "").append(cw);
230            }
231
232            return fillType + ",ls," + angle + "," + sb.toString();
233        }
234    }
235}