Example usage for org.jfree.data.xy XYDataset getItemCount

List of usage examples for org.jfree.data.xy XYDataset getItemCount

Introduction

In this page you can find the example usage for org.jfree.data.xy XYDataset getItemCount.

Prototype

public int getItemCount(int series);

Source Link

Document

Returns the number of items in a series.

Usage

From source file:com.intel.stl.ui.common.view.ComponentFactory.java

public static JFreeChart createStepAreaChart(XYDataset dataset, XYItemLabelGenerator labelGenerator) {
    if (dataset == null) {
        throw new IllegalArgumentException("No dataset.");
    }//  w  w w .  j  av  a2  s  .  co  m

    JFreeChart jfreechart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.VERTICAL,
            false, true, false);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setBackgroundPaint(UIConstants.INTEL_BACKGROUND_GRAY);
    // xyplot.setOutlinePaint(null);
    XYStepAreaRenderer xysteparearenderer = new XYStepAreaRenderer(XYStepAreaRenderer.AREA) {

        @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) {
            setShapesVisible(item == dataset.getItemCount(series) - 1);
            super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item,
                    crosshairState, pass);
        }

    };
    xysteparearenderer.setDataBoundsIncludesVisibleSeriesOnly(false);
    xysteparearenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
    xysteparearenderer.setDefaultEntityRadius(6);
    xysteparearenderer.setShapesFilled(true);
    xyplot.setRenderer(xysteparearenderer);

    if (labelGenerator != null) {
        xysteparearenderer.setBaseItemLabelGenerator(labelGenerator);
    }
    xysteparearenderer.setSeriesPaint(0, UIConstants.INTEL_GREEN);
    xyplot.setOutlinePaint(UIConstants.INTEL_DARK_GRAY);
    xyplot.setDomainGridlinePaint(UIConstants.INTEL_DARK_GRAY);
    xyplot.setRangeGridlinePaint(UIConstants.INTEL_DARK_GRAY);

    xyplot.getDomainAxis().setVisible(false);

    NumberAxis axis = (NumberAxis) xyplot.getRangeAxis();
    axis.setRangeType(RangeType.POSITIVE);
    axis.setAxisLineVisible(false);

    return jfreechart;
}

From source file:de.hdm.uls.loadtests.ui.XYLineAndAreaRenderer.java

