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

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

Introduction

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

Prototype

public RectangleEdge getDomainAxisEdge() 

Source Link

Document

Returns the edge for the primary domain axis (taking into account the plot's orientation).

Usage

From source file:apidemo.PanScrollZoomDemo.java

/**
 * Decreases the range on the horizontal axis, centered about a Java2D x coordinate.
 * <P>// w ww .  ja  va  2 s  .c o  m
 * The range on the x axis is multiplied by zoomFactor
 * 
 * @param x  the x coordinate in Java2D space.
 * @param zoomFactor  the zoomFactor < 1 == zoom in; else out.
 */
private void zoomHorizontal(final double x, final double zoomFactor) {

    final JFreeChart chart = this.chartPanel.getChart();
    final ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo();
    if (chart.getPlot() instanceof XYPlot) {
        final XYPlot hvp = (XYPlot) chart.getPlot();
        final ValueAxis axis = hvp.getDomainAxis();
        if (axis != null) {
            final double anchorValue = axis.java2DToValue((float) x, info.getPlotInfo().getDataArea(),
                    hvp.getDomainAxisEdge());
            if (zoomFactor < 1.0) {
                axis.resizeRange(zoomFactor, anchorValue);
            } else if (zoomFactor > 1.0) {
                final Range range = hvp.getDataRange(axis);
                adjustRange(axis, range, zoomFactor, anchorValue);
            }
        }
    }
}

From source file:org.trade.ui.chart.renderer.PivotRenderer.java

/**
 * Draws the visual representation of a single data item.
 * //from  w w w . ja va2  s. c  o m
 * @param g2
 *            the graphics device.
 * @param state
 *            the renderer state.
 * @param dataArea
 *            the area within which the data is being drawn.
 * @param info
 *            collects information about the drawing.
 * @param plot
 *            the plot (can be used to obtain standard color information
 *            etc).
 * @param domainAxis
 *            the domain (horizontal) axis.
 * @param rangeAxis
 *            the range (vertical) axis.
 * @param dataset
 *            the dataset.
 * @param series
 *            the series index (zero-based).
 * @param item
 *            the item index (zero-based).
 * @param crosshairState
 *            crosshair information for the plot (<code>null</code>
 *            permitted).
 * @param pass
 *            the pass index.
 * @see org.jfree.chart.renderer.xy.XYItemRenderer#drawItem(Graphics2D,
 *      XYItemRendererState, Rectangle2D, PlotRenderingInfo, XYPlot,
 *      ValueAxis, ValueAxis, XYDataset, int, int, CrosshairState, int)
 */
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    // do nothing if item is not visible
    if (!getItemVisible(series, item)) {
        return;
    }

    // get the data point...
    double x = dataset.getXValue(series, item);
    double y = dataset.getYValue(series, item);
    double adjx = (this.dotWidth - 1) / 2.0;
    double adjy = (this.dotHeight - 1) / 2.0;
    if (!Double.isNaN(y)) {
        RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
        RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
        double transX = domainAxis.valueToJava2D(x, dataArea, xAxisLocation) - adjx;
        double transY = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation) - adjy;

        g2.setPaint(getItemPaint(series, item));
        PlotOrientation orientation = plot.getOrientation();
        if (orientation == PlotOrientation.HORIZONTAL) {
            g2.fillRect((int) transY, (int) transX, this.dotHeight, this.dotWidth);
        } else if (orientation == PlotOrientation.VERTICAL) {
            g2.fillRect((int) transX, (int) transY, this.dotWidth, this.dotHeight);
        }

        int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
        int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
        updateCrosshairValues(crosshairState, x, y, domainAxisIndex, rangeAxisIndex, transX, transY,
                orientation);

        PivotDataset pivotDataset = (PivotDataset) dataset;
        if (null != pivotDataset.getPivotSide(series, item)) {
            String ledgend = "Pivot";
            if (pivotDataset.getPivotSide(series, item).equals(Side.BOT)) {
                drawPivotArrow(g2, plot, dataArea, domainAxis, rangeAxis, item, info, 45d, x,
                        pivotDataset.getPivotValue(series, item), ledgend);
            } else {
                drawPivotArrow(g2, plot, dataArea, domainAxis, rangeAxis, item, info, -45d, x,
                        pivotDataset.getPivotValue(series, item), ledgend);
            }
        }
    }

}

