001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ------------------------
028 * CandlestickRenderer.java
029 * ------------------------
030 * (C) Copyright 2001-2008, by Object Refinery Limited.
031 *
032 * Original Authors: David Gilbert (for Object Refinery Limited);
033 * Sylvain Vieujot;
034 * Contributor(s): Richard Atkinson;
035 * Christian W. Zuckschwerdt;
036 * Jerome Fisher;
037 *
038 * Changes
039 * -------
040 * 13-Dec-2001 : Version 1. Based on code in the (now redundant)
041 * CandlestickPlot class, written by Sylvain Vieujot (DG);
042 * 23-Jan-2002 : Added DrawInfo parameter to drawItem() method (DG);
043 * 28-Mar-2002 : Added a property change listener mechanism so that renderers
044 * no longer need to be immutable. Added properties for up and
045 * down colors (DG);
046 * 04-Apr-2002 : Updated with new automatic width calculation and optional
047 * volume display, contributed by Sylvain Vieujot (DG);
048 * 09-Apr-2002 : Removed translatedRangeZero from the drawItem() method, and
049 * changed the return type of the drawItem method to void,
050 * reflecting a change in the XYItemRenderer interface. Added
051 * tooltip code to drawItem() method (DG);
052 * 25-Jun-2002 : Removed redundant code (DG);
053 * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML
054 * image maps (RA);
055 * 19-Sep-2002 : Fixed errors reported by Checkstyle (DG);
056 * 25-Mar-2003 : Implemented Serializable (DG);
057 * 01-May-2003 : Modified drawItem() method signature (DG);
058 * 30-Jun-2003 : Added support for PlotOrientation (for completeness, this
059 * renderer is unlikely to be used with a HORIZONTAL
060 * orientation) (DG);
061 * 30-Jul-2003 : Modified entity constructor (CZ);
062 * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
063 * 29-Aug-2003 : Moved maxVolume calculation to initialise method (see bug
064 * report 796619) (DG);
065 * 02-Sep-2003 : Added maxCandleWidthInMilliseconds as workaround for bug
066 * 796621 (DG);
067 * 08-Sep-2003 : Changed ValueAxis API (DG);
068 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
069 * 13-Oct-2003 : Applied patch from Jerome Fisher to improve auto width
070 * calculations (DG);
071 * 23-Dec-2003 : Fixed bug where up and down paint are used incorrectly (DG);
072 * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
073 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
074 * getYValue() (DG);
075 * ------------- JFREECHART 1.0.x ---------------------------------------------
076 * 06-Jul-2006 : Swapped calls to getX() --> getXValue(), and the same for the
077 * other data values (DG);
078 * 17-Aug-2006 : Corrections to the equals() method (DG);
079 * 05-Mar-2007 : Added flag to allow optional use of outline paint (DG);
080 * 08-Oct-2007 : Added new volumePaint field (DG);
081 * 08-Apr-2008 : Added findRangeBounds() method override (DG);
082 * 13-May-2008 : Fixed chart entity bugs (1962467 and 1962472) (DG);
083 *
084 */
085
086 package org.jfree.chart.renderer.xy;
087
088 import java.awt.AlphaComposite;
089 import java.awt.Color;
090 import java.awt.Composite;
091 import java.awt.Graphics2D;
092 import java.awt.Paint;
093 import java.awt.Stroke;
094 import java.awt.geom.Line2D;
095 import java.awt.geom.Rectangle2D;
096 import java.io.IOException;
097 import java.io.ObjectInputStream;
098 import java.io.ObjectOutputStream;
099 import java.io.Serializable;
100
101 import org.jfree.chart.axis.ValueAxis;
102 import org.jfree.chart.entity.EntityCollection;
103 import org.jfree.chart.event.RendererChangeEvent;
104 import org.jfree.chart.labels.HighLowItemLabelGenerator;
105 import org.jfree.chart.labels.XYToolTipGenerator;
106 import org.jfree.chart.plot.CrosshairState;
107 import org.jfree.chart.plot.PlotOrientation;
108 import org.jfree.chart.plot.PlotRenderingInfo;
109 import org.jfree.chart.plot.XYPlot;
110 import org.jfree.data.Range;
111 import org.jfree.data.general.DatasetUtilities;
112 import org.jfree.data.xy.IntervalXYDataset;
113 import org.jfree.data.xy.OHLCDataset;
114 import org.jfree.data.xy.XYDataset;
115 import org.jfree.io.SerialUtilities;
116 import org.jfree.ui.RectangleEdge;
117 import org.jfree.util.PaintUtilities;
118 import org.jfree.util.PublicCloneable;
119
120 /**
121 * A renderer that draws candlesticks on an {@link XYPlot} (requires a
122 * {@link OHLCDataset}). The example shown here is generated
123 * by the <code>CandlestickChartDemo1.java</code> program included in the
124 * JFreeChart demo collection:
125 * <br><br>
126 * <img src="../../../../../images/CandlestickRendererSample.png"
127 * alt="CandlestickRendererSample.png" />
128 * <P>
129 * This renderer does not include code to calculate the crosshair point for the
130 * plot.
131 */
132 public class CandlestickRenderer extends AbstractXYItemRenderer
133 implements XYItemRenderer, Cloneable, PublicCloneable, Serializable {
134
135 /** For serialization. */
136 private static final long serialVersionUID = 50390395841817121L;
137
138 /** The average width method. */
139 public static final int WIDTHMETHOD_AVERAGE = 0;
140
141 /** The smallest width method. */
142 public static final int WIDTHMETHOD_SMALLEST = 1;
143
144 /** The interval data method. */
145 public static final int WIDTHMETHOD_INTERVALDATA = 2;
146
147 /** The method of automatically calculating the candle width. */
148 private int autoWidthMethod = WIDTHMETHOD_AVERAGE;
149
150 /**
151 * The number (generally between 0.0 and 1.0) by which the available space
152 * automatically calculated for the candles will be multiplied to determine
153 * the actual width to use.
154 */
155 private double autoWidthFactor = 4.5 / 7;
156
157 /** The minimum gap between one candle and the next */
158 private double autoWidthGap = 0.0;
159
160 /** The candle width. */
161 private double candleWidth;
162
163 /** The maximum candlewidth in milliseconds. */
164 private double maxCandleWidthInMilliseconds = 1000.0 * 60.0 * 60.0 * 20.0;
165
166 /** Temporary storage for the maximum candle width. */
167 private double maxCandleWidth;
168
169 /**
170 * The paint used to fill the candle when the price moved up from open to
171 * close.
172 */
173 private transient Paint upPaint;
174
175 /**
176 * The paint used to fill the candle when the price moved down from open
177 * to close.
178 */
179 private transient Paint downPaint;
180
181 /** A flag controlling whether or not volume bars are drawn on the chart. */
182 private boolean drawVolume;
183
184 /**
185 * The paint used to fill the volume bars (if they are visible). Once
186 * initialised, this field should never be set to <code>null</code>.
187 *
188 * @since 1.0.7
189 */
190 private transient Paint volumePaint;
191
192 /** Temporary storage for the maximum volume. */
193 private transient double maxVolume;
194
195 /**
196 * A flag that controls whether or not the renderer's outline paint is
197 * used to draw the outline of the candlestick. The default value is
198 * <code>false</code> to avoid a change of behaviour for existing code.
199 *
200 * @since 1.0.5
201 */
202 private boolean useOutlinePaint;
203
204 /**
205 * Creates a new renderer for candlestick charts.
206 */
207 public CandlestickRenderer() {
208 this(-1.0);
209 }
210
211 /**
212 * Creates a new renderer for candlestick charts.
213 * <P>
214 * Use -1 for the candle width if you prefer the width to be calculated
215 * automatically.
216 *
217 * @param candleWidth The candle width.
218 */
219 public CandlestickRenderer(double candleWidth) {
220 this(candleWidth, true, new HighLowItemLabelGenerator());
221 }
222
223 /**
224 * Creates a new renderer for candlestick charts.
225 * <P>
226 * Use -1 for the candle width if you prefer the width to be calculated
227 * automatically.
228 *
229 * @param candleWidth the candle width.
230 * @param drawVolume a flag indicating whether or not volume bars should
231 * be drawn.
232 * @param toolTipGenerator the tool tip generator. <code>null</code> is
233 * none.
234 */
235 public CandlestickRenderer(double candleWidth, boolean drawVolume,
236 XYToolTipGenerator toolTipGenerator) {
237 super();
238 setBaseToolTipGenerator(toolTipGenerator);
239 this.candleWidth = candleWidth;
240 this.drawVolume = drawVolume;
241 this.volumePaint = Color.gray;
242 this.upPaint = Color.green;
243 this.downPaint = Color.red;
244 this.useOutlinePaint = false; // false preserves the old behaviour
245 // prior to introducing this flag
246 }
247
248 /**
249 * Returns the width of each candle.
250 *
251 * @return The candle width.
252 *
253 * @see #setCandleWidth(double)
254 */
255 public double getCandleWidth() {
256 return this.candleWidth;
257 }
258
259 /**
260 * Sets the candle width and sends a {@link RendererChangeEvent} to all
261 * registered listeners.
262 * <P>
263 * If you set the width to a negative value, the renderer will calculate
264 * the candle width automatically based on the space available on the chart.
265 *
266 * @param width The width.
267 * @see #setAutoWidthMethod(int)
268 * @see #setAutoWidthGap(double)
269 * @see #setAutoWidthFactor(double)
270 * @see #setMaxCandleWidthInMilliseconds(double)
271 */
272 public void setCandleWidth(double width) {
273 if (width != this.candleWidth) {
274 this.candleWidth = width;
275 fireChangeEvent();
276 }
277 }
278
279 /**
280 * Returns the maximum width (in milliseconds) of each candle.
281 *
282 * @return The maximum candle width in milliseconds.
283 *
284 * @see #setMaxCandleWidthInMilliseconds(double)
285 */
286 public double getMaxCandleWidthInMilliseconds() {
287 return this.maxCandleWidthInMilliseconds;
288 }
289
290 /**
291 * Sets the maximum candle width (in milliseconds) and sends a
292 * {@link RendererChangeEvent} to all registered listeners.
293 *
294 * @param millis The maximum width.
295 *
296 * @see #getMaxCandleWidthInMilliseconds()
297 * @see #setCandleWidth(double)
298 * @see #setAutoWidthMethod(int)
299 * @see #setAutoWidthGap(double)
300 * @see #setAutoWidthFactor(double)
301 */
302 public void setMaxCandleWidthInMilliseconds(double millis) {
303 this.maxCandleWidthInMilliseconds = millis;
304 fireChangeEvent();
305 }
306
307 /**
308 * Returns the method of automatically calculating the candle width.
309 *
310 * @return The method of automatically calculating the candle width.
311 *
312 * @see #setAutoWidthMethod(int)
313 */
314 public int getAutoWidthMethod() {
315 return this.autoWidthMethod;
316 }
317
318 /**
319 * Sets the method of automatically calculating the candle width and
320 * sends a {@link RendererChangeEvent} to all registered listeners.
321 * <p>
322 * <code>WIDTHMETHOD_AVERAGE</code>: Divides the entire display (ignoring
323 * scale factor) by the number of items, and uses this as the available
324 * width.<br>
325 * <code>WIDTHMETHOD_SMALLEST</code>: Checks the interval between each
326 * item, and uses the smallest as the available width.<br>
327 * <code>WIDTHMETHOD_INTERVALDATA</code>: Assumes that the dataset supports
328 * the IntervalXYDataset interface, and uses the startXValue - endXValue as
329 * the available width.
330 * <br>
331 *
332 * @param autoWidthMethod The method of automatically calculating the
333 * candle width.
334 *
335 * @see #WIDTHMETHOD_AVERAGE
336 * @see #WIDTHMETHOD_SMALLEST
337 * @see #WIDTHMETHOD_INTERVALDATA
338 * @see #getAutoWidthMethod()
339 * @see #setCandleWidth(double)
340 * @see #setAutoWidthGap(double)
341 * @see #setAutoWidthFactor(double)
342 * @see #setMaxCandleWidthInMilliseconds(double)
343 */
344 public void setAutoWidthMethod(int autoWidthMethod) {
345 if (this.autoWidthMethod != autoWidthMethod) {
346 this.autoWidthMethod = autoWidthMethod;
347 fireChangeEvent();
348 }
349 }
350
351 /**
352 * Returns the factor by which the available space automatically
353 * calculated for the candles will be multiplied to determine the actual
354 * width to use.
355 *
356 * @return The width factor (generally between 0.0 and 1.0).
357 *
358 * @see #setAutoWidthFactor(double)
359 */
360 public double getAutoWidthFactor() {
361 return this.autoWidthFactor;
362 }
363
364 /**
365 * Sets the factor by which the available space automatically calculated
366 * for the candles will be multiplied to determine the actual width to use.
367 *
368 * @param autoWidthFactor The width factor (generally between 0.0 and 1.0).
369 *
370 * @see #getAutoWidthFactor()
371 * @see #setCandleWidth(double)
372 * @see #setAutoWidthMethod(int)
373 * @see #setAutoWidthGap(double)
374 * @see #setMaxCandleWidthInMilliseconds(double)
375 */
376 public void setAutoWidthFactor(double autoWidthFactor) {
377 if (this.autoWidthFactor != autoWidthFactor) {
378 this.autoWidthFactor = autoWidthFactor;
379 fireChangeEvent();
380 }
381 }
382
383 /**
384 * Returns the amount of space to leave on the left and right of each
385 * candle when automatically calculating widths.
386 *
387 * @return The gap.
388 *
389 * @see #setAutoWidthGap(double)
390 */
391 public double getAutoWidthGap() {
392 return this.autoWidthGap;
393 }
394
395 /**
396 * Sets the amount of space to leave on the left and right of each candle
397 * when automatically calculating widths and sends a
398 * {@link RendererChangeEvent} to all registered listeners.
399 *
400 * @param autoWidthGap The gap.
401 *
402 * @see #getAutoWidthGap()
403 * @see #setCandleWidth(double)
404 * @see #setAutoWidthMethod(int)
405 * @see #setAutoWidthFactor(double)
406 * @see #setMaxCandleWidthInMilliseconds(double)
407 */
408 public void setAutoWidthGap(double autoWidthGap) {
409 if (this.autoWidthGap != autoWidthGap) {
410 this.autoWidthGap = autoWidthGap;
411 fireChangeEvent();
412 }
413 }
414
415 /**
416 * Returns the paint used to fill candles when the price moves up from open
417 * to close.
418 *
419 * @return The paint (possibly <code>null</code>).
420 *
421 * @see #setUpPaint(Paint)
422 */
423 public Paint getUpPaint() {
424 return this.upPaint;
425 }
426
427 /**
428 * Sets the paint used to fill candles when the price moves up from open
429 * to close and sends a {@link RendererChangeEvent} to all registered
430 * listeners.
431 *
432 * @param paint the paint (<code>null</code> permitted).
433 *
434 * @see #getUpPaint()
435 */
436 public void setUpPaint(Paint paint) {
437 this.upPaint = paint;
438 fireChangeEvent();
439 }
440
441 /**
442 * Returns the paint used to fill candles when the price moves down from
443 * open to close.
444 *
445 * @return The paint (possibly <code>null</code>).
446 *
447 * @see #setDownPaint(Paint)
448 */
449 public Paint getDownPaint() {
450 return this.downPaint;
451 }
452
453 /**
454 * Sets the paint used to fill candles when the price moves down from open
455 * to close and sends a {@link RendererChangeEvent} to all registered
456 * listeners.
457 *
458 * @param paint The paint (<code>null</code> permitted).
459 */
460 public void setDownPaint(Paint paint) {
461 this.downPaint = paint;
462 fireChangeEvent();
463 }
464
465 /**
466 * Returns a flag indicating whether or not volume bars are drawn on the
467 * chart.
468 *
469 * @return A boolean.
470 *
471 * @since 1.0.5
472 *
473 * @see #setDrawVolume(boolean)
474 */
475 public boolean getDrawVolume() {
476 return this.drawVolume;
477 }
478
479 /**
480 * Sets a flag that controls whether or not volume bars are drawn in the
481 * background and sends a {@link RendererChangeEvent} to all registered
482 * listeners.
483 *
484 * @param flag the flag.
485 *
486 * @see #getDrawVolume()
487 */
488 public void setDrawVolume(boolean flag) {
489 if (this.drawVolume != flag) {
490 this.drawVolume = flag;
491 fireChangeEvent();
492 }
493 }
494
495 /**
496 * Returns the paint that is used to fill the volume bars if they are
497 * visible.
498 *
499 * @return The paint (never <code>null</code>).
500 *
501 * @see #setVolumePaint(Paint)
502 *
503 * @since 1.0.7
504 */
505 public Paint getVolumePaint() {
506 return this.volumePaint;
507 }
508
509 /**
510 * Sets the paint used to fill the volume bars, and sends a
511 * {@link RendererChangeEvent} to all registered listeners.
512 *
513 * @param paint the paint (<code>null</code> not permitted).
514 *
515 * @see #getVolumePaint()
516 * @see #getDrawVolume()
517 *
518 * @since 1.0.7
519 */
520 public void setVolumePaint(Paint paint) {
521 if (paint == null) {
522 throw new IllegalArgumentException("Null 'paint' argument.");
523 }
524 this.volumePaint = paint;
525 fireChangeEvent();
526 }
527
528 /**
529 * Returns the flag that controls whether or not the renderer's outline
530 * paint is used to draw the candlestick outline. The default value is
531 * <code>false</code>.
532 *
533 * @return A boolean.
534 *
535 * @since 1.0.5
536 *
537 * @see #setUseOutlinePaint(boolean)
538 */
539 public boolean getUseOutlinePaint() {
540 return this.useOutlinePaint;
541 }
542
543 /**
544 * Sets the flag that controls whether or not the renderer's outline
545 * paint is used to draw the candlestick outline, and sends a
546 * {@link RendererChangeEvent} to all registered listeners.
547 *
548 * @param use the new flag value.
549 *
550 * @since 1.0.5
551 *
552 * @see #getUseOutlinePaint()
553 */
554 public void setUseOutlinePaint(boolean use) {
555 if (this.useOutlinePaint != use) {
556 this.useOutlinePaint = use;
557 fireChangeEvent();
558 }
559 }
560
561 /**
562 * Returns the range of values the renderer requires to display all the
563 * items from the specified dataset.
564 *
565 * @param dataset the dataset (<code>null</code> permitted).
566 *
567 * @return The range (<code>null</code> if the dataset is <code>null</code>
568 * or empty).
569 */
570 public Range findRangeBounds(XYDataset dataset) {
571 if (dataset != null) {
572 return DatasetUtilities.findRangeBounds(dataset, true);
573 }
574 else {
575 return null;
576 }
577 }
578
579 /**
580 * Initialises the renderer then returns the number of 'passes' through the
581 * data that the renderer will require (usually just one). This method
582 * will be called before the first item is rendered, giving the renderer
583 * an opportunity to initialise any state information it wants to maintain.
584 * The renderer can do nothing if it chooses.
585 *
586 * @param g2 the graphics device.
587 * @param dataArea the area inside the axes.
588 * @param plot the plot.
589 * @param dataset the data.
590 * @param info an optional info collection object to return data back to
591 * the caller.
592 *
593 * @return The number of passes the renderer requires.
594 */
595 public XYItemRendererState initialise(Graphics2D g2,
596 Rectangle2D dataArea,
597 XYPlot plot,
598 XYDataset dataset,
599 PlotRenderingInfo info) {
600
601 // calculate the maximum allowed candle width from the axis...
602 ValueAxis axis = plot.getDomainAxis();
603 double x1 = axis.getLowerBound();
604 double x2 = x1 + this.maxCandleWidthInMilliseconds;
605 RectangleEdge edge = plot.getDomainAxisEdge();
606 double xx1 = axis.valueToJava2D(x1, dataArea, edge);
607 double xx2 = axis.valueToJava2D(x2, dataArea, edge);
608 this.maxCandleWidth = Math.abs(xx2 - xx1);
609 // Absolute value, since the relative x
610 // positions are reversed for horizontal orientation
611
612 // calculate the highest volume in the dataset...
613 if (this.drawVolume) {
614 OHLCDataset highLowDataset = (OHLCDataset) dataset;
615 this.maxVolume = 0.0;
616 for (int series = 0; series < highLowDataset.getSeriesCount();
617 series++) {
618 for (int item = 0; item < highLowDataset.getItemCount(series);
619 item++) {
620 double volume = highLowDataset.getVolumeValue(series, item);
621 if (volume > this.maxVolume) {
622 this.maxVolume = volume;
623 }
624
625 }
626 }
627 }
628
629 return new XYItemRendererState(info);
630 }
631
632 /**
633 * Draws the visual representation of a single data item.
634 *
635 * @param g2 the graphics device.
636 * @param state the renderer state.
637 * @param dataArea the area within which the plot is being drawn.
638 * @param info collects info about the drawing.
639 * @param plot the plot (can be used to obtain standard color
640 * information etc).
641 * @param domainAxis the domain axis.
642 * @param rangeAxis the range axis.
643 * @param dataset the dataset.
644 * @param series the series index (zero-based).
645 * @param item the item index (zero-based).
646 * @param crosshairState crosshair information for the plot
647 * (<code>null</code> permitted).
648 * @param pass the pass index.
649 */
650 public void drawItem(Graphics2D g2,
651 XYItemRendererState state,
652 Rectangle2D dataArea,
653 PlotRenderingInfo info,
654 XYPlot plot,
655 ValueAxis domainAxis,
656 ValueAxis rangeAxis,
657 XYDataset dataset,
658 int series,
659 int item,
660 CrosshairState crosshairState,
661 int pass) {
662
663 boolean horiz;
664 PlotOrientation orientation = plot.getOrientation();
665 if (orientation == PlotOrientation.HORIZONTAL) {
666 horiz = true;
667 }
668 else if (orientation == PlotOrientation.VERTICAL) {
669 horiz = false;
670 }
671 else {
672 return;
673 }
674
675 // setup for collecting optional entity info...
676 EntityCollection entities = null;
677 if (info != null) {
678 entities = info.getOwner().getEntityCollection();
679 }
680
681 OHLCDataset highLowData = (OHLCDataset) dataset;
682
683 double x = highLowData.getXValue(series, item);
684 double yHigh = highLowData.getHighValue(series, item);
685 double yLow = highLowData.getLowValue(series, item);
686 double yOpen = highLowData.getOpenValue(series, item);
687 double yClose = highLowData.getCloseValue(series, item);
688
689 RectangleEdge domainEdge = plot.getDomainAxisEdge();
690 double xx = domainAxis.valueToJava2D(x, dataArea, domainEdge);
691
692 RectangleEdge edge = plot.getRangeAxisEdge();
693 double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, edge);
694 double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, edge);
695 double yyOpen = rangeAxis.valueToJava2D(yOpen, dataArea, edge);
696 double yyClose = rangeAxis.valueToJava2D(yClose, dataArea, edge);
697
698 double volumeWidth;
699 double stickWidth;
700 if (this.candleWidth > 0) {
701 // These are deliberately not bounded to minimums/maxCandleWidth to
702 // retain old behaviour.
703 volumeWidth = this.candleWidth;
704 stickWidth = this.candleWidth;
705 }
706 else {
707 double xxWidth = 0;
708 int itemCount;
709 switch (this.autoWidthMethod) {
710
711 case WIDTHMETHOD_AVERAGE:
712 itemCount = highLowData.getItemCount(series);
713 if (horiz) {
714 xxWidth = dataArea.getHeight() / itemCount;
715 }
716 else {
717 xxWidth = dataArea.getWidth() / itemCount;
718 }
719 break;
720
721 case WIDTHMETHOD_SMALLEST:
722 // Note: It would be nice to pre-calculate this per series
723 itemCount = highLowData.getItemCount(series);
724 double lastPos = -1;
725 xxWidth = dataArea.getWidth();
726 for (int i = 0; i < itemCount; i++) {
727 double pos = domainAxis.valueToJava2D(
728 highLowData.getXValue(series, i), dataArea,
729 domainEdge);
730 if (lastPos != -1) {
731 xxWidth = Math.min(xxWidth,
732 Math.abs(pos - lastPos));
733 }
734 lastPos = pos;
735 }
736 break;
737
738 case WIDTHMETHOD_INTERVALDATA:
739 IntervalXYDataset intervalXYData
740 = (IntervalXYDataset) dataset;
741 double startPos = domainAxis.valueToJava2D(
742 intervalXYData.getStartXValue(series, item),
743 dataArea, plot.getDomainAxisEdge());
744 double endPos = domainAxis.valueToJava2D(
745 intervalXYData.getEndXValue(series, item),
746 dataArea, plot.getDomainAxisEdge());
747 xxWidth = Math.abs(endPos - startPos);
748 break;
749
750 }
751 xxWidth -= 2 * this.autoWidthGap;
752 xxWidth *= this.autoWidthFactor;
753 xxWidth = Math.min(xxWidth, this.maxCandleWidth);
754 volumeWidth = Math.max(Math.min(1, this.maxCandleWidth), xxWidth);
755 stickWidth = Math.max(Math.min(3, this.maxCandleWidth), xxWidth);
756 }
757
758 Paint p = getItemPaint(series, item);
759 Paint outlinePaint = null;
760 if (this.useOutlinePaint) {
761 outlinePaint = getItemOutlinePaint(series, item);
762 }
763 Stroke s = getItemStroke(series, item);
764
765 g2.setStroke(s);
766
767 if (this.drawVolume) {
768 int volume = (int) highLowData.getVolumeValue(series, item);
769 double volumeHeight = volume / this.maxVolume;
770
771 double min, max;
772 if (horiz) {
773 min = dataArea.getMinX();
774 max = dataArea.getMaxX();
775 }
776 else {
777 min = dataArea.getMinY();
778 max = dataArea.getMaxY();
779 }
780
781 double zzVolume = volumeHeight * (max - min);
782
783 g2.setPaint(getVolumePaint());
784 Composite originalComposite = g2.getComposite();
785 g2.setComposite(AlphaComposite.getInstance(
786 AlphaComposite.SRC_OVER, 0.3f));
787
788 if (horiz) {
789 g2.fill(new Rectangle2D.Double(min, xx - volumeWidth / 2,
790 zzVolume, volumeWidth));
791 }
792 else {
793 g2.fill(new Rectangle2D.Double(xx - volumeWidth / 2,
794 max - zzVolume, volumeWidth, zzVolume));
795 }
796
797 g2.setComposite(originalComposite);
798 }
799
800 if (this.useOutlinePaint) {
801 g2.setPaint(outlinePaint);
802 }
803 else {
804 g2.setPaint(p);
805 }
806
807 double yyMaxOpenClose = Math.max(yyOpen, yyClose);
808 double yyMinOpenClose = Math.min(yyOpen, yyClose);
809 double maxOpenClose = Math.max(yOpen, yClose);
810 double minOpenClose = Math.min(yOpen, yClose);
811
812 // draw the upper shadow
813 if (yHigh > maxOpenClose) {
814 if (horiz) {
815 g2.draw(new Line2D.Double(yyHigh, xx, yyMaxOpenClose, xx));
816 }
817 else {
818 g2.draw(new Line2D.Double(xx, yyHigh, xx, yyMaxOpenClose));
819 }
820 }
821
822 // draw the lower shadow
823 if (yLow < minOpenClose) {
824 if (horiz) {
825 g2.draw(new Line2D.Double(yyLow, xx, yyMinOpenClose, xx));
826 }
827 else {
828 g2.draw(new Line2D.Double(xx, yyLow, xx, yyMinOpenClose));
829 }
830 }
831
832 // draw the body
833 Rectangle2D body = null;
834 Rectangle2D hotspot = null;
835 double length = Math.abs(yyHigh - yyLow);
836 double base = Math.min(yyHigh, yyLow);
837 if (horiz) {
838 body = new Rectangle2D.Double(yyMinOpenClose, xx - stickWidth / 2,
839 yyMaxOpenClose - yyMinOpenClose, stickWidth);
840 hotspot = new Rectangle2D.Double(base, xx - stickWidth / 2,
841 length, stickWidth);
842 }
843 else {
844 body = new Rectangle2D.Double(xx - stickWidth / 2, yyMinOpenClose,
845 stickWidth, yyMaxOpenClose - yyMinOpenClose);
846 hotspot = new Rectangle2D.Double(xx - stickWidth / 2,
847 base, stickWidth, length);
848 }
849 if (yClose > yOpen) {
850 if (this.upPaint != null) {
851 g2.setPaint(this.upPaint);
852 }
853 else {
854 g2.setPaint(p);
855 }
856 g2.fill(body);
857 }
858 else {
859 if (this.downPaint != null) {
860 g2.setPaint(this.downPaint);
861 }
862 else {
863 g2.setPaint(p);
864 }
865 g2.fill(body);
866 }
867 if (this.useOutlinePaint) {
868 g2.setPaint(outlinePaint);
869 }
870 else {
871 g2.setPaint(p);
872 }
873 g2.draw(body);
874
875 // add an entity for the item...
876 if (entities != null) {
877 addEntity(entities, hotspot, dataset, series, item, 0.0, 0.0);
878 }
879
880 }
881
882 /**
883 * Tests this renderer for equality with another object.
884 *
885 * @param obj the object (<code>null</code> permitted).
886 *
887 * @return <code>true</code> or <code>false</code>.
888 */
889 public boolean equals(Object obj) {
890 if (obj == this) {
891 return true;
892 }
893 if (!(obj instanceof CandlestickRenderer)) {
894 return false;
895 }
896 CandlestickRenderer that = (CandlestickRenderer) obj;
897 if (this.candleWidth != that.candleWidth) {
898 return false;
899 }
900 if (!PaintUtilities.equal(this.upPaint, that.upPaint)) {
901 return false;
902 }
903 if (!PaintUtilities.equal(this.downPaint, that.downPaint)) {
904 return false;
905 }
906 if (this.drawVolume != that.drawVolume) {
907 return false;
908 }
909 if (this.maxCandleWidthInMilliseconds
910 != that.maxCandleWidthInMilliseconds) {
911 return false;
912 }
913 if (this.autoWidthMethod != that.autoWidthMethod) {
914 return false;
915 }
916 if (this.autoWidthFactor != that.autoWidthFactor) {
917 return false;
918 }
919 if (this.autoWidthGap != that.autoWidthGap) {
920 return false;
921 }
922 if (this.useOutlinePaint != that.useOutlinePaint) {
923 return false;
924 }
925 if (!PaintUtilities.equal(this.volumePaint, that.volumePaint)) {
926 return false;
927 }
928 return super.equals(obj);
929 }
930
931 /**
932 * Returns a clone of the renderer.
933 *
934 * @return A clone.
935 *
936 * @throws CloneNotSupportedException if the renderer cannot be cloned.
937 */
938 public Object clone() throws CloneNotSupportedException {
939 return super.clone();
940 }
941
942 /**
943 * Provides serialization support.
944 *
945 * @param stream the output stream.
946 *
947 * @throws IOException if there is an I/O error.
948 */
949 private void writeObject(ObjectOutputStream stream) throws IOException {
950 stream.defaultWriteObject();
951 SerialUtilities.writePaint(this.upPaint, stream);
952 SerialUtilities.writePaint(this.downPaint, stream);
953 SerialUtilities.writePaint(this.volumePaint, stream);
954 }
955
956 /**
957 * Provides serialization support.
958 *
959 * @param stream the input stream.
960 *
961 * @throws IOException if there is an I/O error.
962 * @throws ClassNotFoundException if there is a classpath problem.
963 */
964 private void readObject(ObjectInputStream stream)
965 throws IOException, ClassNotFoundException {
966 stream.defaultReadObject();
967 this.upPaint = SerialUtilities.readPaint(stream);
968 this.downPaint = SerialUtilities.readPaint(stream);
969 this.volumePaint = SerialUtilities.readPaint(stream);
970 }
971
972 // --- DEPRECATED CODE ----------------------------------------------------
973
974 /**
975 * Returns a flag indicating whether or not volume bars are drawn on the
976 * chart.
977 *
978 * @return <code>true</code> if volume bars are drawn on the chart.
979 *
980 * @deprecated As of 1.0.5, you should use the {@link #getDrawVolume()}
981 * method.
982 */
983 public boolean drawVolume() {
984 return this.drawVolume;
985 }
986
987 }