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

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

Introduction

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

Prototype

public XYDataset getDataset(int index) 

Source Link

Document

Returns the dataset with the specified index, or null if there is no dataset with that index.

Usage

From source file:de.bund.bfr.knime.chart.ChartUtils.java

public static void addDataSetToPlot(XYPlot plot, XYDataset dataSet, XYItemRenderer renderer) {
    int i = plot.getDataset(0) == null ? 0 : plot.getDatasetCount();

    plot.setDataset(i, dataSet);/*from  ww  w .ja  va2 s.  c o m*/
    plot.setRenderer(i, renderer);
}

From source file:de.mpg.mpi_inf.bioinf.netanalyzer.dec.TwoCoefsDecorator.java

/**
 * Gets the index of the first dataset containing the specified series.
 * /*from w ww. java2  s  . co  m*/
 * @param aPlot Plot to be searched for the dataset of the fitted function.
 * @param aSeriesName Name of series the dataset should contain.
 * @return Index of the dataset which contains series named <code>aSeriesName</code>,
 *         <code>-1</code> if such dataset does not exist.
 */
protected static int getDatasetIndex(XYPlot aPlot, String aSeriesName) {
    final int datasetCount = aPlot.getDatasetCount();
    for (int i = 0; i < datasetCount; ++i) {
        if (aSeriesName.equals(aPlot.getDataset(i).getSeriesKey(0))) {
            return i;
        }
    }
    return -1;
}

From source file:ucar.unidata.idv.control.chart.HistogramWrapper.java

/**
 * Plot the displayed {@link DataChoice}.
 * /*from w  w  w .  j av a 2  s.  c  o m*/
 * @param histoWrapper Cannot be {@code null}.
 */
public static void plotHistogram(HistogramWrapper histoWrapper) {
    XYPlot p = histoWrapper.plot;
    List<DataChoiceWrapper> dcWrappers = histoWrapper.getDataChoiceWrappers();

    try {
        for (int dataSetIdx = 0; dataSetIdx < p.getDatasetCount(); dataSetIdx++) {
            MyHistogramDataset dataset = (MyHistogramDataset) p.getDataset(dataSetIdx);
            dataset.removeAllSeries();
        }

        Hashtable props = new Hashtable();
        props.put(TrackDataSource.PROP_TRACKTYPE, TrackDataSource.ID_TIMETRACE);

        for (int paramIdx = 0; paramIdx < dcWrappers.size(); paramIdx++) {
            DataChoiceWrapper wrapper = dcWrappers.get(paramIdx);

            DataChoice dataChoice = wrapper.getDataChoice();
            FlatField data = histoWrapper.getFlatField((FieldImpl) dataChoice.getData(null, props));
            Unit unit = ucar.visad.Util.getDefaultRangeUnits((FlatField) data)[0];
            double[][] samples = data.getValues(false);
            double[] actualValues = histoWrapper.filterData(samples[0],
                    histoWrapper.getTimeValues(samples, data))[0];
            NumberAxis domainAxis = new NumberAxis(wrapper.getLabel(unit));

            XYItemRenderer renderer;
            if (histoWrapper.stacked) {
                renderer = new StackedXYBarRenderer();
            } else {
                renderer = new XYBarRenderer();
            }
            p.setRenderer(paramIdx, renderer);
            Color c = wrapper.getColor(paramIdx);
            domainAxis.setLabelPaint(c);
            renderer.setSeriesPaint(0, c);

            MyHistogramDataset dataset = new MyHistogramDataset();
            dataset.setType(HistogramType.FREQUENCY);
            dataset.addSeries(dataChoice.getName() + " [" + unit + "]", actualValues, histoWrapper.bins);
            p.setDomainAxis(paramIdx, domainAxis, false);
            p.mapDatasetToDomainAxis(paramIdx, paramIdx);
            p.setDataset(paramIdx, dataset);
        }
    } catch (VisADException | RemoteException e) {
        LogUtil.logException("Error creating data set", e);
    }
}

From source file:org.gephi.ui.utils.ChartsUtils.java

/**
 * Modify a scatter plot to show linear regression or not.
 * @param scatterPlot Scatter plot to modify
 * @param enabled Indicates if linear regression has to be shown
 *///w  w  w .  j  a  va2 s  .c o  m
public static void setScatterPlotLinearRegressionEnabled(final JFreeChart scatterPlot, final boolean enabled) {
    XYPlot plot = (XYPlot) scatterPlot.getPlot();
    if (enabled) {
        StandardXYItemRenderer regressionRenderer = new StandardXYItemRenderer();
        regressionRenderer.setBaseSeriesVisibleInLegend(false);
        plot.setDataset(1, regress((XYSeriesCollection) plot.getDataset(0)));
        plot.setRenderer(1, regressionRenderer);
    } else {
        plot.setDataset(1, null);//Remove linear regression
    }
}

