/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. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. package com.googlecode.charts4j.parameters;
  24. import java.util.List;
  25. import com.googlecode.charts4j.Color;
  26. import com.googlecode.charts4j.collect.ImmutableList;
  27. import com.googlecode.charts4j.collect.Lists;
  28. /**
  29. * Class for building chart fills parameter string for the Google Chart API.
  30. *
  31. * @author Julien Chastang (julien.c.chastang at gmail dot com)
  32. */
  33. final class ChartFillsParameter extends AbstractParameter {
  34. /** The Google Chart API chart fills parameter. */
  35. private static final String URL_PARAMETER_KEY = "chf";
  36. /** The fills. */
  37. private final List<Fill> fills = Lists.newLinkedList();
  38. /**
  39. * Adds the solid fill.
  40. *
  41. * @param solidFillType
  42. * the solid fill type
  43. * @param color
  44. * the color
  45. */
  46. void addSolidFill(final SolidFillType solidFillType, final Color color) {
  47. fills.add(new Solid(solidFillType, color));
  48. }
  49. /**
  50. * Adds the linear gradient fill.
  51. *
  52. * @param fillType
  53. * the fill type
  54. * @param angle
  55. * the angle
  56. * @param colorAndOffsets
  57. * the color and offsets
  58. */
  59. void addLinearGradientFill(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndOffset> colorAndOffsets) {
  60. fills.add(new LinearGradient(fillType, angle, colorAndOffsets));
  61. }
  62. /**
  63. * Adds the linear stripe fill.
  64. *
  65. * @param fillType
  66. * the fill type
  67. * @param angle
  68. * the angle
  69. * @param colorAndWidths
  70. * the color and widths
  71. */
  72. void addLinearStripeFill(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndWidth> colorAndWidths) {
  73. fills.add(new LinearStripes(fillType, angle, colorAndWidths));
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. @Override
  79. public String getKey() {
  80. return URL_PARAMETER_KEY;
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. @Override
  86. public String getValue() {
  87. final StringBuilder sb = new StringBuilder();
  88. int cnt = 0;
  89. for (Fill f : fills) {
  90. sb.append(cnt++ > 0 ? "|" : "").append(f);
  91. }
  92. return !fills.isEmpty() ? sb.toString() : "";
  93. }
  94. /**
  95. * The Interface Fill.
  96. */
  97. private interface Fill {
  98. }
  99. /**
  100. * The Class Solid.
  101. */
  102. private static final class Solid implements Fill {
  103. /** The solid fill type. */
  104. private final SolidFillType solidFillType;
  105. /** The color. */
  106. private final Color color;
  107. /**
  108. * Instantiates a new solid.
  109. *
  110. * @param solidFillType
  111. * the solid fill type
  112. * @param color
  113. * the color
  114. */
  115. Solid(final SolidFillType solidFillType, final Color color) {
  116. this.solidFillType = solidFillType;
  117. this.color = color;
  118. }
  119. /**
  120. * {@inheritDoc}
  121. */
  122. @Override
  123. public String toString() {
  124. return solidFillType + ",s," + color;
  125. }
  126. }
  127. /**
  128. * The Class LinearGradient.
  129. */
  130. private static final class LinearGradient implements Fill {
  131. /** The fill type. */
  132. private final FillType fillType;
  133. /** The angle. */
  134. private final int angle;
  135. /** The color and offsets. */
  136. private final ImmutableList<ColorAndOffset> colorAndOffsets;
  137. /**
  138. * Instantiates a new linear gradient.
  139. *
  140. * @param fillType
  141. * the fill type
  142. * @param angle
  143. * the angle
  144. * @param colorAndOffsets
  145. * the color and offsets
  146. */
  147. private LinearGradient(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndOffset> colorAndOffsets) {
  148. this.fillType = fillType;
  149. this.angle = angle;
  150. this.colorAndOffsets = Lists.copyOf(colorAndOffsets);
  151. }
  152. /**
  153. * {@inheritDoc}
  154. */
  155. @Override
  156. public String toString() {
  157. final StringBuilder sb = new StringBuilder();
  158. int cnt = 0;
  159. for (ColorAndOffset co : colorAndOffsets) {
  160. sb.append(cnt++ > 0 ? "," : "").append(co);
  161. }
  162. return fillType + ",lg," + angle + "," + sb.toString();
  163. }
  164. }
  165. /**
  166. * The Class LinearStripes.
  167. */
  168. private static final class LinearStripes implements Fill {
  169. /** The fill type. */
  170. private final FillType fillType;
  171. /** The angle. */
  172. private final int angle;
  173. /** The color and widths. */
  174. private final ImmutableList<ColorAndWidth> colorAndWidths;
  175. /**
  176. * Instantiates a new linear stripes.
  177. *
  178. * @param fillType
  179. * the fill type
  180. * @param angle
  181. * the angle
  182. * @param colorAndWidths
  183. * the color and widths
  184. */
  185. private LinearStripes(final FillType fillType, final int angle, final ImmutableList<? extends ColorAndWidth> colorAndWidths) {
  186. this.fillType = fillType;
  187. this.angle = angle;
  188. this.colorAndWidths = Lists.copyOf(colorAndWidths);
  189. }
  190. /**
  191. * {@inheritDoc}
  192. */
  193. @Override
  194. public String toString() {
  195. final StringBuilder sb = new StringBuilder();
  196. int cnt = 0;
  197. for (ColorAndWidth cw : colorAndWidths) {
  198. sb.append(cnt++ > 0 ? ",": "").append(cw);
  199. }
  200. return fillType + ",ls," + angle + "," + sb.toString();
  201. }
  202. }
  203. }