/**
 * Draws the visual representation of a single data item.
 *
 * @param g2  the graphics device./*from   w  w w.  ja va  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 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;
    }
    XYAreaRendererState areaState = (XYAreaRendererState) state;

    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(y1)) {
        y1 = 0.0;
    }
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, plot.getDomainAxisEdge());
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());

    // get the previous point and the next point so we can calculate a
    // "hot spot" for the area (used by the chart entity)...
    int itemCount = dataset.getItemCount(series);
    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 transX0 = domainAxis.valueToJava2D(x0, dataArea, plot.getDomainAxisEdge());
    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, plot.getRangeAxisEdge());

    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 transX2 = domainAxis.valueToJava2D(x2, dataArea, plot.getDomainAxisEdge());
    double transY2 = rangeAxis.valueToJava2D(y2, dataArea, plot.getRangeAxisEdge());

    double transZero = rangeAxis.valueToJava2D(0.0, dataArea, plot.getRangeAxisEdge());
    Polygon hotspot = null;
    if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
        hotspot = new Polygon();
        hotspot.addPoint((int) transZero, (int) ((transX0 + transX1) / 2.0));
        hotspot.addPoint((int) ((transY0 + transY1) / 2.0), (int) ((transX0 + transX1) / 2.0));
        hotspot.addPoint((int) transY1, (int) transX1);
        hotspot.addPoint((int) ((transY1 + transY2) / 2.0), (int) ((transX1 + transX2) / 2.0));
        hotspot.addPoint((int) transZero, (int) ((transX1 + transX2) / 2.0));
    } else { // vertical orientation
        hotspot = new Polygon();
        hotspot.addPoint((int) ((transX0 + transX1) / 2.0), (int) transZero);
        hotspot.addPoint((int) ((transX0 + transX1) / 2.0), (int) ((transY0 + transY1) / 2.0));
        hotspot.addPoint((int) transX1, (int) transY1);
        hotspot.addPoint((int) ((transX1 + transX2) / 2.0), (int) ((transY1 + transY2) / 2.0));
        hotspot.addPoint((int) ((transX1 + transX2) / 2.0), (int) transZero);
    }

    if (item == 0) { // create a new area polygon for the series
        areaState.area = new Polygon();
        // the first point is (x, 0)
        double zero = rangeAxis.valueToJava2D(0.0, dataArea, plot.getRangeAxisEdge());
        if (plot.getOrientation() == PlotOrientation.VERTICAL) {
            areaState.area.addPoint((int) transX1, (int) zero);
        } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
            areaState.area.addPoint((int) zero, (int) transX1);
        }
    }

    // Add each point to Area (x, y)
    if (plot.getOrientation() == PlotOrientation.VERTICAL) {
        areaState.area.addPoint((int) transX1, (int) transY1);
    } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
        areaState.area.addPoint((int) transY1, (int) transX1);
    }

    PlotOrientation orientation = plot.getOrientation();
    Paint paint = getItemPaint(series, item);
    Stroke stroke = getItemStroke(series, item);
    g2.setPaint(paint);
    g2.setStroke(stroke);

    Shape shape = null;
    if (getPlotShapes()) {
        shape = getItemShape(series, item);
        if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        } else if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        }
        g2.draw(shape);
    }

    if (getPlotLines()) {
        if (item > 0) {
            if (plot.getOrientation() == PlotOrientation.VERTICAL) {
                areaState.line.setLine(transX0, transY0, transX1, transY1);
            } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
                areaState.line.setLine(transY0, transX0, transY1, transX1);
            }
            g2.draw(areaState.line);
        }
    }

    // Check if the item is the last item for the series.
    // and number of items > 0.  We can't draw an area for a single point.
    if (getPlotArea() && item > 0 && item == (itemCount - 1)) {

        if (orientation == PlotOrientation.VERTICAL) {
            // Add the last point (x,0)
            areaState.area.addPoint((int) transX1, (int) transZero);
        } else if (orientation == PlotOrientation.HORIZONTAL) {
            // Add the last point (x,0)
            areaState.area.addPoint((int) transZero, (int) transX1);
        }

        Paint fillPaint = getItemFillPaint(series, item);
        if (fillPaint instanceof GradientPaint) {
            GradientPaint gp = (GradientPaint) fillPaint;
            GradientPaintTransformer t = new StandardGradientPaintTransformer();
            fillPaint = t.transform(gp, areaState.area.getBounds());
        }
        g2.setPaint(fillPaint);
        g2.fill(areaState.area);

        // draw an outline around the Area.
        if (isOutline()) {
            g2.setStroke(getItemOutlineStroke(series, item));
            g2.setPaint(getItemOutlinePaint(series, item));
            g2.draw(areaState.area);
        }
    }

    updateCrosshairValues(crosshairState, x1, y1, transX1, transY1, orientation);

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null && hotspot != null) {
            String tip = null;
            XYToolTipGenerator generator = getToolTipGenerator(series, item);
            if (generator != null) {
                tip = generator.generateToolTip(dataset, series, item);
            }
            String url = null;
            if (getURLGenerator() != null) {
                url = getURLGenerator().generateURL(dataset, series, item);
            }
            XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url);
            entities.add(entity);
        }
    }

}

From source file:no.met.jtimeseries.chart.ChartPlotter.java

/**
Add max min bar. Mainly used to show precipitation that has max and min values
* @param timeBase//ww w  .j  av a2  s.co m
* @param title
* @param maxMinTimeSeriesEnabler
* @param maxColor
* @param minColor 
*/
public void addMaxMinPercipitationBars(TimeBase timeBase, String title, NumberPhenomenon max,
        NumberPhenomenon min, Color maxColor, Color minColor) {

    XYDataset maxDataset = max.getTimeSeries(title, timeBase);
    XYDataset minDataset = min.getTimeSeries(title, timeBase);
    if (maxDataset.getSeriesCount() > 0 && minDataset.getSeriesCount() > 0 && maxDataset.getItemCount(0) > 0
            && minDataset.getItemCount(0) > 0) {

        double margin = 0.2;
        double maxPrecipitation = max.getMaxValue();

        addBarChart(minDataset, "min", minColor, margin, maxPrecipitation);
        showBarValuesOnBottom(plotIndex - 1, 1D);

        rangeAxisIndex--;

        addBarChart(maxDataset, "max", maxColor, margin, maxPrecipitation);
        showBarValuesOnTop(plotIndex - 1, 6D);

        plot.getRangeAxis(getRangeAxisIndex() - 1).setVisible(false);

        final Marker marker = new ValueMarker(0);
        marker.setPaint(Color.GRAY);
        marker.setStroke(new BasicStroke(1));
        plot.addRangeMarker(getRangeAxisIndex() - 1, marker, Layer.BACKGROUND);
    }
}

