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

http://charts4j.googlecode.com/ · Java · 133 lines · 50 code · 12 blank · 71 comment · 10 complexity · 3c6d23a37f9bc985987fff2fcdedf302 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 com.googlecode.charts4j.collect.ImmutableList;
  25. import com.googlecode.charts4j.collect.Lists;
  26. import com.googlecode.charts4j.parameters.ChartType;
  27. /**
  28. * Pie chart constructed with the {@link GCharts} static factory class.
  29. *
  30. * If the number of colors specified in the {@link Slice} objects is less than
  31. * the number of slices, then colors are interpolated. If no colors are
  32. * specified, pie segment colors are interpolated from dark orange to pale
  33. * yellow. If slices do not add up to 100, slices will be proportional to the
  34. * total of all slices.
  35. *
  36. * @author Julien Chastang (julien.c.chastang at gmail dot com)
  37. *
  38. * @see Slice
  39. * @see GCharts
  40. */
  41. public class PieChart extends AbstractGraphChart {
  42. /** Is this a 3D pie chart. */
  43. private boolean threeD = false;
  44. /** An immutable list of slices for this pie. */
  45. private final ImmutableList<Slice> slices;
  46. /** The pie chart orientation radians. */
  47. private double orientation = Double.NaN;
  48. /**
  49. * Set the pie chart orientation in radians. Positive values will rotate the
  50. * chart clockwise.
  51. *
  52. * @param radians
  53. * the orientation to set in radians
  54. */
  55. public void setOrientation(final double radians) {
  56. this.orientation = radians;
  57. }
  58. /**
  59. * Create a pie chart with the given slices. If slices do not add up to 100,
  60. * slices will be proportional to the total of all slices.
  61. *
  62. * @param slices
  63. * list of data slices of the pie chart.
  64. *
  65. * @see Slice
  66. */
  67. PieChart(final ImmutableList<? extends Slice> slices) {
  68. super();
  69. this.slices = Lists.copyOf(slices);
  70. }
  71. /**
  72. * Is this a 3D chart.
  73. *
  74. * @return the threeD
  75. */
  76. public final boolean isThreeD() {
  77. return threeD;
  78. }
  79. /**
  80. * If you want the chart to be 3D, set to true.
  81. *
  82. * @param threeD
  83. * boolean to determine if pie chart is rendered in 3D.
  84. */
  85. public final void setThreeD(final boolean threeD) {
  86. this.threeD = threeD;
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. @Override
  92. protected void prepareData() {
  93. super.prepareData();
  94. final double[] d = new double[slices.size()];
  95. int i = 0;
  96. boolean hasLegend = false;
  97. double sum = 0;
  98. for (Slice slice : slices) {
  99. if (slice.getLegend() != null ){
  100. hasLegend |= true;
  101. }
  102. sum += slice.getPercentage();
  103. }
  104. for (Slice slice : slices) {
  105. d[i++] = (slice.getPercentage()/sum)*100;
  106. parameterManager.addPieChartAndGoogleOMeterLegend(slice.getLabel() != null ? slice.getLabel() : "");
  107. if (hasLegend)
  108. parameterManager.addLegend(slice.getLegend() != null ? slice.getLegend() : "");
  109. if (slice.getColor() != null) {
  110. parameterManager.addColor(slice.getColor());
  111. }
  112. }
  113. if (!Double.isNaN(orientation)) {
  114. parameterManager.addPieChartOrientation(orientation);
  115. }
  116. parameterManager.addData(Data.newData(d));
  117. parameterManager.setChartTypeParameter(threeD ? ChartType.THREE_D_PIE_CHART : ChartType.PIE_CHART);
  118. }
  119. }