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

http://charts4j.googlecode.com/ · Java · 159 lines · 71 code · 17 blank · 71 comment · 11 complexity · bffe8269cf56a7b6ecbc96b158f8f277 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 java.util.Set;
  25. import com.googlecode.charts4j.collect.ImmutableList;
  26. import com.googlecode.charts4j.collect.Lists;
  27. import com.googlecode.charts4j.collect.Sets;
  28. import com.googlecode.charts4j.parameters.ChartType;
  29. /**
  30. * XY Line chart constructed with the {@link GCharts} static factory class.
  31. *
  32. * @author Julien Chastang (julien.c.chastang at gmail dot com)
  33. *
  34. * @see XYLine
  35. * @see GCharts
  36. */
  37. public class XYLineChart extends AbstractLineChart {
  38. /** List of lines to be ploted this chart. **/
  39. private final ImmutableList<Plot> xylines;
  40. /**
  41. * Create a XY line chart.
  42. *
  43. * @param xylines
  44. * xy lines to be rendered by this chart
  45. * @see XYLine
  46. */
  47. XYLineChart(final ImmutableList<? extends Plot> xylines) {
  48. super(xylines);
  49. this.xylines = Lists.copyOf(xylines);
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. @Override
  55. protected void prepareData() {
  56. super.prepareData();
  57. for (Plot xyl : xylines) {
  58. final PlotImpl xyline = (PlotImpl) xyl;
  59. final PlotImpl xy = removeDuplicatePoints(xyline);
  60. parameterManager.addData(xy.getXData());
  61. parameterManager.addData(xy.getYData());
  62. }
  63. parameterManager.setChartTypeParameter(ChartType.XY_LINE_CHART);
  64. }
  65. /**
  66. * Method that purges duplicate points.
  67. *
  68. * @param xyline
  69. * plot that will be purged of duplicate lines.
  70. * @return plot purged of duplicates
  71. */
  72. private PlotImpl removeDuplicatePoints(final PlotImpl xyline) {
  73. final double[] xData = xyline.getXData().getData();
  74. final double[] yData = xyline.getYData().getData();
  75. if (xData.length != yData.length) {
  76. return xyline;
  77. }
  78. // Removing duplicates. Can assume xData and yData are the same length.
  79. final Set<XYPoint> set = Sets.newLinkedHashSet();
  80. for (int i = 0; i < xData.length; i++) {
  81. set.add(new XYPoint(xData[i], yData[i]));
  82. }
  83. final double[] x = new double[set.size()];
  84. final double[] y = new double[set.size()];
  85. int i = 0;
  86. for (XYPoint point : set) {
  87. x[i] = point.x;
  88. y[i] = point.y;
  89. i++;
  90. }
  91. return new PlotImpl(Data.newData(x), Data.newData(y));
  92. }
  93. /**
  94. * XY point.
  95. *
  96. * @author Julien Chastang (julien.c.chastang at gmail dot com)
  97. */
  98. private static final class XYPoint {
  99. /** X. **/
  100. private final double x;
  101. /** Y. **/
  102. private final double y;
  103. /**
  104. * Construct an XY point.
  105. *
  106. * @param x x
  107. * @param y y
  108. */
  109. private XYPoint(final double x, final double y) {
  110. super();
  111. this.x = x;
  112. this.y = y;
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. @Override
  118. public boolean equals(final Object obj) {
  119. if (this == obj) {
  120. return true;
  121. }
  122. if (!(obj instanceof XYPoint)) {
  123. return false;
  124. }
  125. final XYPoint objA = (XYPoint) obj;
  126. return (this.x == objA.x) && (this.y == objA.y);
  127. }
  128. /**
  129. *
  130. * {@inheritDoc}
  131. */
  132. @Override
  133. public int hashCode() {
  134. int hash = 1;
  135. hash = hash * 31 + Double.valueOf(x).hashCode();
  136. hash = hash * 31 + Double.valueOf(y).hashCode();
  137. return hash;
  138. }
  139. }
  140. }