From source file:umontreal.iro.lecuyer.charts.EmpiricalRenderer.java

/**
 * Draws the visual representation of a single data item.
 *
 * @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 axis.// www.j a v  a 2 s .  co  m
 * @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;
    PlotOrientation orientation = plot.getOrientation();
    java.awt.Paint seriesPaint = getItemPaint(series, item);
    java.awt.Stroke seriesStroke = getItemStroke(series, item);
    g2.setPaint(seriesPaint);
    g2.setStroke(seriesStroke);
    double x0 = dataset.getXValue(series, item);
    double y0 = dataset.getYValue(series, item);
    if (java.lang.Double.isNaN(y0))
        return;
    org.jfree.ui.RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    org.jfree.ui.RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

    double x1 = 0, y1 = 0;
    if (item < dataset.getItemCount(series) - 1) {
        x1 = dataset.getXValue(series, item + 1);
        y1 = dataset.getYValue(series, item + 1);
    } else {
        x1 = dataArea.getMaxX();
        y1 = dataArea.getMaxY();
    }

    boolean useFillPaint = getUseFillPaint();
    ;
    boolean drawOutlines = getDrawOutlines();
    if (!java.lang.Double.isNaN(y0)) {
        double transX1;
        double transY1;
        if (item < dataset.getItemCount(series) - 1) {
            transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
            transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
        } else {
            transX1 = x1;
            transY1 = y1;
        }
        Line2D line = state.workingLine;
        if (orientation == PlotOrientation.HORIZONTAL) {
            line.setLine(transY0, transX0, transY0, transX1);
            g2.draw(line);
        } else if (orientation == PlotOrientation.VERTICAL) {
            line.setLine(transX0, transY0, transX1, transY0);
            g2.draw(line);
        }
    }
    if (getItemShapeVisible(series, item)) {
        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL)
            shape = ShapeUtilities.createTranslatedShape(shape, transY0, transX0);
        else if (orientation == PlotOrientation.VERTICAL)
            shape = ShapeUtilities.createTranslatedShape(shape, transX0, transY0);
        if (shape.intersects(dataArea)) {
            if (getItemShapeFilled(series, item)) {
                if (useFillPaint)
                    g2.setPaint(getItemFillPaint(series, item));
                else
                    g2.setPaint(getItemPaint(series, item));
                g2.fill(shape);
            }
            if (drawOutlines) {
                if (getUseOutlinePaint())
                    g2.setPaint(getItemOutlinePaint(series, item));
                else
                    g2.setPaint(getItemPaint(series, item));
                g2.setStroke(getItemOutlineStroke(series, item));
                g2.draw(shape);
            }
        }
    }
    if (isItemLabelVisible(series, item)) {
        double xx = transX0;
        double yy = transY0;
        if (orientation == PlotOrientation.HORIZONTAL) {
            xx = transY0;
            yy = transX0;
        }
        drawItemLabel(g2, orientation, dataset, series, item, xx, yy, y0 < 0.0D);
    }
    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x0, y0, domainAxisIndex, rangeAxisIndex, transX0, transY0,
            orientation);
    if (state.getInfo() != null) {
        EntityCollection entities = state.getEntityCollection();
        if (entities != null) {
            int r = getDefaultEntityRadius();
            java.awt.Shape shape = orientation != PlotOrientation.VERTICAL
                    ? ((java.awt.Shape) (new java.awt.geom.Rectangle2D.Double(transY0 - (double) r,
                            transX0 - (double) r, 2 * r, 2 * r)))
                    : ((java.awt.Shape) (new java.awt.geom.Rectangle2D.Double(transX0 - (double) r,
                            transY0 - (double) r, 2 * r, 2 * r)));
            if (shape != null) {
                String tip = null;
                XYToolTipGenerator generator = getToolTipGenerator(series, item);
                if (generator != null)
                    tip = generator.generateToolTip(dataset, series, item);
                String url = null;
                if (getURLGenerator() != null)
                    url = getURLGenerator().generateURL(dataset, series, item);
                XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url);
                entities.add(entity);
            }
        }
    }
}

From source file:windows.sensorWindow.java

/**
 * collects values from the selection and copies them into the templatePlot
 * object// ww w  . ja v  a  2s  .  c om
 * 
 * @param x1
 * @param x2
 * @param dataSet
 */
