Example usage for org.jfree.chart.renderer.xy XYItemRendererState getLastItemIndex

List of usage examples for org.jfree.chart.renderer.xy XYItemRendererState getLastItemIndex

Introduction

In this page you can find the example usage for org.jfree.chart.renderer.xy XYItemRendererState getLastItemIndex.

Prototype

public int getLastItemIndex() 

Source Link

Document

Returns the last item index (this is updated with each call to #startSeriesPass(XYDataset,int,int,int,int,int) .

Usage

From source file:aprofplot.jfreechart.SamplingXYLineAndShapeRenderer.java

/**
 * Draws the visual representation of a series as lines.
 *
 * @param g2  the graphics device.//from   ww w . j a v a 2  s .c om
 * @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 crosshairState  crosshair information for the plot
 *                        (<code>null</code> permitted).
 * @param pass  the pass index.
 */
protected void drawVerticalSeriesLine(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea,
        PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
        int series, CrosshairState crosshairState, int pass) {

    State s = (State) state;
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double lowestVisibleX = domainAxis.getLowerBound();
    double highestVisibleX = domainAxis.getUpperBound();
    double width = dataArea.getWidth();
    double dX = (highestVisibleX - lowestVisibleX) / width * lineWidth;
    double lowestVisibleY = rangeAxis.getLowerBound();
    double highestVisibleY = rangeAxis.getUpperBound();

    double lastX = Double.NEGATIVE_INFINITY;
    double lastY = 0.0;
    double highY = 0.0;
    double lowY = 0.0;
    double openY = 0.0;
    double closeY = 0.0;
    boolean lastIntervalDone = false;
    boolean currentPointVisible = false;
    boolean lastPointVisible = false;
    boolean lastPointGood = false;
    boolean lastPointInInterval = false;
    boolean pathStarted = false;
    for (int itemIndex = state.getFirstItemIndex(); itemIndex <= state.getLastItemIndex(); itemIndex++) {
        double x = dataset.getXValue(series, itemIndex);
        double y = dataset.getYValue(series, itemIndex);
        if (!Double.isNaN(x) && !Double.isNaN(y)) {
            //System.out.println ("Breakpoint 736: pathStarted " + pathStarted);
            if (!pathStarted) {
                float startX = (float) domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
                float startY = (float) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
                s.seriesPath.moveTo(startX, startY);
                pathStarted = true;
            }
            if ((Math.abs(x - lastX) > dX)) {
                //System.out.println ("Breakpoint 744: leaving interval ");
                //in any case, add the interval that we are about to leave to the intervalPath
                float intervalPathX = 0.0f;
                float intervalPathStartY = 0.0f;
                float intervalPathEndY = 0.0f;
                float seriesPathCurrentX = 0.0f;
                float seriesPathCurrentY = 0.0f;
                float seriesPathLastX = 0.0f;
                float seriesPathLastY = 0.0f;

                //first set some variables
                intervalPathX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                intervalPathStartY = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                intervalPathEndY = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
                seriesPathCurrentX = (float) domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
                seriesPathLastX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                seriesPathCurrentY = (float) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
                seriesPathLastY = (float) rangeAxis.valueToJava2D(closeY, dataArea, yAxisLocation);
                /*if((lowY - highY) < 1){
                lastPointInInterval = false;
                }*/
                //Double[] values = new Double[]{new Double(openY), new Double(closeY), new Double(highY), new Double(lowY), new Double(lastX)};
                /*MessageFormat mf = new MessageFormat("openY {0,number, integer},"
                    + " closeY {1,number, integer}"
                    + " highY {2,number, integer}"
                    + " lowY {3,number, integer}"
                    + "lastX {4,number, integer}");*/
                //String text = mf.format(values);
                //System.out.println("Breakpoint 772"  + text);
                boolean drawInterval = false;
                if (openY >= closeY) {
                    if (highY > openY || lowY < closeY) {
                        drawInterval = true;
                    }
                }
                if (openY < closeY) {
                    if (lowY < openY || highY > closeY) {
                        drawInterval = true;
                    }
                }
                //System.out.println("Breakpoint 784, drawInterval "  + drawInterval);
                if (drawInterval) {
                    s.intervalPath.moveTo(intervalPathX, intervalPathStartY);
                    s.intervalPath.lineTo(intervalPathX, intervalPathEndY);
                }
                lastIntervalDone = true;

                //now the series path
                currentPointVisible = ((x >= lowestVisibleX) && (x <= highestVisibleX) && (y >= lowestVisibleY)
                        && (y <= highestVisibleY));
                if (!lastPointGood && !lastPointInInterval) {//last point not valid --
                    if (currentPointVisible) {//--> if the current position is visible move seriesPath cursor to the current position
                        s.seriesPath.moveTo(seriesPathCurrentX, seriesPathCurrentY);
                    }
                } else {//last point valid
                    //if the last point was visible and not part of an interval,
                    //we have already moved the seriesPath cursor to the last point, either with or without drawingh a line
                    //thus we only need to draw a line to the current position
                    if (lastPointInInterval && lastPointGood) {
                        s.seriesPath.lineTo(seriesPathLastX, seriesPathLastY);
                        s.seriesPath.lineTo(seriesPathCurrentX, seriesPathCurrentY);
                    } //if the last point was not visible or part of an interval, we have just stored the y values of the last point
                      //and not yet moved the seriesPath cursor. Thus, we need to move the cursor to the last point without drawing
                      //and draw a line to the current position.
                    else {
                        s.seriesPath.lineTo(seriesPathCurrentX, seriesPathCurrentY);
                    }
                }
                lastPointVisible = currentPointVisible;
                lastX = x;
                lastY = y;
                highY = y;
                lowY = y;
                openY = y;
                closeY = y;
                lastPointInInterval = false;
            } else {
                lastIntervalDone = false;
                lastPointInInterval = true;
                highY = Math.max(highY, y);
                lowY = Math.min(lowY, y);
                closeY = y;
            }
            lastPointGood = true;
        } else {
            lastPointGood = false;
        }
    }
    // if this is the last item, draw the path ...
    // draw path, but first check whether we need to complete an interval
    if (!lastIntervalDone) {
        if (lowY < highY) {
            float intervalX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
            float intervalStartY = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
            float intervalEndY = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
            s.intervalPath.moveTo(intervalX, intervalStartY);
            s.intervalPath.lineTo(intervalX, intervalEndY);
        }
    }
    PathIterator pi = s.seriesPath.getPathIterator(null);
    g2.setStroke(getItemStroke(series, 0));
    g2.setPaint(getItemPaint(series, 0));
    g2.draw(s.seriesPath);
    g2.draw(s.intervalPath);
}

From source file:aprofplot.jfreechart.SamplingXYLineAndShapeRenderer.java

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

    State s = (State) state;
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double lowestVisibleX = domainAxis.getLowerBound();
    double highestVisibleX = domainAxis.getUpperBound();
    double width = (orientation == PlotOrientation.HORIZONTAL) ? dataArea.getHeight() : dataArea.getWidth();
    double dX = (highestVisibleX - lowestVisibleX) / width * lineWidth;
    double lowestVisibleY = rangeAxis.getLowerBound();
    double highestVisibleY = rangeAxis.getUpperBound();

    double lastX = Double.NEGATIVE_INFINITY;
    double lastY = 0.0;
    double highY = 0.0;
    double lowY = 0.0;
    double closeY = 0.0;
    boolean lastIntervalDone = false;
    boolean currentPointVisible = false;
    boolean lastPointVisible = false;
    boolean lastPointGood = false;
    boolean lastPointInInterval = false;
    int intervalCount = 0;
    int badPoints = 0;
    for (int itemIndex = state.getFirstItemIndex(); itemIndex <= state.getLastItemIndex(); itemIndex++) {
        double x = dataset.getXValue(series, itemIndex);
        double y = dataset.getYValue(series, itemIndex);
        if (!Double.isNaN(x) && !Double.isNaN(y)) {
            if ((Math.abs(x - lastX) > dX)) {
                //System.out.println("Breakpoint 1: leaving interval");
                //in any case, add the interval that we are about to leave to the intervalPath
                float intervalStartX = 0.0f;
                float intervalEndX = 0.0f;
                float intervalStartY = 0.0f;
                float intervalEndY = 0.0f;
                float currentX = 0.0f;
                float currentY = 0.0f;
                float lastFX = 0.0f;
                float lastFY = 0.0f;

                //first set some variables
                if (orientation == PlotOrientation.VERTICAL) {
                    intervalStartX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    intervalEndX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    intervalStartY = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                    intervalEndY = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
                    currentX = (float) domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
                    lastFX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    currentY = (float) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
                    lastFY = (float) rangeAxis.valueToJava2D(closeY, dataArea, yAxisLocation);
                } else {
                    intervalStartX = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                    intervalEndX = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
                    intervalStartY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    intervalEndY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                    currentX = (float) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation);
                    lastFX = (float) rangeAxis.valueToJava2D(closeY, dataArea, yAxisLocation);
                    currentY = (float) domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
                    lastFY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                }// w  w  w.j a  va 2s .c  o  m
                if ((lowY - highY) < 1) {
                    //System.out.println("Breakpoint 2: setting lastPointInInterval");
                    lastPointInInterval = false;
                }
                //System.out.println("Breakpoint 3: lastPointInInterval: " +lastPointInInterval);
                if ((lowY < highY)) {
                    intervalCount++;
                    //System.out.println("Breakpoint 4: adding segment to interval path:" );
                    //System.out.println("xStart" + intervalStartX + ", yStart " + intervalStartY + ", xEnd " + intervalEndX + ", yEnd " + intervalEndY);
                    s.intervalPath.moveTo(intervalStartX, intervalStartY);
                    s.intervalPath.lineTo(intervalEndX, intervalEndY);
                    lastIntervalDone = true;
                }

                //now the series path
                currentPointVisible = ((x >= lowestVisibleX) && (x <= highestVisibleX) && (y >= lowestVisibleY)
                        && (y <= highestVisibleY));
                if (!lastPointGood) {//last point not valid --
                    badPoints++;
                    if (currentPointVisible) {//--> if the current position is visible move seriesPath cursor to the current position
                        s.seriesPath.moveTo(currentX, currentY);
                    }
                } else {//last point valid
                    //if the last point was visible and not part of an interval,
                    //we have already moved the seriesPath cursor to the last point, either with or without drawingh a line
                    //thus we only need to draw a line to the current position
                    if (lastPointVisible && !lastPointInInterval) {
                        s.seriesPath.lineTo(currentX, currentY);
                    } //if the last point was not visible or part of an interval, we have just stored the y values of the last point
                      //and not yet moved the seriesPath cursor. Thus, we need to move the cursor to the last point without drawing
                      //and draw a line to the current position.
                    else {
                        s.seriesPath.moveTo(lastFX, lastFY);
                        s.seriesPath.lineTo(currentX, currentY);
                    }
                }
                lastPointVisible = currentPointVisible;
                lastX = x;
                lastY = y;
                highY = y;
                lowY = y;
                closeY = y;
                lastPointInInterval = false;
            } else {
                lastIntervalDone = false;
                lastPointInInterval = true;
                highY = Math.max(highY, y);
                lowY = Math.min(lowY, y);
                closeY = y;
            }
            lastPointGood = true;
        } else {
            lastPointGood = false;
        }
    }
    // if this is the last item, draw the path ...
    // draw path, but first check whether we need to complete an interval
    if (!lastIntervalDone) {
        if (lowY < highY) {
            float intervalStartX = 0.0f;
            float intervalEndX = 0.0f;
            float intervalStartY = 0.0f;
            float intervalEndY = 0.0f;
            if (orientation == PlotOrientation.VERTICAL) {
                intervalStartX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                intervalEndX = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                intervalStartY = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                intervalEndY = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
            } else {
                intervalStartX = (float) rangeAxis.valueToJava2D(lowY, dataArea, yAxisLocation);
                intervalEndX = (float) rangeAxis.valueToJava2D(highY, dataArea, yAxisLocation);
                intervalStartY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
                intervalEndY = (float) domainAxis.valueToJava2D(lastX, dataArea, xAxisLocation);
            }
            intervalCount++;
            s.intervalPath.moveTo(intervalStartX, intervalStartY);
            s.intervalPath.lineTo(intervalEndX, intervalEndY);
        }
    }
    PathIterator pi = s.seriesPath.getPathIterator(null);
    g2.setStroke(getItemStroke(series, 0));
    g2.setPaint(getItemPaint(series, 0));
    g2.draw(s.seriesPath);
    g2.draw(s.intervalPath);
    //System.out.println("Interval count " + intervalCount);
    //System.out.println("Bad points " + badPoints);
}