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