public static void collectPlotValues(double x1, double x2, XYDataset dataSet) {
    Comparable comparable;
    int indexOf;
    ArrayList<MeasurementEntry> entries = new ArrayList<MeasurementEntry>();
    if (dataSet != null) {
        int k = dataSet.getSeriesCount();

        for (int i = 0; i < dataSet.getSeriesCount(); i++) {
            comparable = dataSet.getSeriesKey(i);
            indexOf = dataSet.indexOf(comparable);
            for (int j = 0; j < dataSet.getItemCount(indexOf); j++) {
                long x = (long) dataSet.getXValue(indexOf, j);
                if (x >= x1) {
                    double y = dataSet.getYValue(indexOf, j);
                    // System.out.println("valueX["+j+"] = "+functions.Common.doubleToTime(x)+", y = "+y);
                    entries.add(new MeasurementEntry(x, y));
                }
                if (x >= x2) {
                    break;
                }
            }
        }
    }
    templatePlot.replacePointList(entries);
}

From source file:se.technipelago.weather.chart.Generator.java

private JFreeChart createLineChart(final String title, final String valueLabel, final XYDataset dataset) {
    final JFreeChart chart = ChartFactory.createTimeSeriesChart(title, null, valueLabel, dataset, false, false,
            false);/*  w w w.  ja v  a 2s  .  co m*/

    if (dataset.getItemCount(0) < 30) {
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        final XYPlot plot = (XYPlot) chart.getPlot();
        plot.setRenderer(renderer);
    }
    return chart;
}

From source file:org.operamasks.faces.render.graph.ChartRenderer.java

private XYSeries createCurveSeries(UICurve curve, XYDataset data, int index) {
    if (data.getItemCount(index) < 2) {
        throw new IllegalArgumentException("Not enough data");
    }// w w  w .  ja v  a 2 s . c  o m

    Key key = new Key(index, curve.getLegend());

    if (curve instanceof UIAverageLine) {
        double period = ((UIAverageLine) curve).getPeriod();
        double skip = ((UIAverageLine) curve).getSkip();
        if (data instanceof TimeSeriesCollection) {
            TimeSeries ts = ((TimeSeriesCollection) data).getSeries(index);
            return MovingAverage.createMovingAverage(ts, key, (int) period, (int) skip);
        } else {
            return MovingAverage.createMovingAverage(data, index, key, period, skip);
        }
    } else if (curve instanceof UIRegressionLine) {
        RegressionType type = ((UIRegressionLine) curve).getType();
        int samples = ((UIRegressionLine) curve).getSamples();
        if (type == null || type == RegressionType.Linear) {
            double[] p = Regression.getOLSRegression(data, index);
            Function2D f = new LineFunction2D(p[0], p[1]);
            return createFunctionSeries(data, index, key, f, samples);
        } else if (type == RegressionType.Power) {
            double[] p = Regression.getPowerRegression(data, index);
            Function2D f = new PowerFunction2D(p[0], p[1]);
            return createFunctionSeries(data, index, key, f, samples);
        } else {
            throw new IllegalArgumentException(type.toString());
        }
    } else if (curve instanceof UISpline) {
        SplineType type = ((UISpline) curve).getType();
        int samples = ((UISpline) curve).getSamples();
        int degree = ((UISpline) curve).getDegree();
        if (type == null || type == SplineType.BSpline) {
            return BSpline.createBSpline(data, index, key, samples, degree);
        } else if (type == SplineType.CubicSpline) {
            return CubicSpline.createCubicSpline(data, index, key, samples);
        }
    }

    return null;
}

