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:de.saring.util.gui.jfreechart.StackedRenderer.java

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2 the graphics device.//ww  w  . ja  v a 2s.co  m
 * @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 axis.
 * @param rangeAxis the range axis.
 * @param dataset the dataset.
 * @param series the series index (zero-based).
 * @param item the item index (zero-based).
 * @param crosshairState information about crosshairs on a plot.
 * @param pass the pass index.
 */
@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 crosshairState, int pass) {

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

    TableXYDataset tdataset = (TableXYDataset) dataset;

    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(y1)) {
        y1 = 0.0;
    }
    double[] stack1 = getStackValues(tdataset, series, item);

    // get the previous point and the next point so we can calculate a 
    // "hot spot" for the area (used by the chart entity)...
    double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
    double y0 = dataset.getYValue(series, Math.max(item - 1, 0));
    if (Double.isNaN(y0)) {
        y0 = 0.0;
    }
    double[] stack0 = getStackValues(tdataset, series, Math.max(item - 1, 0));

    int itemCount = dataset.getItemCount(series);
    double x2 = dataset.getXValue(series, Math.min(item + 1, itemCount - 1));
    double y2 = dataset.getYValue(series, Math.min(item + 1, itemCount - 1));
    if (Double.isNaN(y2)) {
        y2 = 0.0;
    }
    double[] stack2 = getStackValues(tdataset, series, Math.min(item + 1, itemCount - 1));

    double xleft = (x0 + x1) / 2.0;
    double xright = (x1 + x2) / 2.0;
    double[] stackLeft = averageStackValues(stack0, stack1);
    double[] stackRight = averageStackValues(stack1, stack2);
    double[] adjStackLeft = adjustedStackValues(stack0, stack1);
    double[] adjStackRight = adjustedStackValues(stack1, stack2);

    RectangleEdge edge0 = plot.getDomainAxisEdge();

    float transX1 = (float) domainAxis.valueToJava2D(x1, dataArea, edge0);
    float transXLeft = (float) domainAxis.valueToJava2D(xleft, dataArea, edge0);
    float transXRight = (float) domainAxis.valueToJava2D(xright, dataArea, edge0);

    if (this.roundXCoordinates) {
        transX1 = Math.round(transX1);
        transXLeft = Math.round(transXLeft);
        transXRight = Math.round(transXRight);
    }
    float transY1;

    RectangleEdge edge1 = plot.getRangeAxisEdge();

    GeneralPath left = new GeneralPath();
    GeneralPath right = new GeneralPath();
    if (y1 >= 0.0) { // handle positive value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[1], dataArea, edge1);
        float transStack1 = (float) rangeAxis.valueToJava2D(stack1[1], dataArea, edge1);
        float transStackLeft = (float) rangeAxis.valueToJava2D(stackLeft[1], dataArea, edge1); // other than StackedXYAreaRenderer2!

        // LEFT POLYGON
        if (y0 >= 0.0) {
            double yleft = (y0 + y1) / 2.0 + stackLeft[1];
            float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            left.moveTo(transX1, transY1);
            left.lineTo(transX1, transStack1);
            left.lineTo(transXLeft, transStackLeft);
            left.lineTo(transXLeft, transYLeft);
            left.closePath();
        } else {
            left.moveTo(transX1, transStack1);
            left.lineTo(transX1, transY1);
            left.lineTo(transXLeft, transStackLeft);
            left.closePath();
        }

        float transStackRight = (float) rangeAxis.valueToJava2D(stackRight[1], dataArea, edge1); // other than StackedXYAreaRenderer2!
        // RIGHT POLYGON
        if (y2 >= 0.0) {
            double yright = (y1 + y2) / 2.0 + stackRight[1];
            float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            right.moveTo(transX1, transStack1);
            right.lineTo(transX1, transY1);
            right.lineTo(transXRight, transYRight);
            right.lineTo(transXRight, transStackRight);
            right.closePath();
        } else {
            right.moveTo(transX1, transStack1);
            right.lineTo(transX1, transY1);
            right.lineTo(transXRight, transStackRight);
            right.closePath();
        }
    } else { // handle negative value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[0], dataArea, edge1);
        float transStack1 = (float) rangeAxis.valueToJava2D(stack1[0], dataArea, edge1);
        float transStackLeft = (float) rangeAxis.valueToJava2D(adjStackLeft[0], dataArea, edge1);

        // LEFT POLYGON
        if (y0 >= 0.0) {
            left.moveTo(transX1, transStack1);
            left.lineTo(transX1, transY1);
            left.lineTo(transXLeft, transStackLeft);
            left.clone();
        } else {
            double yleft = (y0 + y1) / 2.0 + stackLeft[0];
            float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            left.moveTo(transX1, transY1);
            left.lineTo(transX1, transStack1);
            left.lineTo(transXLeft, transStackLeft);
            left.lineTo(transXLeft, transYLeft);
            left.closePath();
        }
        float transStackRight = (float) rangeAxis.valueToJava2D(adjStackRight[0], dataArea, edge1);

        // RIGHT POLYGON
        if (y2 >= 0.0) {
            right.moveTo(transX1, transStack1);
            right.lineTo(transX1, transY1);
            right.lineTo(transXRight, transStackRight);
            right.closePath();
        } else {
            double yright = (y1 + y2) / 2.0 + stackRight[0];
            float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            right.moveTo(transX1, transStack1);
            right.lineTo(transX1, transY1);
            right.lineTo(transXRight, transYRight);
            right.lineTo(transXRight, transStackRight);
            right.closePath();
        }
    }

    //  Get series Paint and Stroke
    Paint itemPaint = getItemPaint(series, item);
    if (pass == 0) {
        g2.setPaint(itemPaint);
        g2.fill(left);
        g2.fill(right);
    } else if (pass == 1) {
        if (item == 0 || (y1 == 0.0 && y0 == 0.0)) {
            return;
        }

        // get the data point...
        if (Double.isNaN(y1) || Double.isNaN(x1)) {
            return;
        }

        if (Double.isNaN(y0) || Double.isNaN(x0)) {
            return;
        }

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

        double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
        double transY0 = rangeAxis.valueToJava2D(y0 + stack0[1], dataArea, yAxisLocation);

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

        state.workingLine.setLine(transX0, transY0, transX1, transY1);

        if (state.workingLine.intersects(dataArea)) {
            g2.setStroke(getItemStroke(series, item));
            g2.setPaint(super.lookupSeriesPaint(series));
            g2.draw(state.workingLine);
        }

        return;
    }

    // add an entity for the item...
    if (entities != null) {
        GeneralPath gp = new GeneralPath(left);
        gp.append(right, false);
        entityArea = gp;
        addEntity(entities, entityArea, dataset, series, item, transX1, transY1);
    }

}

