PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/ofc4j/model/elements/StackedBarChart.java

http://ofcj.googlecode.com/
Java | 233 lines | 129 code | 39 blank | 65 comment | 3 complexity | 355ab694486d7c46af8f477a416db4be MD5 | raw file
  1. /*
  2. This file is part of OFC4J.
  3. OFC4J is free software: you can redistribute it and/or modify
  4. it under the terms of the Lesser GNU General Public License as
  5. published by the Free Software Foundation, either version 3 of
  6. the License, or (at your option) any later version.
  7. OFC4J is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. See <http://www.gnu.org/licenses/lgpl-3.0.txt>.
  12. */
  13. package ofc4j.model.elements;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.List;
  17. import ofc4j.model.metadata.Alias;
  18. import ofc4j.model.metadata.Converter;
  19. import ofc4j.util.StackKeyConverter;
  20. import ofc4j.util.StackValueConverter;
  21. /**
  22. * The stacked bar chart allows you to draw a bar chart divided
  23. * into value regions.
  24. */
  25. /*
  26. * Implementation note: the Stack class wraps standard List objects.
  27. * The List objects are preserved in the element value field rather than
  28. * the Stack object itself so that XStream renders the data correctly. I
  29. * didn't have much luck trying to use a custom converter or an implicit
  30. * collection.
  31. */
  32. public class StackedBarChart extends Element {
  33. private List<StackKey> keys = new ArrayList<StackKey>();
  34. public StackedBarChart() {
  35. super("bar_stack");
  36. }
  37. /**
  38. * Add stacks to the chart (var-args version).
  39. * @param stacks the stacks that have not yet been placed into the chart
  40. * @return the chart element object being operated on
  41. */
  42. public StackedBarChart addStack(Stack... stacks) {
  43. return copy(Arrays.asList(stacks));
  44. }
  45. /**
  46. * Add stacks to the chart (Collections version).
  47. * @param stacks the stacks that have not yet been placed into the chart
  48. * @return the chart element object being operated on
  49. */
  50. public StackedBarChart addStack(List<Stack> stacks) {
  51. return copy(stacks);
  52. }
  53. public StackedBarChart addKeys(StackKey... keys)
  54. {
  55. this.keys.addAll(Arrays.asList(keys));
  56. return this;
  57. }
  58. /**
  59. * Create a stack and add it into the chart. You do not need to
  60. * pass this Stack object to addStack.
  61. * @return the stack that has been created in the chart
  62. */
  63. public Stack newStack() {
  64. Stack s = new Stack();
  65. copy(Arrays.asList(s));
  66. return s;
  67. }
  68. /**
  69. * Find the most recently created stack, or create one if
  70. * there are none.
  71. * @return the last stack in the chart
  72. */
  73. public Stack lastStack() {
  74. if (getValues().isEmpty()) {
  75. return newStack();
  76. } else {
  77. return stack(getStackCount() - 1);
  78. }
  79. }
  80. /**
  81. * Find an arbitrary stack by index number. (Starts at 0.)
  82. * @param index the index of the stack, 0 to getStackCount() - 1.
  83. * @return the stack at the specified index
  84. */
  85. @SuppressWarnings("unchecked")
  86. public Stack stack(int index) {
  87. return new Stack((List<Object>) getValues().get(index));
  88. }
  89. /**
  90. * The number of stacks in the chart.
  91. * @return the number of stacks in the chart
  92. */
  93. public int getStackCount() {
  94. return getValues().size();
  95. }
  96. private StackedBarChart copy(List<Stack> stacks) {
  97. for (Stack s : stacks) {
  98. getValues().add(s.getBackingList());
  99. }
  100. return this;
  101. }
  102. /**
  103. * Representation of a stack in the chart. This class allows
  104. * you to add numbers or complex values with custom data.
  105. */
  106. public static class Stack {
  107. private transient List<Object> values;
  108. public Stack() {
  109. values = new ArrayList<Object>();
  110. }
  111. Stack(List<Object> values) {
  112. this.values = values;
  113. }
  114. public Stack addStackValues(StackValue... values) {
  115. return doAdd(Arrays.asList(values));
  116. }
  117. public Stack addStackValues(List<StackValue> values) {
  118. return doAdd(values);
  119. }
  120. public Stack addValues(Number... numbers) {
  121. return doAdd(Arrays.asList(numbers));
  122. }
  123. public Stack addValues(List<Number> numbers) {
  124. return doAdd(numbers);
  125. }
  126. private Stack doAdd(List<? extends Object> values) {
  127. this.values.addAll(values);
  128. return this;
  129. }
  130. List<Object> getBackingList() {
  131. return this.values;
  132. }
  133. }
  134. /**
  135. * Representation of data in the stacked bar chart.
  136. */
  137. @Converter(StackValueConverter.class)
  138. public static class StackValue {
  139. private Number val;
  140. private String colour;
  141. public StackValue(Number value) {
  142. this(value, null);
  143. }
  144. public StackValue(Number value, String colour) {
  145. setValue(value);
  146. setColour(colour);
  147. }
  148. public Number getValue() {
  149. return val;
  150. }
  151. public StackValue setValue(Number val) {
  152. this.val = val;
  153. return this;
  154. }
  155. public String getColour() {
  156. return colour;
  157. }
  158. public StackValue setColour(String colour) {
  159. this.colour = colour;
  160. return this;
  161. }
  162. }
  163. /**
  164. * Representation of key in the stacked bar chart.
  165. */
  166. @Converter(StackKeyConverter.class)
  167. public static class StackKey
  168. {
  169. private String text;
  170. private String colour;
  171. @Alias("font-size") private String fontSize;
  172. public String getText() {
  173. return text;
  174. }
  175. public void setText(String text) {
  176. this.text = text;
  177. }
  178. public String getColour() {
  179. return colour;
  180. }
  181. public void setColour(String colour) {
  182. this.colour = colour;
  183. }
  184. public String getFontSize() {
  185. return fontSize;
  186. }
  187. public void setFontSize(String fontSize) {
  188. this.fontSize = fontSize;
  189. }
  190. }
  191. }