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:ec.util.chart.swing.Charts.java

/**
 * Finds the index of the nearest dataitem of a series to the left of the
 * point we clicked on the chart by using dichotomy.
 *
 * @param chartX Position of the click on the domain axis
 * @param begin Lower bound of current interval
 * @param end Upper bound of current interval
 * @param series Index of series in dataset
 * @param dataset Data used by the chart
 * @return Index of dataitem in the series
 *//*from  w ww  .  j  a v a 2s.co  m*/
public static int getNearestLeftPoint(double chartX, int begin, int end, int series,
        @Nonnull XYDataset dataset) {
    // Index of center point
    int mid = begin + (end - begin) / 2;

    // Click is totally on the left side of chart panel
    if (mid == 0) {
        return 0;
    }

    if (mid < dataset.getItemCount(series) - 1) {
        // Get positions on the domain axis
        double left = dataset.getXValue(series, mid);
        double right = dataset.getXValue(series, mid + 1);

        if (left <= chartX && right >= chartX) // We've found our target
        {
            return mid;
        } else if (left <= chartX && right <= chartX) // Our target is on the
        // right side from mid
        {
            return getNearestLeftPoint(chartX, mid + 1, end, series, dataset);
        } else // Our target is on the left side from mid
        {
            return getNearestLeftPoint(chartX, begin, mid, series, dataset);
        }
    } else // Click is totally on the right side of chart panel
    {
        return dataset.getItemCount(series) - 1;
    }
}

From source file:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Adds a Gaussian curve to the plot/*from w  ww.ja  v a2 s . c  o m*/
 * 
 * @param plot
 * @param data the data
 * @param series the series index
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return
 */
public static double[] addGaussianFit(XYPlot plot, XYDataset data, int series, double gMin, double gMax,
        int sigDigits, boolean annotations) {
    double[] fit = gaussianFit(data, series, gMin, gMax);
    double minval = data.getX(series, 0).doubleValue();
    double maxval = data.getX(series, data.getItemCount(series) - 1).doubleValue();
    return addGaussianFit(plot, fit, minval, maxval, gMin, gMax, sigDigits, annotations);
}

From source file:ec.util.chart.swing.Charts.java

@Nullable
public static LegendItemEntity getSeriesForPoint(@Nonnull Point pt, @Nonnull ChartPanel cp) {

    final double chartX;
    final double chartY;
    final Rectangle2D plotArea;
    final XYPlot plot;
    {/* w  ww  .j  ava 2  s  . com*/
        // Let's find the X and Y values of the clicked point
        Point2D p = cp.translateScreenToJava2D(pt);
        chartX = p.getX();
        chartY = p.getY();
        // Let's find plotArea and plot
        XYPlot tmpPlot = cp.getChart().getXYPlot();
        PlotRenderingInfo plotInfo = cp.getChartRenderingInfo().getPlotInfo();
        if (tmpPlot instanceof CombinedDomainXYPlot) {
            int subplotIndex = plotInfo.getSubplotIndex(p);
            if (subplotIndex == -1) {
                return null;
            }
            plotArea = plotInfo.getSubplotInfo(subplotIndex).getDataArea();
            plot = ((CombinedDomainXYPlot) tmpPlot).findSubplot(plotInfo, p);
        } else {
            plotArea = plotInfo.getDataArea();
            plot = tmpPlot;
        }
    }

    // Let's avoid unnecessary computation
    final ValueAxis domainAxis = plot.getDomainAxis();
    final ValueAxis rangeAxis = plot.getRangeAxis();
    final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    final double x = domainAxis.java2DToValue(chartX, plotArea, domainAxisEdge);

    final double sensitivity = TOL;
    double distanceClickSeries = TOL + 1;

    Entry<XYDataset, Comparable> result = null;

    // For each series in each datasets
    for (XYDataset dataset : asDatasetList(plot)) {
        for (int series = 0; series < dataset.getSeriesCount(); series++) {
            // Index of the closest data item of the current series just left to the click
            int lp = getNearestLeftPoint(x, 0, dataset.getItemCount(series) - 1, series, dataset);

            try {
                // X and Y values of data items to the left and to the right
                double leftX = dataset.getXValue(series, lp);
                double leftY = dataset.getYValue(series, lp);
                double rightX = dataset.getXValue(series, lp + 1);
                double rightY = dataset.getYValue(series, lp + 1);

                double lx = domainAxis.valueToJava2D(leftX, plotArea, domainAxisEdge);
                double ly = rangeAxis.valueToJava2D(leftY, plotArea, rangeAxisEdge);
                double rx = domainAxis.valueToJava2D(rightX, plotArea, domainAxisEdge);
                double ry = rangeAxis.valueToJava2D(rightY, plotArea, rangeAxisEdge);

                // Distance to left point
                double distL = Point2D.distance(lx, ly, chartX, chartY);
                // Distance to right point
                double distR = Point2D.distance(rx, ry, chartX, chartY);
                // Average of both distances
                double distLRavg = (distL + distR) / 2d;
                // Distance to the segment between L and R
                //double distSeg = Line2D.ptSegDist(leftX, leftY, rightX, rightY, chartX, chartY);
                double distSeg = ptSegDist(lx, ly, rx, ry, chartX, chartY);

                // With a line renderer, this is probably a bit of overkill as
                // distSeg would be enough, but it becomes more reliable to check all these
                // if using splines
                double tmp = Math.min(Math.min(distSeg, Math.min(distL, distR)), distLRavg);

                // Are we closer than the previous series?
                if (tmp < sensitivity && tmp < distanceClickSeries) {
                    distanceClickSeries = tmp;
                    result = new SimpleEntry<>(dataset, dataset.getSeriesKey(series));
                }
            } catch (Exception ex) {
                /*
                 * An exception might happen when some series have less data
                 * than others, catching the the exception here will simply rule
                 * them out from the detection on this click
                 */
            }
        }
    }

    return result != null ? createFakeLegendItemEntity(result.getKey(), result.getValue()) : null;
}