From source file:com.att.aro.diagnostics.GraphPanel.java

/**
 * Returns the Cross hair current coordinate.
 *///from  w ww.jav  a 2  s.c om
private int getHandleCoordinate() {
    Rectangle2D plotArea = advancedGraphPanel.getScreenDataArea();
    XYPlot plot = (XYPlot) advancedGraph.getPlot();
    int handleCoordinate = new Float(
            plot.getDomainAxis().valueToJava2D(getCrosshair(), plotArea, plot.getDomainAxisEdge())).intValue();
    return handleCoordinate;
}

From source file:com.att.aro.diagnostics.GraphPanel.java

/**
 * This method is invoked when a mouse click event occurs in the GraphPanel.
 *//*w  w w.j  a v  a  2  s. c  o m*/
@Override
public void chartMouseClicked(ChartMouseEvent chartmouseevent) {
    Point2D point = chartmouseevent.getTrigger().getPoint();
    Rectangle2D plotArea = advancedGraphPanel.getScreenDataArea();

    XYPlot plot = (XYPlot) advancedGraph.getPlot();
    final double lastChartX = new Double(
            plot.getDomainAxis().java2DToValue(point.getX(), plotArea, plot.getDomainAxisEdge()));

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            setCrossHair(lastChartX);
        }

    });

    for (GraphPanelListener l : listeners) {
        l.graphPanelClicked(lastChartX);
    }
}

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

