1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ---------------------------
  25. * CyclicXYItemRenderer.java
  26. * ---------------------------
  27. * (C) Copyright 2003, 2004, by Nicolas Brodu and Contributors.
  28. *
  29. * Original Author: Nicolas Brodu;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: CyclicXYItemRenderer.java,v 1.1 2004/08/31 14:49:29 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 19-Nov-2003 : Initial import to JFreeChart from the JSynoptic project (NB);
  37. * 23-Dec-2003 : Added missing Javadocs (DG);
  38. * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
  39. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  40. *
  41. */
  42. package org.jfree.chart.renderer.xy;
  43. import java.awt.Graphics2D;
  44. import java.awt.geom.Rectangle2D;
  45. import org.jfree.chart.axis.CyclicNumberAxis;
  46. import org.jfree.chart.axis.ValueAxis;
  47. import org.jfree.chart.labels.XYToolTipGenerator;
  48. import org.jfree.chart.plot.CrosshairState;
  49. import org.jfree.chart.plot.PlotRenderingInfo;
  50. import org.jfree.chart.plot.XYPlot;
  51. import org.jfree.chart.urls.XYURLGenerator;
  52. import org.jfree.data.DomainOrder;
  53. import org.jfree.data.general.DatasetChangeListener;
  54. import org.jfree.data.general.DatasetGroup;
  55. import org.jfree.data.xy.XYDataset;
  56. /**
  57. * The Cyclic XY item renderer is specially designed to handle cyclic axis.
  58. * While the standard renderer would draw a line across the plot when a cycling occurs, the cyclic
  59. * renderer splits the line at each cycle end instead. This is done by interpolating new points at
  60. * cycle boundary. Thus, correct appearance is restored.
  61. *
  62. * The Cyclic XY item renderer works exactly like a standard XY item renderer with non-cyclic axis.
  63. *
  64. * @author Nicolas Brodu
  65. */
  66. public class CyclicXYItemRenderer extends StandardXYItemRenderer {
  67. /**
  68. * Default constructor.
  69. */
  70. public CyclicXYItemRenderer() {
  71. super();
  72. }
  73. /**
  74. * Creates a new renderer.
  75. *
  76. * @param type the renderer type.
  77. */
  78. public CyclicXYItemRenderer(int type) {
  79. super(type);
  80. }
  81. /**
  82. * Creates a new renderer.
  83. *
  84. * @param type the renderer type.
  85. * @param labelGenerator the tooltip generator.
  86. */
  87. public CyclicXYItemRenderer(int type, XYToolTipGenerator labelGenerator) {
  88. super(type, labelGenerator);
  89. }
  90. /**
  91. * Creates a new renderer.
  92. *
  93. * @param type the renderer type.
  94. * @param labelGenerator the tooltip generator.
  95. * @param urlGenerator the url generator.
  96. */
  97. public CyclicXYItemRenderer(int type,
  98. XYToolTipGenerator labelGenerator,
  99. XYURLGenerator urlGenerator) {
  100. super(type, labelGenerator, urlGenerator);
  101. }
  102. /**
  103. * Draws the visual representation of a single data item.
  104. * When using cyclic axis, do not draw a line from right to left when cycling as would a
  105. * standard XY item renderer, but instead draw a line from the previous point to the cycle bound
  106. * in the last cycle, and a line from the cycle bound to current point in the current cycle.
  107. *
  108. * @param g2 the graphics device.
  109. * @param state the renderer state.
  110. * @param dataArea the data area.
  111. * @param info the plot rendering info.
  112. * @param plot the plot.
  113. * @param domainAxis the domain axis.
  114. * @param rangeAxis the range axis.
  115. * @param dataset the dataset.
  116. * @param series the series index.
  117. * @param item the item index.
  118. * @param crosshairState crosshair information for the plot (<code>null</code> permitted).
  119. * @param pass the current pass index.
  120. */
  121. public void drawItem(Graphics2D g2,
  122. XYItemRendererState state,
  123. Rectangle2D dataArea,
  124. PlotRenderingInfo info,
  125. XYPlot plot,
  126. ValueAxis domainAxis,
  127. ValueAxis rangeAxis,
  128. XYDataset dataset,
  129. int series,
  130. int item,
  131. CrosshairState crosshairState,
  132. int pass) {
  133. if ((!getPlotLines()) || ((!(domainAxis instanceof CyclicNumberAxis))
  134. && (!(rangeAxis instanceof CyclicNumberAxis))) || (item <= 0)) {
  135. super.drawItem(
  136. g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item,
  137. crosshairState, pass
  138. );
  139. return;
  140. }
  141. // get the previous data point...
  142. Number xn = dataset.getX(series, item - 1);
  143. Number yn = dataset.getY(series, item - 1);
  144. // If null, don't draw line => then delegate to parent
  145. if (yn == null || xn == null) {
  146. super.drawItem(
  147. g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item,
  148. crosshairState, pass
  149. );
  150. return;
  151. }
  152. double[] x = new double[2];
  153. double[] y = new double[2];
  154. x[0] = xn.doubleValue();
  155. y[0] = yn.doubleValue();
  156. // get the data point...
  157. xn = dataset.getX(series, item);
  158. yn = dataset.getY(series, item);
  159. // If null, don't draw line at all
  160. if (yn == null || xn == null) {
  161. return;
  162. }
  163. x[1] = xn.doubleValue();
  164. y[1] = yn.doubleValue();
  165. // Now split the segment as needed
  166. double xcycleBound = Double.NaN;
  167. double ycycleBound = Double.NaN;
  168. boolean xBoundMapping = false, yBoundMapping = false;
  169. CyclicNumberAxis cnax = null, cnay = null;
  170. if (domainAxis instanceof CyclicNumberAxis) {
  171. cnax = (CyclicNumberAxis) domainAxis;
  172. xcycleBound = cnax.getCycleBound();
  173. xBoundMapping = cnax.isBoundMappedToLastCycle();
  174. // If the segment must be splitted, insert a new point
  175. // Strict test forces to have real segments (not 2 equal points) and avoids
  176. // division by 0
  177. if ((x[0] != x[1]) && ((xcycleBound >= x[0]) && (xcycleBound <= x[1])
  178. || (xcycleBound >= x[1]) && (xcycleBound <= x[0]))) {
  179. double[] nx = new double[3];
  180. double[] ny = new double[3];
  181. nx[0] = x[0]; nx[2] = x[1]; ny[0] = y[0]; ny[2] = y[1];
  182. nx[1] = xcycleBound;
  183. ny[1] = (y[1] - y[0]) * (xcycleBound - x[0]) / (x[1] - x[0]) + y[0];
  184. x = nx; y = ny;
  185. }
  186. }
  187. if (rangeAxis instanceof CyclicNumberAxis) {
  188. cnay = (CyclicNumberAxis) rangeAxis;
  189. ycycleBound = cnay.getCycleBound();
  190. yBoundMapping = cnay.isBoundMappedToLastCycle();
  191. // The split may occur in either x splitted segments, if any, but not in both
  192. if ((y[0] != y[1]) && ((ycycleBound >= y[0]) && (ycycleBound <= y[1])
  193. || (ycycleBound >= y[1]) && (ycycleBound <= y[0]))) {
  194. double[] nx = new double[x.length + 1];
  195. double[] ny = new double[y.length + 1];
  196. nx[0] = x[0]; nx[2] = x[1]; ny[0] = y[0]; ny[2] = y[1];
  197. ny[1] = ycycleBound;
  198. nx[1] = (x[1] - x[0]) * (ycycleBound - y[0]) / (y[1] - y[0]) + x[0];
  199. if (x.length == 3) {
  200. nx[3] = x[2]; ny[3] = y[2];
  201. }
  202. x = nx; y = ny;
  203. }
  204. else if ((x.length == 3) && (y[1] != y[2]) && ((ycycleBound >= y[1])
  205. && (ycycleBound <= y[2]) || (ycycleBound >= y[2]) && (ycycleBound <= y[1]))) {
  206. double[] nx = new double[4];
  207. double[] ny = new double[4];
  208. nx[0] = x[0]; nx[1] = x[1]; nx[3] = x[2];
  209. ny[0] = y[0]; ny[1] = y[1]; ny[3] = y[2];
  210. ny[2] = ycycleBound;
  211. nx[2] = (x[2] - x[1]) * (ycycleBound - y[1]) / (y[2] - y[1]) + x[1];
  212. x = nx; y = ny;
  213. }
  214. }
  215. // If the line is not wrapping, then parent is OK
  216. if (x.length == 2) {
  217. super.drawItem(
  218. g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset,
  219. series, item, crosshairState, pass
  220. );
  221. return;
  222. }
  223. OverwriteDataSet newset = new OverwriteDataSet(x, y, dataset);
  224. if (cnax != null) {
  225. if (xcycleBound == x[0]) {
  226. cnax.setBoundMappedToLastCycle(x[1] <= xcycleBound);
  227. }
  228. if (xcycleBound == x[1]) {
  229. cnax.setBoundMappedToLastCycle(x[0] <= xcycleBound);
  230. }
  231. }
  232. if (cnay != null) {
  233. if (ycycleBound == y[0]) {
  234. cnay.setBoundMappedToLastCycle(y[1] <= ycycleBound);
  235. }
  236. if (ycycleBound == y[1]) {
  237. cnay.setBoundMappedToLastCycle(y[0] <= ycycleBound);
  238. }
  239. }
  240. super.drawItem(
  241. g2, state, dataArea, info, plot, domainAxis, rangeAxis, newset, series, 1,
  242. crosshairState, pass
  243. );
  244. if (cnax != null) {
  245. if (xcycleBound == x[1]) {
  246. cnax.setBoundMappedToLastCycle(x[2] <= xcycleBound);
  247. }
  248. if (xcycleBound == x[2]) {
  249. cnax.setBoundMappedToLastCycle(x[1] <= xcycleBound);
  250. }
  251. }
  252. if (cnay != null) {
  253. if (ycycleBound == y[1]) {
  254. cnay.setBoundMappedToLastCycle(y[2] <= ycycleBound);
  255. }
  256. if (ycycleBound == y[2]) {
  257. cnay.setBoundMappedToLastCycle(y[1] <= ycycleBound);
  258. }
  259. }
  260. super.drawItem(
  261. g2, state, dataArea, info, plot, domainAxis, rangeAxis, newset, series, 2,
  262. crosshairState, pass
  263. );
  264. if (x.length == 4) {
  265. if (cnax != null) {
  266. if (xcycleBound == x[2]) {
  267. cnax.setBoundMappedToLastCycle(x[3] <= xcycleBound);
  268. }
  269. if (xcycleBound == x[3]) {
  270. cnax.setBoundMappedToLastCycle(x[2] <= xcycleBound);
  271. }
  272. }
  273. if (cnay != null) {
  274. if (ycycleBound == y[2]) {
  275. cnay.setBoundMappedToLastCycle(y[3] <= ycycleBound);
  276. }
  277. if (ycycleBound == y[3]) {
  278. cnay.setBoundMappedToLastCycle(y[2] <= ycycleBound);
  279. }
  280. }
  281. super.drawItem(
  282. g2, state, dataArea, info, plot, domainAxis, rangeAxis, newset,
  283. series, 3, crosshairState, pass
  284. );
  285. }
  286. if (cnax != null) {
  287. cnax.setBoundMappedToLastCycle(xBoundMapping);
  288. }
  289. if (cnay != null) {
  290. cnay.setBoundMappedToLastCycle(yBoundMapping);
  291. }
  292. }
  293. /**
  294. * A dataset to hold the interpolated points when drawing new lines.
  295. */
  296. protected static class OverwriteDataSet implements XYDataset {
  297. /** The delegate dataset. */
  298. protected XYDataset delegateSet;
  299. /** Storage for the x and y values. */
  300. Double[] x, y;
  301. /**
  302. * Creates a new dataset.
  303. *
  304. * @param x the x values.
  305. * @param y the y values.
  306. * @param delegateSet the dataset.
  307. */
  308. public OverwriteDataSet(double [] x, double[] y, XYDataset delegateSet) {
  309. this.delegateSet = delegateSet;
  310. this.x = new Double[x.length]; this.y = new Double[y.length];
  311. for (int i = 0; i < x.length; ++i) {
  312. this.x[i] = new Double(x[i]);
  313. this.y[i] = new Double(y[i]);
  314. }
  315. }
  316. /**
  317. * Returns the order of the domain (X) values.
  318. *
  319. * @return The domain order.
  320. */
  321. public DomainOrder getDomainOrder() {
  322. return DomainOrder.NONE;
  323. }
  324. /**
  325. * Returns the number of items for the given series.
  326. *
  327. * @param series the series index (zero-based).
  328. *
  329. * @return The item count.
  330. */
  331. public int getItemCount(int series) {
  332. return this.x.length;
  333. }
  334. /**
  335. * Returns the x-value.
  336. *
  337. * @param series the series index (zero-based).
  338. * @param item the item index (zero-based).
  339. *
  340. * @return the x-value.
  341. */
  342. public Number getX(int series, int item) {
  343. return this.x[item];
  344. }
  345. /**
  346. * Returns the x-value (as a double primitive) for an item within a series.
  347. *
  348. * @param series the series (zero-based index).
  349. * @param item the item (zero-based index).
  350. *
  351. * @return The x-value.
  352. */
  353. public double getXValue(int series, int item) {
  354. double result = Double.NaN;
  355. Number x = getX(series, item);
  356. if (x != null) {
  357. result = x.doubleValue();
  358. }
  359. return result;
  360. }
  361. /**
  362. * Returns the y-value.
  363. *
  364. * @param series the series index (zero-based).
  365. * @param item the item index (zero-based).
  366. *
  367. * @return the y-value.
  368. */
  369. public Number getY(int series, int item) {
  370. return this.y[item];
  371. }
  372. /**
  373. * Returns the y-value (as a double primitive) for an item within a series.
  374. *
  375. * @param series the series (zero-based index).
  376. * @param item the item (zero-based index).
  377. *
  378. * @return The y-value.
  379. */
  380. public double getYValue(int series, int item) {
  381. double result = Double.NaN;
  382. Number y = getY(series, item);
  383. if (y != null) {
  384. result = y.doubleValue();
  385. }
  386. return result;
  387. }
  388. /**
  389. * Returns the number of series in the dataset.
  390. *
  391. * @return the series count.
  392. */
  393. public int getSeriesCount() {
  394. return this.delegateSet.getSeriesCount();
  395. }
  396. /**
  397. * Returns the name of the given series.
  398. *
  399. * @param series the series index (zero-based).
  400. *
  401. * @return The series name.
  402. */
  403. public String getSeriesName(int series) {
  404. return this.delegateSet.getSeriesName(series);
  405. }
  406. /**
  407. * Does nothing.
  408. *
  409. * @param listener ignored.
  410. */
  411. public void addChangeListener(DatasetChangeListener listener) {
  412. // unused in parent
  413. }
  414. /**
  415. * Does nothing.
  416. *
  417. * @param listener ignored.
  418. */
  419. public void removeChangeListener(DatasetChangeListener listener) {
  420. // unused in parent
  421. }
  422. /**
  423. * Returns the dataset group.
  424. *
  425. * @return the dataset group.
  426. */
  427. public DatasetGroup getGroup() {
  428. // unused but must return something, so while we are at it...
  429. return this.delegateSet.getGroup();
  430. }
  431. /**
  432. * Does nothing.
  433. *
  434. * @param group ignored.
  435. */
  436. public void setGroup(DatasetGroup group) {
  437. // unused in parent
  438. }
  439. }
  440. }