From source file:com.diversityarrays.kdxplore.scatterplot.ScatterPlotPanel.java

private void setXYValues(ChartMouseEvent cme) {

    double pointX = cme.getTrigger().getPoint().x;
    double pointY = cme.getTrigger().getPoint().y;

    Rectangle2D plotArea = getChartPanel().getScreenDataArea();
    XYPlot plot = (XYPlot) getChart().getPlot();
    Double chartX = plot.getDomainAxis().java2DToValue(pointX, plotArea, plot.getDomainAxisEdge());
    Double chartY = plot.getRangeAxis().java2DToValue(pointY, plotArea, plot.getRangeAxisEdge());

    if (!stillChanging) {
        mouseDownPoint = new Point2D.Double(chartX, chartY);
        stillChanging = true;//from w  w  w  . j a va  2s  .  com
    } else {
        mouseUpPoint = new Point2D.Double(chartX, chartY);
    }

    if (mouseDownPoint != null && mouseUpPoint != null) {

        double dn = mouseDownPoint.getX();
        double up = mouseUpPoint.getX();

        double x0 = Math.min(dn, up);
        double x1 = Math.max(dn, up);

        dn = mouseDownPoint.getY();
        up = mouseUpPoint.getY();

        double y0 = Math.min(dn, up);
        double y1 = Math.max(dn, up);

        setSelectionRange(x0, y0, x1, y1);
    }
}

From source file:projects.wdlf47tuc.ProcessAllSwathcal.java

/**
 * Main entry point.//  w  w  w .jav  a2 s  .  com
 * 
 * @param args
 * 
 * @throws IOException 
 * 
 */