/**
 * Draws the visual representation of a single data item.
 * // w ww.  j  a v  a  2s.  c  o m
 * @param g2
 *            the graphics device.
 * @param state
 *            the renderer state.
 * @param dataArea
 *            the area within which the plot 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 axis.
 * @param rangeAxis
 *            the range 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) {

    if (!getItemVisible(series, item)) {
        return;
    }

    VolumeDataset volumeDataset = (VolumeDataset) dataset;
    VolumeItem volumeItem = (VolumeItem) volumeDataset.getSeries(series).getDataItem(item);

    if (volumeItem.isSide()) {
        this.color = Color.GREEN;
    } else {
        this.color = Color.RED;
    }

    double value0;
    double value1;
    if (this.getUseYInterval()) {
        value0 = volumeDataset.getStartYValue(series, item);
        value1 = volumeDataset.getEndYValue(series, item);
    } else {
        value0 = this.getBase();
        value1 = volumeDataset.getYValue(series, item);
    }
    if (Double.isNaN(value0) || Double.isNaN(value1)) {
        return;
    }
    if (value0 <= value1) {
        if (!rangeAxis.getRange().intersects(value0, value1)) {
            return;
        }
    } else {
        if (!rangeAxis.getRange().intersects(value1, value0)) {
            return;
        }
    }

    double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea, plot.getRangeAxisEdge());
    double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea, plot.getRangeAxisEdge());
    double bottom = Math.min(translatedValue0, translatedValue1);
    double top = Math.max(translatedValue0, translatedValue1);

    double startX = volumeItem.getPeriod().getFirstMillisecond();
    if (Double.isNaN(startX)) {
        return;
    }
    double endX = volumeItem.getPeriod().getLastMillisecond();
    if (Double.isNaN(endX)) {
        return;
    }

    if (startX <= endX) {
        if (!domainAxis.getRange().intersects(startX, endX)) {
            return;
        }
    } else {
        if (!domainAxis.getRange().intersects(endX, startX)) {
            return;
        }
    }

    // is there an alignment adjustment to be made?
    if (this.getBarAlignmentFactor() >= 0.0 && this.getBarAlignmentFactor() <= 1.0) {
        double x = volumeDataset.getXValue(series, item);
        double interval = endX - startX;
        startX = x - interval * this.getBarAlignmentFactor();
        endX = startX + interval;
    }

    RectangleEdge location = plot.getDomainAxisEdge();
    double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, location);
    double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, location);

    double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));

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

    if (getMargin() > 0.0) {
        double cut = translatedWidth * getMargin();
        translatedWidth = translatedWidth - cut;
    }

    Rectangle2D bar = null;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        // clip left and right bounds to data area
        bottom = Math.max(bottom, dataArea.getMinX());
        top = Math.min(top, dataArea.getMaxX());
        bar = new Rectangle2D.Double(bottom, xx, top - bottom, translatedWidth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        // clip top and bottom bounds to data area
        bottom = Math.max(bottom, dataArea.getMinY());
        top = Math.min(top, dataArea.getMaxY());
        bar = new Rectangle2D.Double(xx - (translatedWidth / 2), bottom, translatedWidth, top - bottom);
    }

    boolean positive = (value1 > 0.0);
    boolean inverted = rangeAxis.isInverted();
    RectangleEdge barBase;
    if (orientation == PlotOrientation.HORIZONTAL) {
        if (positive && inverted || !positive && !inverted) {
            barBase = RectangleEdge.RIGHT;
        } else {
            barBase = RectangleEdge.LEFT;
        }
    } else {
        if (positive && !inverted || !positive && inverted) {
            barBase = RectangleEdge.BOTTOM;
        } else {
            barBase = RectangleEdge.TOP;
        }
    }
    if (getShadowsVisible()) {
        this.getBarPainter().paintBarShadow(g2, this, series, item, bar, barBase, !this.getUseYInterval());
    }
    this.getBarPainter().paintBar(g2, this, series, item, bar, barBase);

    if (isItemLabelVisible(series, item)) {
        XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
        drawItemLabel(g2, dataset, series, item, plot, generator, bar, value1 < 0.0);
    }

    // update the cross hair point
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, location);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1,
            plot.getOrientation());

    EntityCollection entities = state.getEntityCollection();
    // 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(bar, dataset, series, item, tip, null);

        entities.add(entity);
    }
}

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

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2  the graphics device./*from   w  w w . ja  v a  2s. com*/
 * @param state  the renderer state.
 * @param dataArea  the area within which the plot 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 axis.
 * @param rangeAxis  the range 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.
 */
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 (!getItemVisible(series, item)) {
        return;
    }
    IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;

    double value0;
    double value1;
    if (this.useYInterval) {
        value0 = intervalDataset.getStartYValue(series, item);
        value1 = intervalDataset.getEndYValue(series, item);
    } else {
        value0 = this.base;
        value1 = intervalDataset.getYValue(series, item);
    }
    if (Double.isNaN(value0) || Double.isNaN(value1)) {
        return;
    }
    if (value0 <= value1) {
        if (!rangeAxis.getRange().intersects(value0, value1)) {
            return;
        }
    } else {
        if (!rangeAxis.getRange().intersects(value1, value0)) {
            return;
        }
    }

    double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea, plot.getRangeAxisEdge());
    double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea, plot.getRangeAxisEdge());
    double bottom = Math.min(translatedValue0, translatedValue1);
    double top = Math.max(translatedValue0, translatedValue1);

    double startX = intervalDataset.getStartXValue(series, item);
    if (Double.isNaN(startX)) {
        return;
    }
    double endX = intervalDataset.getEndXValue(series, item);
    if (Double.isNaN(endX)) {
        return;
    }
    if (startX <= endX) {
        if (!domainAxis.getRange().intersects(startX, endX)) {
            return;
        }
    } else {
        if (!domainAxis.getRange().intersects(endX, startX)) {
            return;
        }
    }

    RectangleEdge location = plot.getDomainAxisEdge();
    double translatedStartX1 = domainAxis.valueToJava2D(startX, dataArea, location);
    double translatedEndX1 = domainAxis.valueToJava2D(endX, dataArea, location);

    double translatedWidth = Math.max(1, Math.abs(translatedEndX1 - translatedStartX1));

    double translatedStartX = translatedStartX1 - translatedWidth / 2;

    double translatedEndX = translatedEndX1 - translatedWidth / 2;
    double left = Math.min(translatedStartX, translatedEndX);
    if (getMargin() > 0.0) {
        double cut = translatedWidth * getMargin();
        translatedWidth = translatedWidth - cut;
        left = left + cut / 2;
    }

    Rectangle2D bar = null;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        // clip left and right bounds to data area
        bottom = Math.max(bottom, dataArea.getMinX());
        top = Math.min(top, dataArea.getMaxX());
        bar = new Rectangle2D.Double(bottom, left, top - bottom, translatedWidth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        // clip top and bottom bounds to data area
        bottom = Math.max(bottom, dataArea.getMinY());
        top = Math.min(top, dataArea.getMaxY());
        bar = new Rectangle2D.Double(left, bottom, translatedWidth, top - bottom);
    }

    Paint itemPaint = getItemPaint(series, item);
    if (getGradientPaintTransformer() != null && itemPaint instanceof GradientPaint) {
        GradientPaint gp = (GradientPaint) itemPaint;
        itemPaint = getGradientPaintTransformer().transform(gp, bar);
    }
    g2.setPaint(itemPaint);
    g2.fill(bar);
    if (isDrawBarOutline() && Math.abs(translatedEndX - translatedStartX) > 3) {
        Stroke stroke = getItemOutlineStroke(series, item);
        Paint paint = getItemOutlinePaint(series, item);
        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(bar);
        }
    }

    if (isItemLabelVisible(series, item)) {
        XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
        drawItemLabel(g2, dataset, series, item, plot, generator, bar, value1 < 0.0);
    }

    // update the crosshair point
    double x1 = (startX + endX) / 2.0;
    double y1 = dataset.getYValue(series, item);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, location);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1,
            plot.getOrientation());

    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addEntity(entities, bar, dataset, series, item, 0.0, 0.0);
    }

}

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