From source file:org.jfree.data.general.DatasetUtilities.java

/**
 * Returns <code>true</code> if the dataset is empty (or <code>null</code>),
 * and <code>false</code> otherwise.
 *
 * @param dataset  the dataset (<code>null</code> permitted).
 *
 * @return A boolean.//from ww w.j  ava  2  s  .  c  o m
 */
public static boolean isEmptyOrNull(XYDataset dataset) {
    if (dataset != null) {
        for (int s = 0; s < dataset.getSeriesCount(); s++) {
            if (dataset.getItemCount(s) > 0) {
                return false;
            }
        }
    }
    return true;
}

From source file:de.laures.cewolf.jfree.XYSplineRenderer.java

/**
 * Draws the item (first pass). This method draws the lines
 * connecting the items. Instead of drawing separate lines,
 * a GeneralPath is constructed and drawn at the end of the series painting.
 *
 * @param g2  the graphics device./*from  w ww  .  j a v  a 2s  .  c  o m*/
 * @param state  the renderer state.
 * @param plot  the plot (can be used to obtain standard color information etc).
 * @param dataset  the dataset.
 * @param pass  the pass.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataArea  the area within which the data is being drawn.
 */
protected void drawPrimaryLineAsPath(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset,
        int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) {

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

    // get the data points
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

    // collect points
    if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) {
        ControlPoint p = new ControlPoint(
                plot.getOrientation() == PlotOrientation.HORIZONTAL ? (float) transY1 : (float) transX1,
                plot.getOrientation() == PlotOrientation.HORIZONTAL ? (float) transX1 : (float) transY1);
        if (!this.points.contains(p)) {
            this.points.add(p);
        }
    }
    if (item == dataset.getItemCount(series) - 1) {
        State s = (State) state;
        // construct path
        if (this.points.size() > 1) {
            // we need at least two points to draw something
            ControlPoint cp0 = (ControlPoint) this.points.get(0);
            s.seriesPath.moveTo(cp0.x, cp0.y);
            if (this.points.size() == 2) {
                // we need at least 3 points to spline. Draw simple line for two points
                ControlPoint cp1 = (ControlPoint) this.points.get(1);
                s.seriesPath.lineTo(cp1.x, cp1.y);
            } else {
                // construct spline
                int np = this.points.size(); // number of points
                float[] d = new float[np]; // Newton form coefficients
                float[] x = new float[np]; // x-coordinates of nodes
                float y;
                float t;
                float[] a = new float[np];
                float t1;
                float t2;
                float[] h = new float[np];

                for (int i = 0; i < np; i++) {
                    ControlPoint cpi = (ControlPoint) this.points.get(i);
                    x[i] = cpi.x;
                    d[i] = cpi.y;
                }

                for (int i = 1; i <= np - 1; i++) {
                    h[i] = x[i] - x[i - 1];
                }
                float[] sub = new float[np - 1];
                float[] diag = new float[np - 1];
                float[] sup = new float[np - 1];

                for (int i = 1; i <= np - 2; i++) {
                    diag[i] = (h[i] + h[i + 1]) / 3;
                    sup[i] = h[i + 1] / 6;
                    sub[i] = h[i] / 6;
                    a[i] = (d[i + 1] - d[i]) / h[i + 1] - (d[i] - d[i - 1]) / h[i];
                }
                solveTridiag(sub, diag, sup, a, np - 2);

                // note that a[0]=a[np-1]=0
                // draw
                s.seriesPath.moveTo(x[0], d[0]);
                for (int i = 1; i <= np - 1; i++) {
                    // loop over intervals between nodes
                    for (int j = 1; j <= this.precision; j++) {
                        t1 = (h[i] * j) / this.precision;
                        t2 = h[i] - t1;
                        y = ((-a[i - 1] / 6 * (t2 + h[i]) * t1 + d[i - 1]) * t2
                                + (-a[i] / 6 * (t1 + h[i]) * t2 + d[i]) * t1) / h[i];
                        t = x[i - 1] + t1;
                        s.seriesPath.lineTo(t, y);
                    }
                }
            }
            // draw path
            drawFirstPassShape(g2, pass, series, item, s.seriesPath);
        }

        // reset points vector
        this.points = new Vector();
    }
}