public ProcessAllSwathcal() {

    // Path to AllSwathcal.dat file
    File allSwathcal = new File(
            "/home/nrowell/Astronomy/Data/47_Tuc/Kalirai_2012/UVIS/www.stsci.edu/~jkalirai/47Tuc/AllSwathcal.dat");

    // Read file contents into the List
    try (BufferedReader in = new BufferedReader(new FileReader(allSwathcal))) {
        String sourceStr;
        while ((sourceStr = in.readLine()) != null) {
            Source source = Source.parseSource(sourceStr);
            if (source != null) {
                allSources.add(source);
            }
        }
    } catch (IOException e) {
    }

    logger.info("Parsed " + allSources.size() + " Sources from AllSwathcal.dat");

    // Initialise chart
    cmdPanel = new ChartPanel(updateDataAndPlotCmd(allSources));
    cmdPanel.addChartMouseListener(new ChartMouseListener() {
        @Override
        public void chartMouseClicked(ChartMouseEvent e) {
            // Capture mouse click location, transform to graph coordinates and add
            // a point to the polygonal selection box.
            Point2D p = cmdPanel.translateScreenToJava2D(e.getTrigger().getPoint());
            Rectangle2D plotArea = cmdPanel.getScreenDataArea();
            XYPlot plot = (XYPlot) cmdPanel.getChart().getPlot();
            double chartX = plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge());
            double chartY = plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge());
            points.add(new double[] { chartX, chartY });
            cmdPanel.setChart(plotCmd());
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent arg0) {
        }
    });

    // Create colour combo boxes
    final JComboBox<Filter> magComboBox = new JComboBox<Filter>(filters);
    final JComboBox<Filter> col1ComboBox = new JComboBox<Filter>(filters);
    final JComboBox<Filter> col2ComboBox = new JComboBox<Filter>(filters);

    // Set initial values
    magComboBox.setSelectedItem(magFilter);
    col1ComboBox.setSelectedItem(col1Filter);
    col2ComboBox.setSelectedItem(col2Filter);

    // Create an action listener for these
    ActionListener al = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            if (evt.getSource() == magComboBox) {
                magFilter = (Filter) magComboBox.getSelectedItem();
            }
            if (evt.getSource() == col1ComboBox) {
                col1Filter = (Filter) col1ComboBox.getSelectedItem();
            }
            if (evt.getSource() == col2ComboBox) {
                col2Filter = (Filter) col2ComboBox.getSelectedItem();
            }
            // Changed colour(s), so reset selection box coordinates
            points.clear();
            cmdPanel.setChart(updateDataAndPlotCmd(allSources));
        }
    };
    magComboBox.addActionListener(al);
    col1ComboBox.addActionListener(al);
    col2ComboBox.addActionListener(al);
    // Add a bit of padding to space things out
    magComboBox.setBorder(new EmptyBorder(5, 5, 5, 5));
    col1ComboBox.setBorder(new EmptyBorder(5, 5, 5, 5));
    col2ComboBox.setBorder(new EmptyBorder(5, 5, 5, 5));

    // Set up statistic sliders
    final JSlider magErrMaxSlider = GuiUtil.buildSlider(magErrorRangeMin, magErrorRangeMax, 3, "%3.3f");
    final JSlider chi2MaxSlider = GuiUtil.buildSlider(chi2RangeMin, chi2RangeMax, 3, "%3.3f");
    final JSlider sharpMinSlider = GuiUtil.buildSlider(sharpRangeMin, sharpRangeMax, 3, "%3.3f");
    final JSlider sharpMaxSlider = GuiUtil.buildSlider(sharpRangeMin, sharpRangeMax, 3, "%3.3f");

    // Set intial values
    magErrMaxSlider.setValue(
            (int) Math.rint(100.0 * (magErrMax - magErrorRangeMin) / (magErrorRangeMax - magErrorRangeMin)));
    chi2MaxSlider.setValue((int) Math.rint(100.0 * (chi2Max - chi2RangeMin) / (chi2RangeMax - chi2RangeMin)));
    sharpMinSlider
            .setValue((int) Math.rint(100.0 * (sharpMin - sharpRangeMin) / (sharpRangeMax - sharpRangeMin)));
    sharpMaxSlider
            .setValue((int) Math.rint(100.0 * (sharpMax - sharpRangeMin) / (sharpRangeMax - sharpRangeMin)));

    // Set labels & initial values
    final JLabel magErrMaxLabel = new JLabel(getMagErrMaxLabel());
    final JLabel chi2MaxLabel = new JLabel(getChi2MaxLabel());
    final JLabel sharpMinLabel = new JLabel(getSharpMinLabel());
    final JLabel sharpMaxLabel = new JLabel(getSharpMaxLabel());

    // Create a change listener fot these
    ChangeListener cl = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();

            if (source == magErrMaxSlider) {
                // Compute max mag error from slider position
                double newMagErrMax = magErrorRangeMin
                        + (magErrorRangeMax - magErrorRangeMin) * (source.getValue() / 100.0);
                magErrMax = newMagErrMax;
                magErrMaxLabel.setText(getMagErrMaxLabel());
            }
            if (source == chi2MaxSlider) {
                // Compute Chi2 max from slider position
                double newChi2Max = chi2RangeMin + (chi2RangeMax - chi2RangeMin) * (source.getValue() / 100.0);
                chi2Max = newChi2Max;
                chi2MaxLabel.setText(getChi2MaxLabel());
            }
            if (source == sharpMinSlider) {
                // Compute sharp min from slider position
                double newSharpMin = sharpRangeMin
                        + (sharpRangeMax - sharpRangeMin) * (source.getValue() / 100.0);
                sharpMin = newSharpMin;
                sharpMinLabel.setText(getSharpMinLabel());
            }
            if (source == sharpMaxSlider) {
                // Compute sharp max from slider position
                double newSharpMax = sharpRangeMin
                        + (sharpRangeMax - sharpRangeMin) * (source.getValue() / 100.0);
                sharpMax = newSharpMax;
                sharpMaxLabel.setText(getSharpMaxLabel());
            }
            cmdPanel.setChart(updateDataAndPlotCmd(allSources));
        }
    };
    magErrMaxSlider.addChangeListener(cl);
    chi2MaxSlider.addChangeListener(cl);
    sharpMinSlider.addChangeListener(cl);
    sharpMaxSlider.addChangeListener(cl);
    // Add a bit of padding to space things out
    magErrMaxSlider.setBorder(new EmptyBorder(5, 5, 5, 5));
    chi2MaxSlider.setBorder(new EmptyBorder(5, 5, 5, 5));
    sharpMinSlider.setBorder(new EmptyBorder(5, 5, 5, 5));
    sharpMaxSlider.setBorder(new EmptyBorder(5, 5, 5, 5));

    // Text field to store distance modulus
    final JTextField distanceModulusField = new JTextField(Double.toString(mu));
    distanceModulusField.setBorder(new EmptyBorder(5, 5, 5, 5));

    Border compound = BorderFactory.createCompoundBorder(new LineBorder(this.getBackground(), 5),
            BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

    final JButton lfButton = new JButton("Luminosity function for selection");
    lfButton.setBorder(compound);
    lfButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            // Read distance modulus field
            try {
                double mu_new = Double.parseDouble(distanceModulusField.getText());
                mu = mu_new;
            } catch (NullPointerException | NumberFormatException ex) {
                JOptionPane.showMessageDialog(lfButton,
                        "Error parsing the distance modulus: " + ex.getMessage(), "Distance Modulus Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (boxedSources.isEmpty()) {
                JOptionPane.showMessageDialog(lfButton, "No sources are currently selected!", "Selection Error",
                        JOptionPane.ERROR_MESSAGE);
            } else {
                computeAndPlotLuminosityFunction(boxedSources);
            }
        }
    });
    final JButton clearSelectionButton = new JButton("Clear selection");
    clearSelectionButton.setBorder(compound);
    clearSelectionButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            points.clear();
            cmdPanel.setChart(plotCmd());
        }
    });

    JPanel controls = new JPanel(new GridLayout(9, 2));
    controls.setBorder(new EmptyBorder(10, 10, 10, 10));
    controls.add(new JLabel("Magnitude = "));
    controls.add(magComboBox);
    controls.add(new JLabel("Colour 1 = "));
    controls.add(col1ComboBox);
    controls.add(new JLabel("Colour 2 = "));
    controls.add(col2ComboBox);
    controls.add(magErrMaxLabel);
    controls.add(magErrMaxSlider);
    controls.add(chi2MaxLabel);
    controls.add(chi2MaxSlider);
    controls.add(sharpMinLabel);
    controls.add(sharpMinSlider);
    controls.add(sharpMaxLabel);
    controls.add(sharpMaxSlider);
    controls.add(new JLabel("Adopted distance modulus = "));
    controls.add(distanceModulusField);
    controls.add(lfButton);
    controls.add(clearSelectionButton);

    this.setLayout(new BorderLayout());
    this.add(cmdPanel, BorderLayout.CENTER);
    this.add(controls, BorderLayout.SOUTH);

    this.validate();
}