/**    
* Draws the visual representation of a single data item.    
*    //from w  ww.  ja  v  a2  s. c o  m
* @param g2  the graphics device.    
* @param state  the renderer state.    
* @param dataArea  the area within which the plot 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 axis.    
* @param rangeAxis  the range 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.    
*/
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 (!getItemVisible(series, item)) {
        return;
    }
    IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;

    double value0;
    double value1;
    if (this.useYInterval) {
        value0 = intervalDataset.getStartYValue(series, item);
        value1 = intervalDataset.getEndYValue(series, item);
    } else {
        value0 = this.base;
        value1 = intervalDataset.getYValue(series, item);
    }
    if (Double.isNaN(value0) || Double.isNaN(value1)) {
        return;
    }
    if (value0 <= value1) {
        if (!rangeAxis.getRange().intersects(value0, value1)) {
            return;
        }
    } else {
        if (!rangeAxis.getRange().intersects(value1, value0)) {
            return;
        }
    }

    double translatedValue0 = rangeAxis.valueToJava2D(value0, dataArea, plot.getRangeAxisEdge());
    double translatedValue1 = rangeAxis.valueToJava2D(value1, dataArea, plot.getRangeAxisEdge());
    double bottom = Math.min(translatedValue0, translatedValue1);
    double top = Math.max(translatedValue0, translatedValue1);

    double startX = intervalDataset.getStartXValue(series, item);
    if (Double.isNaN(startX)) {
        return;
    }
    double endX = intervalDataset.getEndXValue(series, item);
    if (Double.isNaN(endX)) {
        return;
    }
    if (startX <= endX) {
        if (!domainAxis.getRange().intersects(startX, endX)) {
            return;
        }
    } else {
        if (!domainAxis.getRange().intersects(endX, startX)) {
            return;
        }
    }

    RectangleEdge location = plot.getDomainAxisEdge();
    double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, location);
    double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, location);

    double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));

    double left = Math.min(translatedStartX, translatedEndX);
    if (getMargin() > 0.0) {
        double cut = translatedWidth * getMargin();
        translatedWidth = translatedWidth - cut;
        left = left + cut / 2;
    }

    Rectangle2D bar = null;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation == PlotOrientation.HORIZONTAL) {
        // clip left and right bounds to data area    
        bottom = Math.max(bottom, dataArea.getMinX());
        top = Math.min(top, dataArea.getMaxX());
        bar = new Rectangle2D.Double(bottom, left, top - bottom, translatedWidth);
    } else if (orientation == PlotOrientation.VERTICAL) {
        // clip top and bottom bounds to data area    
        bottom = Math.max(bottom, dataArea.getMinY());
        top = Math.min(top, dataArea.getMaxY());
        bar = new Rectangle2D.Double(left, bottom, translatedWidth, top - bottom);
    }

    /* Paint itemPaint = getItemPaint(series, item);    
     if (getGradientPaintTransformer()     
           != null && itemPaint instanceof GradientPaint) {    
       GradientPaint gp = (GradientPaint) itemPaint;    
       itemPaint = getGradientPaintTransformer().transform(gp, bar);    
     }    
             
    g2.setPaint(itemPaint);  
    */
    if (dataset.getYValue(series, item) >= 0) {
        g2.setPaint(Color.red);
    } else {
        g2.setPaint(Color.green);
    }
    g2.fill(bar);
    if (isDrawBarOutline() && Math.abs(translatedEndX - translatedStartX) > 3) {
        Stroke stroke = getItemOutlineStroke(series, item);
        Paint paint = getItemOutlinePaint(series, item);

        if (stroke != null && paint != null) {
            g2.setStroke(stroke);
            g2.setPaint(paint);
            g2.draw(bar);
        }
    }

    if (isItemLabelVisible(series, item)) {
        XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
        drawItemLabel(g2, dataset, series, item, plot, generator, bar, value1 < 0.0);
    }

    // update the crosshair point    
    double x1 = (startX + endX) / 2.0;
    double y1 = dataset.getYValue(series, item);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, location);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1,
            plot.getOrientation());

    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addEntity(entities, bar, dataset, series, item, 0.0, 0.0);
    }

}