From source file:playground.benjamin.scenarios.zurich.analysis.charts.BkChartWriter.java

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {/* w ww .  ja v a 2 s  .co  m*/
        BufferedWriter writer = IOUtils.getBufferedWriter(filename);
        try { /*read "try" as if (plot instanceof XYPlot)*/
            XYPlot xy = chart.getXYPlot();
            String yAxisLabel = xy.getRangeAxis().getLabel();

            String xAxisLabel = "";
            if (xy.getDomainAxis() != null) {
                xAxisLabel = xy.getDomainAxis().getLabel();
            }
            String header = xAxisLabel + "\t " + yAxisLabel;
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < xy.getDatasetCount(); i++) {
                XYDataset xyds = xy.getDataset(i);
                for (int seriesIndex = 0; seriesIndex < xyds.getSeriesCount(); seriesIndex++) {
                    writer.newLine();
                    writer.write("Series " + "'" + xyds.getSeriesKey(seriesIndex).toString());
                    writer.write("'");
                    writer.newLine();
                    int items = xyds.getItemCount(seriesIndex);
                    for (int itemsIndex = 0; itemsIndex < items; itemsIndex++) {
                        Number xValue = xyds.getX(seriesIndex, itemsIndex);
                        Number yValue = xyds.getY(seriesIndex, itemsIndex);
                        writer.write(xValue.toString());
                        writer.write("\t");
                        writer.write(yValue.toString());
                        writer.newLine();
                    }
                }
            }
            System.out.println("Table written to : " + filename + "\n"
                    + "==================================================");

        } catch (ClassCastException e) { //else instanceof CategoryPlot
            log.info("caught class cast exception, trying to write CategoryPlot");
            CategoryPlot cp = chart.getCategoryPlot();
            String header = "CategoryRowKey \t CategoryColumnKey \t CategoryRowIndex \t CategoryColumnIndex \t Value";
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < cp.getDatasetCount(); i++) {
                CategoryDataset cpds = cp.getDataset(i);
                for (int rowIndex = 0; rowIndex < cpds.getRowCount(); rowIndex++) {
                    for (int columnIndex = 0; columnIndex < cpds.getColumnCount(); columnIndex++) {
                        Number value = cpds.getValue(rowIndex, columnIndex);
                        writer.write(cpds.getRowKey(rowIndex).toString());
                        writer.write("\t");
                        writer.write(cpds.getColumnKey(columnIndex).toString());
                        writer.write("\t");
                        writer.write(Integer.toString(rowIndex));
                        writer.write("\t");
                        writer.write(Integer.toString(columnIndex));
                        writer.write("\t");
                        writer.write(value.toString());
                        writer.newLine();
                    }
                }
            }

        }
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

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

@Nonnull
public static List<XYDataset> asDatasetList(@Nonnull final XYPlot plot) {
    return new AbstractList<XYDataset>() {

        @Override//from   w w  w. j a va  2 s  .  c o m
        public XYDataset get(int index) {
            return plot.getDataset(index);
        }

        @Override
        public int size() {
            return plot.getDatasetCount();
        }
    };
}