From source file:cgpanalyser.gui.chart.ChartCreator.java

/**
 * Sets value of crosshair to first item in chart.
 *
 * @param jfreechart/*  w  w  w.  j ava  2 s. com*/
 */
private void setCrosshairDefaultValue(XYPlot plot, XYDataset dataset) {
    if (dataset.getItemCount(0) != 0) {
        plot.setDomainCrosshairValue(dataset.getXValue(0, 0));
        plot.setRangeCrosshairValue(dataset.getYValue(0, 0));
    }
}

From source file:edu.psu.citeseerx.misc.charts.CiteChartBuilderJFree.java

protected JFreeChart buildChart(Document doc) throws SQLException {

    Long clusterid = doc.getClusterID();
    if (clusterid == null) {
        return null;
    }/* ww  w.j  a  va 2 s.co m*/

    java.util.List<ThinDoc> citingDocs = citedao.getCitingDocuments(clusterid, 0, MAX_CITING);
    XYDataset dataset = collectData(citingDocs);
    if (dataset.getItemCount(0) <= 1) {
        return null;
    }

    XYBarDataset ivl_dataset = new XYBarDataset(dataset, 15.0);
    JFreeChart chart = ChartFactory.createXYBarChart(null, "Year", true, "Citation Count", ivl_dataset,
            PlotOrientation.VERTICAL, false, false, false);
    chart.setBackgroundPaint(Color.WHITE);

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.WHITE);
    plot.setRangeGridlinePaint(Color.WHITE);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

    XYItemRenderer r = plot.getRenderer();

    NumberAxis axis = (NumberAxis) plot.getDomainAxis();
    axis.setNumberFormatOverride(NumberFormat.getIntegerInstance());
    //axis.setTickUnit(new DateTickUnit(DateTickUnit.YEAR, -1));
    //axis.setDateFormatOverride(new SimpleDateFormat("yyyy"));
    return chart;

}

From source file:com.od.jtimeseries.ui.visualizer.chart.creator.EfficientXYLineAndShapeRenderer.java

private boolean isLastItem(XYDataset dataset, int series, int item) {
    return item == dataset.getItemCount(series) - 1;
}

From source file:slash.navigation.converter.gui.profileview.LazyToolTipChartPanel.java

protected String getTooltipAtPoint(Point point) {
    XYPlot plot = (XYPlot) getChart().getPlot();
    PlotRenderingInfo info = getChartRenderingInfo().getPlotInfo();
    double x0 = point.getX();
    double x1 = x0 - 2 * getScaleX();
    double x2 = x0 + 4 * getScaleX();

    ValueAxis domainAxis = plot.getDomainAxis();
    Rectangle2D screenArea = scale(info.getDataArea());
    double tx1 = domainAxis.java2DToValue(x1, screenArea, BOTTOM);
    double tx2 = domainAxis.java2DToValue(x2, screenArea, BOTTOM);

    for (int datasetIndex = 0; datasetIndex < plot.getDatasetCount(); datasetIndex++) {
        XYDataset dataset = plot.getDataset(datasetIndex);
        for (int seriesIndex = 0; seriesIndex < dataset.getSeriesCount(); seriesIndex++) {
            int itemCount = dataset.getItemCount(seriesIndex);
            for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
                double xValue = dataset.getXValue(seriesIndex, itemIndex);
                if (tx1 < xValue && xValue < tx2)
                    return toolTipGenerator.generateToolTip(dataset, seriesIndex, itemIndex);
            }/*from w  ww.j  a v  a2  s  .  co m*/
        }
    }
    return null;
}