From source file:edu.dlnu.liuwenpeng.render.XYLineAndShapeRenderer.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) {

    ls.add(dataset.getYValue(0, item));/*from w  ww  .j  av a 2s .  co  m*/

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

    // first pass draws the background (lines, for instance)    
    if (isLinePass(pass)) {
        if (item == 0) {
            if (this.drawSeriesLineAsPath) {
                State s = (State) state;
                s.seriesPath.reset();
                s.lastPointGood = false;
            }
        }

        if (getItemLineVisible(series, item)) {
            if (this.drawSeriesLineAsPath) {
                drawPrimaryLineAsPath(state, g2, plot, dataset, pass, series, item, domainAxis, rangeAxis,
                        dataArea);
            } else {
                drawPrimaryLine(state, g2, plot, dataset, pass, series, item, domainAxis, rangeAxis, dataArea);
            }
        }
    }
    // second pass adds shapes where the items are ..    
    else if (isItemPass(pass)) {

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

        drawSecondaryPass(g2, plot, dataset, pass, series, item, domainAxis, dataArea, rangeAxis,
                crosshairState, entities);
    }
    if (i == dataset.getItemCount(0) - 1) {
        Double average = GetAverage.getAverage(ls);
        double averangeline = rangeAxis.valueToJava2D(average, dataArea, plot.getRangeAxisEdge());

        double x = rangeAxis.valueToJava2D(dataset.getItemCount(0), dataArea, plot.getDomainAxisEdge());
        Line2D line2d = new Line2D.Double(0, averangeline, x, averangeline);
        g2.setPaint(Color.white);
        g2.draw(line2d);
        ls.clear();
        i = 0;
    }
    i++;
}