From source file:com.att.aro.main.PacketPlots.java

/**
 * The utility method that creates the packet plots from a packet series map
 *///from w  w  w. j  a  v a  2 s  .  c  o m
private XYPlot createPlot() {

    // Create the plot renderer
    YIntervalRenderer renderer = new YIntervalRenderer() {
        private static final long serialVersionUID = 1L;

        public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea,
                PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis,
                XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) {

            // setup for collecting optional entity info...
            Shape entityArea = null;
            EntityCollection entities = null;
            if (info != null) {
                entities = info.getOwner().getEntityCollection();
            }

            IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;

            double x = intervalDataset.getXValue(series, item);
            double yLow = intervalDataset.getStartYValue(series, item);
            double yHigh = intervalDataset.getEndYValue(series, item);

            RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
            RectangleEdge yAxisLocation = plot.getRangeAxisEdge();

            double xx = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
            double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, yAxisLocation);
            double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, yAxisLocation);

            Paint p = getItemPaint(series, item);
            Stroke s = getItemStroke(series, item);

            Line2D line = null;
            PlotOrientation orientation = plot.getOrientation();
            if (orientation == PlotOrientation.HORIZONTAL) {
                line = new Line2D.Double(yyLow, xx, yyHigh, xx);
            } else if (orientation == PlotOrientation.VERTICAL) {
                line = new Line2D.Double(xx, yyLow, xx, yyHigh);
            }
            g2.setPaint(p);
            g2.setStroke(s);
            g2.draw(line);

            // add an entity for the item...
            if (entities != null) {
                if (entityArea == null) {
                    entityArea = line.getBounds();
                }
                String tip = null;
                XYToolTipGenerator generator = getToolTipGenerator(series, item);
                if (generator != null) {
                    tip = generator.generateToolTip(dataset, series, item);
                }
                XYItemEntity entity = new XYItemEntity(entityArea, dataset, series, item, tip, null);
                entities.add(entity);
            }

        }

    };
    renderer.setAdditionalItemLabelGenerator(null);
    renderer.setBaseShape(new Rectangle());
    renderer.setAutoPopulateSeriesShape(false);
    renderer.setAutoPopulateSeriesPaint(false);
    renderer.setBasePaint(Color.GRAY);

    // Create the plot
    XYPlot plot = new XYPlot(null, null, new NumberAxis(), renderer);
    plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
    plot.getRangeAxis().setVisible(false);

    return plot;
}