From source file:com.bdb.weather.display.day.ItemRenderer.java

/**
 * Plots the data for a given series./*from   w  w w.jav a  2s. co m*/
 * 
 * @param g2  the drawing surface.
 * @param dataArea  the data area.
 * @param info  collects plot rendering info.
 * @param plot  the plot.
 * @param dataset  the dataset.
 * @param seriesIndex  the series index.
 */
@Override
public void drawSeries(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, PolarPlot plot,
        XYDataset dataset, int seriesIndex) {
    Shape point = new Rectangle2D.Double(-2, -2, 4, 4);

    int numPoints = dataset.getItemCount(seriesIndex);

    g2.setPaint(lookupSeriesPaint(seriesIndex));
    g2.setStroke(lookupSeriesStroke(seriesIndex));

    for (int i = 0; i < numPoints; i++) {
        double theta = dataset.getXValue(seriesIndex, i);
        double radius = dataset.getYValue(seriesIndex, i);

        Point p = plot.translateToJava2D(theta, radius, plot.getAxis(), dataArea);

        Shape shape = ShapeUtilities.createTranslatedShape(point, p.getX(), p.getY());

        g2.fill(shape);
    }
}

From source file:service.chart.TimeSeriesChart.java

/**
 * Utworz nowa przestrzen z wykresem szeregu czasowego (z wyroznionymi punktami dla danych pochodzacych 
 * z predykcji) i dodaj ja do glownego panelu
 * /*from  w  w  w  . j ava  2s .  c  o m*/
 * @param timeSeriesList Lista szeregow czasowych
 * @param numOfDataPoints Liczba punktow czasowych dla ktorych zostala wykonana predykcja
 */
public void createChartPanel(List<TimeSeries> timeSeriesList, int numOfDataPoints) {
    XYDataset dataset = createDataset(timeSeriesList);

    if (numOfDataPoints > 0) {
        firstColumnID = dataset.getItemCount(0) - numOfDataPoints;
        lastColumnID = dataset.getItemCount(0) - 1;
        renderer = new MyRenderer(true, false);
    } else {
        renderer = new XYLineAndShapeRenderer(true, false);
    }

    JFreeChart chart = createChart(dataset);

    ChartPanel panel = new ChartPanel(chart);
    panel.setFillZoomRectangle(true);
    panel.setMouseWheelEnabled(true);

    this.removeAll();
    this.add(panel);
}

From source file:scheduler.benchmarker.manager.CreateSimpleSplineChart.java

public ChartPanel createChartPanel() {
    XYDataset dataset = createDataset();
    NumberAxis numberaxis = new NumberAxis("EMAILS");
    numberaxis.setAutoRangeIncludesZero(true);
    numberaxis.setRange(0, dataset.getItemCount(1));
    numberaxis.setVisible(false);/*w  w w  .  j a va  2 s. com*/
    NumberAxis numberaxis1 = new NumberAxis("TIME CONSUMED");
    numberaxis1.setAutoRangeIncludesZero(false);
    XYSplineRenderer xysplinerenderer = new XYSplineRenderer();
    XYPlot xyplot = new XYPlot(dataset, numberaxis, numberaxis1, xysplinerenderer);
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setRangeGridlinePaint(Color.white);
    //xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
    JFreeChart jfreechart = new JFreeChart("PLAN VALUES FOR '" + sName + "' SCHEDULER",
            new Font(Font.SANS_SERIF, Font.PLAIN, 11), xyplot, true);
    chartPanel = new ChartPanel(jfreechart, true);

    //Creating listener
    chartPanel.addChartMouseListener(new ChartMouseListener() {
        @Override
        public void chartMouseClicked(ChartMouseEvent e) {
            ChartEntity entity = e.getEntity();
            if (entity != null && (entity instanceof XYItemEntity)) {
                XYItemEntity item = (XYItemEntity) entity;
                String chartTitle = "RULE ARRANGEMENT INFORMATION FOR EMAIL \""
                        + dataSource.get(item.getItem()).getEmailName() + "\"";
                createSubChart(
                        new CreateStackedBarChart3D(dataSource.get(item.getItem()), pluginColor, chartTitle)
                                .createChartPanel());
            }
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent e) {
            //DO NOTHING
        }

    });
    return chartPanel;
}