From source file:com.peterbochs.instrument.InstrumentPanel.java

@Override
public void chartMouseClicked(ChartMouseEvent event) {
    try {//from  w  ww  .j a  v a 2s  . c  o m
        // System.out.println(event.getTrigger().getX());
        JFreeChart chart = event.getChart();
        XYPlot xyplot = chart.getXYPlot();
        MyXYBlockRenderer renderer = (MyXYBlockRenderer) xyplot.getRenderer();

        XYZDataset dataset = (XYZDataset) xyplot.getDataset();
        XYItemEntity entity = (XYItemEntity) event.getEntity();
        int series = entity.getSeriesIndex();
        int item = entity.getItem();

        int i = event.getTrigger().getX();
        int j = event.getTrigger().getY();
        Point2D point2d = jMemoryChartPanel.translateScreenToJava2D(new Point(i, j));
        ChartRenderingInfo chartrenderinginfo = jMemoryChartPanel.getChartRenderingInfo();
        Rectangle2D rectangle2d = chartrenderinginfo.getPlotInfo().getDataArea();
        double x = xyplot.getDomainAxis().java2DToValue(point2d.getX(), rectangle2d,
                xyplot.getDomainAxisEdge());
        double y = xyplot.getRangeAxis().java2DToValue(point2d.getY(), rectangle2d, xyplot.getRangeAxisEdge());
        int realX = (int) Math.round(x);
        int realY = (int) Math.round(y);
        renderer.setSelectedXY(realX, realY);
        long blockSize = CommonLib.convertFilesize((String) jBlockSizeComboBox.getSelectedItem());
        long columnCount = Data.getColumnCount(
                CommonLib.convertFilesize((String) jFromComboBox.getSelectedItem()),
                CommonLib.convertFilesize((String) jToComboBox.getSelectedItem()), blockSize);
        Long address = ((realY * columnCount) + realX) * blockSize;

        updateHotestTable(address, blockSize);

        this.jAddressLabel.setText("Address=0x" + Long.toHexString(address));
        this.jRWCountLabel.setText("R/W count=" + (int) dataset.getZValue(series, item));
    } catch (Exception ex) {

    }
}