From source file:playground.dgrether.analysis.charts.utils.DgChartWriter.java

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {/*from   w w w .  j  a v a 2 s.c  om*/
        BufferedWriter writer = IOUtils.getBufferedWriter(filename);
        try { /*read "try" as if (plot instanceof XYPlot)*/
            XYPlot xy = chart.getXYPlot();
            String yAxisLabel = xy.getRangeAxis().getLabel();

            String xAxisLabel = "";
            if (xy.getDomainAxis() != null) {
                xAxisLabel = xy.getDomainAxis().getLabel();
            }
            String header = "#" + xAxisLabel + "\t " + yAxisLabel;
            writer.write(header);
            writer.newLine();
            //write the header
            writer.write("#");
            for (int i = 0; i < xy.getDatasetCount(); i++) {
                XYDataset xyds = xy.getDataset(i);
                int seriesIndex = 0;
                int maxItems = 0;
                int seriesCount = xyds.getSeriesCount();
                while (seriesIndex < seriesCount) {
                    writer.write("Series " + xyds.getSeriesKey(seriesIndex).toString());
                    if (seriesIndex < seriesCount - 1) {
                        writer.write("\t \t");
                    }
                    if (xyds.getItemCount(seriesIndex) > maxItems) {
                        maxItems = xyds.getItemCount(seriesIndex);
                    }
                    seriesIndex++;
                }
                writer.newLine();

                //write the data
                Number xValue, yValue = null;
                for (int itemsIndex = 0; itemsIndex < maxItems; itemsIndex++) {
                    for (int seriesIdx = 0; seriesIdx < seriesCount; seriesIdx++) {
                        if (seriesIdx < xyds.getSeriesCount() && itemsIndex < xyds.getItemCount(seriesIdx)) {
                            xValue = xyds.getX(seriesIdx, itemsIndex);
                            yValue = xyds.getY(seriesIdx, itemsIndex);
                            if (xValue != null && yValue != null) {
                                writer.write(xValue.toString());
                                writer.write("\t");
                                writer.write(yValue.toString());
                                if (seriesIdx < seriesCount - 1) {
                                    writer.write("\t");
                                }
                            }
                        }
                    }
                    writer.newLine();
                }
            }
        } catch (ClassCastException e) { //else instanceof CategoryPlot
            log.info("Due to a caught class cast exception, it should be a CategoryPlot");
            CategoryPlot cp = chart.getCategoryPlot();
            String header = "# CategoryRowKey \t CategoryColumnKey \t CategoryRowIndex \t CategoryColumnIndex \t Value";
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < cp.getDatasetCount(); i++) {
                CategoryDataset cpds = cp.getDataset(i);
                for (int rowIndex = 0; rowIndex < cpds.getRowCount(); rowIndex++) {
                    for (int columnIndex = 0; columnIndex < cpds.getColumnCount(); columnIndex++) {
                        Number value = cpds.getValue(rowIndex, columnIndex);
                        writer.write(cpds.getRowKey(rowIndex).toString());
                        writer.write("\t");
                        writer.write(cpds.getColumnKey(columnIndex).toString());
                        writer.write("\t");
                        writer.write(Integer.toString(rowIndex));
                        writer.write("\t");
                        writer.write(Integer.toString(columnIndex));
                        writer.write("\t");
                        writer.write(value.toString());
                        writer.newLine();
                    }
                }
            }

        }
        writer.close();
        log.info("Chart data written to: " + filename);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:gda.plots.SimpleXYBarRenderer.java

/**
 * Creates a SimpleLegendItem for a SimpleXYSeries in the dataset.
 * //from   w  w  w .j a v a2  s  .  c  om
 * @param datasetIndex
 *            which dataset (left or right effectively)
 * @param series
 *            which data series
 * @return the created legend
 */
@Override
public LegendItem getLegendItem(int datasetIndex, int series) {
    LegendItem result = null;

    XYPlot plot = getPlot();
    if (plot != null) {
        SimpleXYSeriesCollection dataset = (SimpleXYSeriesCollection) plot.getDataset(datasetIndex);
        if (dataset != null) {
            SimpleXYSeries sxys = (SimpleXYSeries) dataset.getSeries(series);
            if (sxys.isVisible() && sxys.isVisibleInLegend()) {
                result = new LegendItem(sxys.getName(), sxys.getName(), null, null, sxys.isDrawMarkers(),
                        sxys.getSymbol(), sxys.getFilled(), sxys.getSymbolPaint(), !sxys.getFilled(),
                        sxys.getSymbolPaint(), new BasicStroke(), sxys.isDrawLines(),
                        new Line2D.Double(-8.0, 0.0, 8.0, 0.0), sxys.getStroke(), sxys.getPaint());
            }
        }
    }
    return result;

}

From source file:org.kalypso.ogc.sensor.diagview.jfreechart.XYCurveRenderer.java

/**
 * @see org.jfree.chart.renderer.xy.StandardXYItemRenderer#getLegendItem(int, int)
 *///  w  ww . j a  v  a  2 s.c  om
@Override
public LegendItem getLegendItem(final int datasetIndex, final int series) {
    final XYPlot plot = getPlot();
    if (plot == null)
        return null;

    final XYDataset dataset = plot.getDataset(datasetIndex);
    if (dataset == null)
        return null;

    if (dataset instanceof CurveDataset && ((CurveDataset) dataset).hideLegend(series))
        return null;

    return super.getLegendItem(datasetIndex, series);
}

From source file:com.trivadis.loganalysis.ui.ChartPanel.java

/**
 * This method first removes the change listener, otherwise the change
 * listener will act on a already disposed widget.
 * /* ww w .  j av  a 2 s .  c om*/
 * @param index
 * @param plot
 */
private void removeDataset(final int index, final XYPlot plot) {
    final XYDataset existing = plot.getDataset(index);
    if (existing != null)
        existing.removeChangeListener(plot);
    plot.setDataset(index, null);
}