From source file:net.sourceforge.processdash.ev.ui.chart.RangeXYItemRenderer.java

/** Draws the visual representation of a single data item.
 *///from   w ww.ja  va  2  s .  co  m
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairInfo, int pass) {

    // setup for collecting optional entity info...
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }
    Shape entityArea = null;

    Paint paint = getItemPaint(series, item);
    Stroke seriesStroke = getItemStroke(series, item);
    g2.setPaint(paint);
    g2.setStroke(seriesStroke);

    // get the data point...
    Number x1n = dataset.getX(series, item);
    Number y1n = dataset.getY(series, item);
    if (y1n == null || x1n == null) {
        return;
    }

    double x1 = x1n.doubleValue();
    double y1 = y1n.doubleValue();
    final RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    final RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
    PlotOrientation orientation = plot.getOrientation();

    if (item > 0) {
        // get the previous data point...
        Number x0n = dataset.getX(series, item - 1);
        Number y0n = dataset.getY(series, item - 1);
        if (y0n != null && x0n != null) {
            double x0 = x0n.doubleValue();
            double y0 = y0n.doubleValue();

            double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
            double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

            // only draw if we have good values
            if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1)
                    || Double.isNaN(transY1)) {
                return;
            }

            if (orientation == PlotOrientation.HORIZONTAL) {
                line.setLine(transY0, transX0, transY1, transX1);
            } else if (orientation == PlotOrientation.VERTICAL) {
                line.setLine(transX0, transY0, transX1, transY1);
            }

            if (y1n instanceof RangeInfo) {
                RangeInfo y1r = (RangeInfo) y1n;
                double transY1low = rangeAxis.valueToJava2D(y1r.getRangeLowerBound(false), dataArea,
                        yAxisLocation);
                double transY1high = rangeAxis.valueToJava2D(y1r.getRangeUpperBound(false), dataArea,
                        yAxisLocation);
                drawItemRangeGradient(g2, line, paint, seriesStroke, transX1, transY1low, transX1, transY1high);

            } else if (x1n instanceof RangeInfo) {
                RangeInfo x1r = (RangeInfo) x1n;
                double transX1low = domainAxis.valueToJava2D(x1r.getRangeLowerBound(false), dataArea,
                        xAxisLocation);
                double transX1high = domainAxis.valueToJava2D(x1r.getRangeUpperBound(false), dataArea,
                        xAxisLocation);
                drawItemRangeGradient(g2, line, paint, seriesStroke, transX1low, transY1, transX1high, transY1);

            } else if (line.intersects(dataArea)) {
                g2.draw(line);
            }
        }
    } else if (dataset.getItemCount(series) == 1) {
        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        }
        if (shape.intersects(dataArea)) {
            if (getItemShapeFilled(series, item)) {
                g2.fill(shape);
            } else {
                g2.draw(shape);
            }
        }
        entityArea = shape;
    }

    if (entities != null && (dataArea.contains(transX1, transY1) || entityArea != null)) {
        addEntity(entities, entityArea, dataset, series, item, transX1, transY1);
    }

}