From source file:com.stableapps.anglewraparounddemo.AngleWrapDemoMain.java

/**
 * Creates a sample chart.// ww w . java  2s .  co m
 *
 * @return a sample chart.
 */
private JFreeChart createChart() {
    final XYDataset direction = createAngleDataset(600);
    final JFreeChart chart = ChartFactory.createTimeSeriesChart("Time", "Date", "Direction", direction, true,
            true, false);

    final XYPlot plot = chart.getXYPlot();
    plot.getDomainAxis().setLowerMargin(0.0);
    plot.getDomainAxis().setUpperMargin(0.0);

    // configure the range axis to provide a fix set of TickUnits depending on size of chart
    NumberAxis rangeAxis = new NumberAxis() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public NumberTickUnit getTickUnit() {
            NumberTickUnit tickUnit = super.getTickUnit();
            if (tickUnit.getSize() < 15) {
                return tickUnit;
            } else if (tickUnit.getSize() < 45) {
                return new NumberTickUnit(45);
            } else if (tickUnit.getSize() < 90) {
                return new NumberTickUnit(90);
            } else if (tickUnit.getSize() < 180) {
                return new NumberTickUnit(180);
            } else {
                return new NumberTickUnit(360);
            }
        }

    };
    rangeAxis.setAutoRangeIncludesZero(false);
    plot.setRangeAxis(rangeAxis);

    final OverflowCondition overflowCondition = new OverflowCondition() {
        @Override
        public boolean isOverflow(double y0, double x0, double y1, double x1) {
            return Math.abs(y1 - y0) > 180;
        }
    };
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false) {
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        double min = 0;
        double max = 360;
        LinearInterpolator interpolator = new LinearInterpolator();

        @Override
        protected void drawPrimaryLine(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset,
                int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis,
                Rectangle2D dataArea) {
            if (item == 0) {
                return;
            }

            // get the data point...
            double x1 = dataset.getXValue(series, item);
            double y1 = dataset.getYValue(series, item);
            if (Double.isNaN(y1) || Double.isNaN(x1)) {
                return;
            }

            double x0 = dataset.getXValue(series, item - 1);
            double y0 = dataset.getYValue(series, item - 1);
            if (Double.isNaN(y0) || Double.isNaN(x0)) {
                return;
            }

            if (overflowCondition.isOverflow(y0, x0, y1, x1)) {
                boolean overflowAtMax = y1 < y0;
                if (overflowAtMax) {
                    LinearFunction lf = interpolator.interpolate(new double[] { y0, y1 + (max - min) },
                            new double[] { x0, x1 });
                    double xmid = lf.value(max);
                    drawPrimaryLine(state, g2, plot, x0, y0, xmid, max, pass, series, item, domainAxis,
                            rangeAxis, dataArea);
                    drawPrimaryLine(state, g2, plot, xmid, min, x1, y1, pass, series, item, domainAxis,
                            rangeAxis, dataArea);
                } else {
                    LinearFunction lf = interpolator.interpolate(new double[] { y1 - (max - min), y0 },
                            new double[] { x1, x0 });
                    double xmid = lf.value(min);
                    drawPrimaryLine(state, g2, plot, x0, y0, xmid, min, pass, series, item, domainAxis,
                            rangeAxis, dataArea);
                    drawPrimaryLine(state, g2, plot, xmid, max, x1, y1, pass, series, item, domainAxis,
                            rangeAxis, dataArea);
                }
            } else {
                drawPrimaryLine(state, g2, plot, x0, y0, x1, y1, pass, series, item, domainAxis, rangeAxis,
                        dataArea);
            }

        }

        private void drawPrimaryLine(XYItemRendererState state, Graphics2D g2, XYPlot plot, double x0,
                double y0, double x1, double y1, int pass, int series, int item, ValueAxis domainAxis,
                ValueAxis rangeAxis, Rectangle2D dataArea) {
            RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
            RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
            double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
            double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);
            double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
            double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
            // only draw if we have good values
            if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1)
                    || Double.isNaN(transY1)) {
                return;
            }
            PlotOrientation orientation = plot.getOrientation();
            boolean visible;
            if (orientation == PlotOrientation.HORIZONTAL) {
                state.workingLine.setLine(transY0, transX0, transY1, transX1);
            } else if (orientation == PlotOrientation.VERTICAL) {
                state.workingLine.setLine(transX0, transY0, transX1, transY1);
            }
            visible = LineUtilities.clipLine(state.workingLine, dataArea);
            if (visible) {
                drawFirstPassShape(g2, pass, series, item, state.workingLine);
            }
        }

        @Override
        protected void drawPrimaryLineAsPath(XYItemRendererState state, Graphics2D g2, XYPlot plot,
                XYDataset dataset, int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis,
                Rectangle2D dataArea) {

            // get the data point...
            State s = (State) state;
            try {
                double x1 = dataset.getXValue(series, item);
                double y1 = dataset.getYValue(series, item);
                if (Double.isNaN(x1) && Double.isNaN(y1)) {
                    s.setLastPointGood(false);
                    return;
                }

                if (!s.isLastPointGood()) {
                    ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1);
                    s.seriesPath.moveTo(xy.getLeft(), xy.getRight());
                    s.setLastPointGood(true);
                    return;
                }

                double x0 = dataset.getXValue(series, item - 1);
                double y0 = dataset.getYValue(series, item - 1);
                if (overflowCondition.isOverflow(y0, x0, y1, x1)) {
                    boolean overflowAtMax = y1 < y0;
                    if (overflowAtMax) {
                        LinearFunction lf = interpolator.interpolate(new double[] { y0, y1 + (max - min) },
                                new double[] { x0, x1 });
                        double xmid = lf.value(max);
                        ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid,
                                max);
                        s.seriesPath.lineTo(xy.getLeft(), xy.getRight());
                        xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid, min);
                        s.seriesPath.moveTo(xy.getLeft(), xy.getRight());
                        xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1);
                        s.seriesPath.lineTo(xy.getLeft(), xy.getRight());
                    } else {
                        LinearFunction lf = interpolator.interpolate(new double[] { y1 - (max - min), y0 },
                                new double[] { x1, x0 });
                        double xmid = lf.value(min);
                        ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid,
                                min);
                        s.seriesPath.lineTo(xy.getLeft(), xy.getRight());
                        xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid, max);
                        s.seriesPath.moveTo(xy.getLeft(), xy.getRight());
                        xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1);
                        s.seriesPath.lineTo(xy.getLeft(), xy.getRight());
                    }
                } else {
                    ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1);
                    s.seriesPath.lineTo(xy.getLeft(), xy.getRight());
                }

                s.setLastPointGood(true);
            } finally {
                // if this is the last item, draw the path ...
                if (item == s.getLastItemIndex()) {
                    // draw path
                    drawFirstPassShape(g2, pass, series, item, s.seriesPath);
                }

            }
        }

        private ImmutablePair<Float, Float> translate(XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis,
                Rectangle2D dataArea, double x, double y) {
            RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
            RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
            double transX1 = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
            double transY1 = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
            // update path to reflect latest point
            float xtrans = (float) transX1;
            float ytrans = (float) transY1;
            PlotOrientation orientation = plot.getOrientation();
            if (orientation == PlotOrientation.HORIZONTAL) {
                xtrans = (float) transY1;
                ytrans = (float) transX1;
            }
            return new ImmutablePair<>(xtrans, ytrans);
        }
    };
    renderer.setDrawSeriesLineAsPath(true);
    plot.setRenderer(0, renderer);

    return chart;
}