From source file:no.met.jtimeseries.chart.XYSplineRenderer.java

/**
 * Draws the item (first pass). This method draws the lines
 * connecting the items. Instead of drawing separate lines,
 * a GeneralPath is constructed and drawn at the end of
 * the series painting./*  w w w . j  a v a2 s . c  o  m*/
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param plot  the plot (can be used to obtain standard color information
 *              etc).
 * @param dataset  the dataset.
 * @param pass  the pass.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataArea  the area within which the data is being drawn.
 */
@Override
protected void drawPrimaryLineAsPath(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset,
        int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) {

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

    // get the data points
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

    // collect points
    if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) {
        ControlPoint p = new ControlPoint(
                plot.getOrientation() == PlotOrientation.HORIZONTAL ? (float) transY1 : (float) transX1,
                plot.getOrientation() == PlotOrientation.HORIZONTAL ? (float) transX1 : (float) transY1);
        if (!this.points.contains(p)) {
            this.points.add(p);
        }
    }
    if (item == dataset.getItemCount(series) - 1) {
        State s = (State) state;
        // construct path
        if (this.points.size() > 1) {
            // we need at least two points to draw something
            ControlPoint cp0 = this.points.get(0);
            s.seriesPath.moveTo(cp0.x, cp0.y);
            if (this.points.size() == 2) {
                // we need at least 3 points to spline. Draw simple line
                // for two points
                ControlPoint cp1 = this.points.get(1);
                s.seriesPath.lineTo(cp1.x, cp1.y);
            } else {
                // construct spline
                int np = this.points.size(); // number of points
                float[] d = new float[np]; // Newton form coefficients
                float[] x = new float[np]; // x-coordinates of nodes
                float y;
                float t;
                float oldy = 0;
                float oldt = 0;

                float[] a = new float[np];
                float t1;
                float t2;
                float[] h = new float[np];

                for (int i = 0; i < np; i++) {
                    ControlPoint cpi = this.points.get(i);
                    x[i] = cpi.x;
                    d[i] = cpi.y;
                }

                for (int i = 1; i <= np - 1; i++) {
                    h[i] = x[i] - x[i - 1];
                }
                float[] sub = new float[np - 1];
                float[] diag = new float[np - 1];
                float[] sup = new float[np - 1];

                for (int i = 1; i <= np - 2; i++) {
                    diag[i] = (h[i] + h[i + 1]) / 3;
                    sup[i] = h[i + 1] / 6;
                    sub[i] = h[i] / 6;
                    a[i] = (d[i + 1] - d[i]) / h[i + 1] - (d[i] - d[i - 1]) / h[i];
                }
                solveTridiag(sub, diag, sup, a, np - 2);

                // note that a[0]=a[np-1]=0
                // draw
                oldt = x[0];
                oldy = d[0];
                s.seriesPath.moveTo(oldt, oldy);
                for (int i = 1; i <= np - 1; i++) {
                    // loop over intervals between nodes
                    for (int j = 1; j <= this.precision; j++) {
                        t1 = (h[i] * j) / this.precision;
                        t2 = h[i] - t1;
                        y = ((-a[i - 1] / 6 * (t2 + h[i]) * t1 + d[i - 1]) * t2
                                + (-a[i] / 6 * (t1 + h[i]) * t2 + d[i]) * t1) / h[i];
                        t = x[i - 1] + t1;
                        s.seriesPath.lineTo(t, y);
                        oldt = t;
                        oldy = y;
                    }
                }
            }
            // draw path
            drawFirstPassShape(g2, pass, series, item, s.seriesPath);
        }

        // reset points vector
        this.points = new Vector<ControlPoint>();
    }
}