From source file:anl.verdi.plot.jfree.XYBlockRenderer.java

/**
 * Draws the block representing the specified item.
 *
 * @param g2             the graphics device.
 * @param state          the state./* w w w  . j  av a  2 s .c om*/
 * @param dataArea       the data area.
 * @param info           the plot rendering info.
 * @param plot           the plot.
 * @param domainAxis     the x-axis.
 * @param rangeAxis      the y-axis.
 * @param dataset        the dataset.
 * @param series         the series index.
 * @param item           the item index.
 * @param crosshairState the crosshair state.
 * @param pass           the pass index.
 */
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    double x = dataset.getXValue(series, item);
    double y = dataset.getYValue(series, item);
    double z = 0.0;
    double max = paintScale.getUpperBound();
    double min = paintScale.getLowerBound();

    if (dataset instanceof XYZDataset)
        z = ((XYZDataset) dataset).getZValue(series, item);

    //NOTE: so to get the max/min color instead of unknown (Qun He, UNC, 03/19/2009)
    if (z > max)
        z = max;

    if (z < min)
        z = min;

    Color p = (Color) this.paintScale.getPaint(z);

    double xx0 = domainAxis.valueToJava2D(x + this.xOffset, dataArea, plot.getDomainAxisEdge());
    double yy0 = rangeAxis.valueToJava2D(y + this.yOffset, dataArea, plot.getRangeAxisEdge());
    double xx1 = domainAxis.valueToJava2D(x + this.blockWidth + this.xOffset, dataArea,
            plot.getDomainAxisEdge());
    double yy1 = rangeAxis.valueToJava2D(y + this.blockHeight + this.yOffset, dataArea,
            plot.getRangeAxisEdge());
    Rectangle2D block;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation.equals(PlotOrientation.HORIZONTAL)) {
        block = new Rectangle2D.Double(Math.min(yy0, yy1), Math.min(xx0, xx1), Math.abs(yy1 - yy0),
                Math.abs(xx0 - xx1));
    } else {
        block = new Rectangle2D.Double(Math.min(xx0, xx1), Math.min(yy0, yy1), Math.abs(xx1 - xx0),
                Math.abs(yy1 - yy0));
    }
    g2.setColor(p);
    g2.fill(block);

    if (gridLinesEnabled) {
        boolean aaOn = false;
        if (g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING) == RenderingHints.VALUE_ANTIALIAS_ON) {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            aaOn = true;
        }
        g2.setPaint(gridLineColor);
        g2.setStroke(gridLineStroke);
        g2.draw(block);
        if (aaOn)
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } else {
        g2.setStroke(basicStroke);
        g2.draw(block);
    }
}

From source file:edu.dlnu.liuwenpeng.render.CandlestickRenderer.java

