Example usage for org.jfree.chart.plot XYPlot getRangeAxisLocation

List of usage examples for org.jfree.chart.plot XYPlot getRangeAxisLocation

Introduction

In this page you can find the example usage for org.jfree.chart.plot XYPlot getRangeAxisLocation.

Prototype

public AxisLocation getRangeAxisLocation(int index) 

Source Link

Document

Returns the location for a range axis.

Usage

From source file:org.trade.ui.chart.CandlestickChart.java

/**
 * A demonstration application showing a candlestick chart.
 * //  w w w  .  j ava 2 s.  c o m
 * @param title
 *            the frame title.
 * @param strategyData
 *            StrategyData
 */
public CandlestickChart(final String title, StrategyData strategyData, Tradingday tradingday) {

    this.strategyData = strategyData;
    this.setLayout(new BorderLayout());
    // Used to mark the current price
    stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 10, 3 }, 0);
    valueMarker = new ValueMarker(0.00, Color.black, stroke);

    this.chart = createChart(this.strategyData, title, tradingday);

    BlockContainer container = new BlockContainer(new BorderArrangement());
    container.add(titleLegend1, RectangleEdge.LEFT);
    container.add(titleLegend2, RectangleEdge.RIGHT);
    container.add(new EmptyBlock(2000, 0));
    CompositeTitle legends = new CompositeTitle(container);
    legends.setPosition(RectangleEdge.BOTTOM);
    this.chart.addSubtitle(legends);

    final ChartPanel chartPanel = new ChartPanel(this.chart);
    chartPanel.setFillZoomRectangle(true);
    chartPanel.setMouseZoomable(true, true);
    chartPanel.setRefreshBuffer(true);
    chartPanel.setDoubleBuffered(true);
    chartPanel.setVerticalAxisTrace(true);
    chartPanel.setHorizontalAxisTrace(true);
    chartPanel.addChartMouseListener(new ChartMouseListener() {

        public void chartMouseMoved(ChartMouseEvent e) {
        }

        public void chartMouseClicked(final ChartMouseEvent e) {
            CombinedDomainXYPlot combinedXYplot = (CombinedDomainXYPlot) e.getChart().getPlot();
            @SuppressWarnings("unchecked")
            List<XYPlot> subplots = combinedXYplot.getSubplots();
            if (e.getTrigger().getClickCount() == 2) {
                double xItem = 0;
                double yItem = 0;
                if (e.getEntity() instanceof XYItemEntity) {
                    XYItemEntity xYItemEntity = ((XYItemEntity) e.getEntity());
                    xItem = xYItemEntity.getDataset().getXValue(xYItemEntity.getSeriesIndex(),
                            xYItemEntity.getItem());
                    yItem = xYItemEntity.getDataset().getYValue(xYItemEntity.getSeriesIndex(),
                            xYItemEntity.getItem());
                } else {
                    PlotEntity plotEntity = ((PlotEntity) e.getEntity());
                    XYPlot plot = (XYPlot) plotEntity.getPlot();
                    xItem = plot.getDomainCrosshairValue();
                    yItem = plot.getRangeCrosshairValue();
                }

                for (XYPlot xyplot : subplots) {

                    double x = xyplot.getDomainCrosshairValue();
                    double y = xyplot.getRangeCrosshairValue();

                    /*
                     * If the cross hair is from a right-hand y axis we need
                     * to convert this to a left-hand y axis.
                     */
                    String rightAxisName = ", Price: ";
                    double rangeLowerLeft = 0;
                    double rangeUpperLeft = 0;
                    double rangeLowerRight = 0;
                    double rangeUpperRight = 0;
                    double yRightLocation = 0;
                    for (int index = 0; index < xyplot.getRangeAxisCount(); index++) {
                        AxisLocation axisLocation = xyplot.getRangeAxisLocation(index);
                        Range range = xyplot.getRangeAxis(index).getRange();

                        if (axisLocation.equals(AxisLocation.BOTTOM_OR_LEFT)
                                || axisLocation.equals(AxisLocation.TOP_OR_LEFT)) {
                            rangeLowerLeft = range.getLowerBound();
                            rangeUpperLeft = range.getUpperBound();
                            rightAxisName = ", " + xyplot.getRangeAxis(index).getLabel() + ": ";
                        }
                        if (y >= range.getLowerBound() && y <= range.getUpperBound()
                                && (axisLocation.equals(AxisLocation.BOTTOM_OR_RIGHT)
                                        || axisLocation.equals(AxisLocation.TOP_OR_RIGHT))) {
                            rangeUpperRight = range.getUpperBound();
                            rangeLowerRight = range.getLowerBound();
                        }
                    }
                    if ((rangeUpperRight - rangeLowerRight) > 0) {
                        yRightLocation = rangeLowerLeft + ((rangeUpperLeft - rangeLowerLeft)
                                * ((y - rangeLowerRight) / (rangeUpperRight - rangeLowerRight)));
                    } else {
                        yRightLocation = y;
                    }

                    String text = " Time: " + dateFormatShort.format(new Date((long) (x))) + rightAxisName
                            + new Money(y);
                    if (x == xItem && y == yItem) {
                        titleLegend1.setText(text);
                        if (null == clickCrossHairs) {
                            clickCrossHairs = new XYTextAnnotation(text, x, yRightLocation);
                            clickCrossHairs.setTextAnchor(TextAnchor.BOTTOM_LEFT);
                            xyplot.addAnnotation(clickCrossHairs);
                        } else {
                            clickCrossHairs.setText(text);
                            clickCrossHairs.setX(x);
                            clickCrossHairs.setY(yRightLocation);
                        }
                    }
                }
            } else if (e.getTrigger().getClickCount() == 1 && null != clickCrossHairs) {
                for (XYPlot xyplot : subplots) {
                    if (xyplot.removeAnnotation(clickCrossHairs)) {
                        clickCrossHairs = null;
                        titleLegend1.setText(" Time: 0, Price :0.0");
                        break;
                    }
                }
            }
        }
    });
    this.add(chartPanel, null);
    this.strategyData.getCandleDataset().getSeries(0).addChangeListener(this);
}