/**    
* Initialises the renderer then returns the number of 'passes' through the    
* data that the renderer will require (usually just one).  This method    
* will be called before the first item is rendered, giving the renderer    
* an opportunity to initialise any state information it wants to maintain.    
* The renderer can do nothing if it chooses.    
*    //w w  w .j a v  a  2  s . c  o  m
* @param g2  the graphics device.    
* @param dataArea  the area inside the axes.    
* @param plot  the plot.    
* @param dataset  the data.    
* @param info  an optional info collection object to return data back to    
*              the caller.    
*    
* @return The number of passes the renderer requires.    
*/
public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, XYPlot plot, XYDataset dataset,
        PlotRenderingInfo info) {

    // calculate the maximum allowed candle width from the axis...    
    ValueAxis axis = plot.getDomainAxis();
    double x1 = axis.getLowerBound();
    double x2 = x1 + this.maxCandleWidthInMilliseconds;
    RectangleEdge edge = plot.getDomainAxisEdge();
    double xx1 = axis.valueToJava2D(x1, dataArea, edge);
    double xx2 = axis.valueToJava2D(x2, dataArea, edge);
    this.maxCandleWidth = Math.abs(xx2 - xx1);
    // Absolute value, since the relative x    
    // positions are reversed for horizontal orientation    

    // calculate the highest volume in the dataset...    
    if (this.drawVolume) {
        OHLCDataset highLowDataset = (OHLCDataset) dataset;
        this.maxVolume = 0.0;
        for (int series = 0; series < highLowDataset.getSeriesCount(); series++) {
            for (int item = 0; item < highLowDataset.getItemCount(series); item++) {
                double volume = highLowDataset.getVolumeValue(series, item);
                if (volume > this.maxVolume) {
                    this.maxVolume = volume;
                }

            }
        }
    }

    return new XYItemRendererState(info);
}

From source file:org.trade.ui.chart.renderer.HeikinAshiRenderer.java

/**
 * Method drawItem.//from w  ww .j a v a2  s. c om
 * 
 * @param g2
 *            Graphics2D
 * @param state
 *            XYItemRendererState
 * @param dataArea
 *            Rectangle2D
 * @param info
 *            PlotRenderingInfo
 * @param plot
 *            XYPlot
 * @param domainAxis
 *            ValueAxis
 * @param rangeAxis
 *            ValueAxis
 * @param dataset
 *            XYDataset
 * @param series
 *            int
 * @param item
 *            int
 * @param crosshairState
 *            CrosshairState
 * @param pass
 *            int
 * @see org.jfree.chart.renderer.xy.XYItemRenderer#drawItem(Graphics2D,
 *      XYItemRendererState, Rectangle2D, PlotRenderingInfo, XYPlot,
 *      ValueAxis, ValueAxis, XYDataset, int, int, CrosshairState, int)
 */
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    if (dataset instanceof HeikinAshiDataset) {

        // setup for collecting optional entity info...
        EntityCollection entities = null;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
        }

        HeikinAshiDataset highLowData = (HeikinAshiDataset) dataset;

        double x = highLowData.getXValue(series, item);
        double yHigh = highLowData.getHighValue(series, item);
        double yLow = highLowData.getLowValue(series, item);
        double yOpen = highLowData.getOpenValue(series, item);
        double yClose = highLowData.getCloseValue(series, item);

        RectangleEdge domainEdge = plot.getDomainAxisEdge();
        double xx = domainAxis.valueToJava2D(x, dataArea, domainEdge);

        RectangleEdge edge = plot.getRangeAxisEdge();
        double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, edge);
        double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, edge);
        double yyOpen = rangeAxis.valueToJava2D(yOpen, dataArea, edge);
        double yyClose = rangeAxis.valueToJava2D(yClose, dataArea, edge);

        int itemCount = highLowData.getItemCount(series);
        double xxWidth = dataArea.getWidth() / itemCount;

        xxWidth -= 2 * this.getAutoWidthGap();
        xxWidth *= this.getAutoWidthFactor();
        xxWidth = Math.min(xxWidth, this.maxCandleWidth);
        double stickWidth = Math.max(Math.min(3, this.maxCandleWidth), xxWidth);

        Paint outlinePaint = null;
        outlinePaint = getItemOutlinePaint(series, item);
        g2.setStroke(getItemStroke(series, item));
        g2.setPaint(outlinePaint);

        double yyMaxOpenClose = Math.max(yyOpen, yyClose);
        double yyMinOpenClose = Math.min(yyOpen, yyClose);
        double maxOpenClose = Math.max(yOpen, yClose);
        double minOpenClose = Math.min(yOpen, yClose);

        Shape body = null;
        boolean highlight = highlight(series, item);
        /**********************************
         * draw the upper shadow START
         **********************************/

        if (yHigh > maxOpenClose) {
            if (highlight) {
                body = new Rectangle2D.Double(xx - (stickWidth / 2), yyHigh - 10, stickWidth,
                        (yyMaxOpenClose - yyHigh) + 10);
                g2.setPaint(Color.YELLOW);
                g2.fill(body);
                g2.draw(body);
            }
        }

        if (yHigh > maxOpenClose) {
            if (nightMode) {
                if (yClose > yOpen) {
                    g2.setPaint(upPaint);
                } else {
                    g2.setPaint(downPaint);
                }
            } else {
                g2.setPaint(Color.black);
            }

            g2.draw(new Line2D.Double(xx, yyHigh, xx, yyMaxOpenClose));
        }

        /**********************************
         * draw the lower shadow START
         **********************************/
        if (yLow < minOpenClose) {
            if (highlight) {
                body = new Rectangle2D.Double(xx - (stickWidth / 2), yyMinOpenClose, stickWidth,
                        (yyLow - yyMinOpenClose) + 10);
                g2.setPaint(Color.YELLOW);
                g2.fill(body);
                g2.draw(body);
            }
            if (yLow < minOpenClose) {
                if (nightMode) {
                    if (yClose > yOpen) {
                        g2.setPaint(upPaint);
                    } else {
                        g2.setPaint(downPaint);
                    }
                } else {
                    g2.setPaint(Color.BLACK);
                }
                g2.draw(new Line2D.Double(xx, yyLow, xx, yyMinOpenClose));
            }
        }

        /**********************************
         * draw the body
         **********************************/

        body = new Rectangle2D.Double(xx - (stickWidth / 2), yyMinOpenClose, stickWidth,
                yyMaxOpenClose - yyMinOpenClose);

        if (nightMode) {
            g2.setPaint(Color.white);
        } else {
            if (yClose > yOpen) {
                g2.setPaint(upPaint);
            } else {
                g2.setPaint(downPaint);
            }
        }

        g2.fill(body);
        g2.draw(body);

        if (nightMode) {
            if (yClose > yOpen) {
                g2.setPaint(upPaint);
            } else {
                g2.setPaint(downPaint);
            }
        } else {
            g2.setPaint(outlinePaint);
        }
        g2.draw(body);
        // add an entity for the item...
        if (entities != null) {
            String tip = null;
            XYToolTipGenerator generator = getToolTipGenerator(series, item);
            if (generator != null) {
                tip = generator.generateToolTip(dataset, series, item);
            }

            XYItemEntity entity = new XYItemEntity(body, dataset, series, item, tip, null);

            entities.add(entity);
        }
    }
}

From source file:com.newatlanta.bluedragon.CustomXYAreaRenderer.java

public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {
    super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item,
            crosshairState, pass);//  ww w.  ja v a  2  s .com

    // The complete area chart is drawn when drawItem() is called for the last
    // item
    // so we need to draw all of the item labels after the last item so they'll
    // be
    // visible if they overlap the area chart.
    int itemCount = dataset.getItemCount(series);
    if (getPlotArea() && item > 0 && item == (itemCount - 1)) {
        // this is the last item so draw the item labels
        PlotOrientation orientation = plot.getOrientation();

        for (int i = 0; i < itemCount; i++) {
            if (isItemLabelVisible(series, i)) {
                double xValue = dataset.getXValue(series, i);
                double yValue = dataset.getYValue(series, i);
                if (Double.isNaN(yValue)) {
                    yValue = 0.0;
                }
                double transXValue = domainAxis.valueToJava2D(xValue, dataArea, plot.getDomainAxisEdge());
                double transYValue = rangeAxis.valueToJava2D(yValue, dataArea, plot.getRangeAxisEdge());

                double xx = transXValue;
                double yy = transYValue;
                if (orientation == PlotOrientation.HORIZONTAL) {
                    xx = transYValue;
                    yy = transXValue;
                }
                drawItemLabel(g2, orientation, dataset, series, i, xx, yy, (yValue < 0.0));
            }
        }
    }
}