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() 

Source Link

Document

Returns the primary dataset for the plot.

Usage

From source file:net.sf.fspdfs.chartthemes.spring.AegeanChartTheme.java

/**
 *
 */// ww w  . j  av a2 s. c om
protected JFreeChart createCandlestickChart() throws JRException {
    JFreeChart jfreeChart = super.createCandlestickChart();
    XYPlot xyPlot = (XYPlot) jfreeChart.getPlot();
    CandlestickRenderer renderer = (CandlestickRenderer) xyPlot.getRenderer();
    DefaultHighLowDataset dataset = (DefaultHighLowDataset) xyPlot.getDataset();
    List seriesPaints = (List) getDefaultValue(defaultChartPropertiesMap, ChartThemesConstants.SERIES_COLORS);

    for (int i = 0; i < dataset.getSeriesCount(); i++) {

        renderer.setSeriesFillPaint(i, (Paint) seriesPaints.get(i));
        renderer.setSeriesPaint(i, Color.DARK_GRAY);
    }
    return jfreeChart;
}

From source file:net.sf.jasperreports.chartthemes.spring.AegeanChartTheme.java

@Override
protected JFreeChart createBubbleChart() throws JRException {
    JFreeChart jfreeChart = super.createBubbleChart();

    XYPlot xyPlot = (XYPlot) jfreeChart.getPlot();
    XYBubbleRenderer bubbleRenderer = (XYBubbleRenderer) xyPlot.getRenderer();
    XYDataset xyDataset = xyPlot.getDataset();
    if (xyDataset != null) {
        for (int i = 0; i < xyDataset.getSeriesCount(); i++) {
            bubbleRenderer.setSeriesOutlinePaint(i, ChartThemesConstants.TRANSPARENT_PAINT);
        }/*from   w w  w  . j  av  a 2s .c o  m*/
    }
    return jfreeChart;
}

From source file:net.sf.jasperreports.chartthemes.spring.AegeanChartTheme.java

@Override
protected JFreeChart createCandlestickChart() throws JRException {
    JFreeChart jfreeChart = super.createCandlestickChart();
    XYPlot xyPlot = (XYPlot) jfreeChart.getPlot();
    CandlestickRenderer renderer = (CandlestickRenderer) xyPlot.getRenderer();
    DefaultHighLowDataset dataset = (DefaultHighLowDataset) xyPlot.getDataset();
    @SuppressWarnings("unchecked")
    List<Paint> seriesPaints = (List<Paint>) getDefaultValue(defaultChartPropertiesMap,
            ChartThemesConstants.SERIES_COLORS);

    for (int i = 0; i < dataset.getSeriesCount(); i++) {

        renderer.setSeriesFillPaint(i, seriesPaints.get(i));
        renderer.setSeriesPaint(i, Color.DARK_GRAY);
    }/*from  w  w  w .  ja va2 s  . c  o m*/
    return jfreeChart;
}

From source file:net.sf.maltcms.chromaui.chromatogram1Dviewer.ui.panel.Chromatogram1DHeatmapViewerPanel.java

public void setPlot(final XYPlot plot) {
    removeAxisListener();//from w w w . j a  v  a2  s  .  c o  m
    ADataset1D<?, IScan> dataset = null;
    if (plot.getDataset() instanceof ADataset1D) {
        dataset = (ADataset1D<?, IScan>) plot.getDataset();
    } else {
        throw new IllegalArgumentException("Requires a plot with ADataset1D!");
    }
    this.plot = plot;
    if (this.selectionOverlay != null) {
        this.content.remove(selectionOverlay);
        this.selectionOverlay = null;
    }
    if (selectionOverlay == null) {
        selectionOverlay = new SelectionOverlay(Color.LIGHT_GRAY, Color.RED, 2.5f, 2.5f, 0.66f);
        chartPanel.addOverlay(selectionOverlay);
        selectionOverlay.addChangeListener(chartPanel);
        this.content.add(selectionOverlay);
    } else {
        for (ISelection selection : selectionOverlay.getMouseClickSelection()) {
            selection.setDataset(dataset);
        }

        ISelection selection = selectionOverlay.getMouseHoverSelection();
        if (selection != null) {
            selection.setDataset(dataset);
        }
    }
    if (selectionHandler == null) {
        selectionHandler = new InstanceContentSelectionHandler(this.content, selectionOverlay,
                InstanceContentSelectionHandler.Mode.valueOf((String) modeSpinner.getValue()), dataset, 1);
    } else {
        selectionHandler.setDataset(dataset);
    }
    if (mouseSelectionHandler == null) {
        mouseSelectionHandler = new XYMouseSelectionHandler<>(dataset);
        mouseSelectionHandler.addSelectionChangeListener(selectionOverlay);
        mouseSelectionHandler.addSelectionChangeListener(selectionHandler);
        chartPanel.addChartMouseListener(mouseSelectionHandler);
    } else {
        mouseSelectionHandler.setDataset(dataset);
    }

    XYItemRenderer xyir = plot.getRenderer();
    if (xyir instanceof XYBlockRenderer) {
        XYBlockRenderer xybr = (XYBlockRenderer) xyir;
        boxWidthSpinner.setValue(xybr.getBlockWidth());
        boxHeightSpinner.setValue(xybr.getBlockHeight());
    }

    AxisChangeListener listener = selectionOverlay;
    ValueAxis domain = this.plot.getDomainAxis();
    ValueAxis range = this.plot.getRangeAxis();
    if (domain != null) {
        domain.addChangeListener(listener);
    }
    if (range != null) {
        range.addChangeListener(listener);
    }

    this.plot.setNoDataMessage("Loading Data...");
    chart = new JFreeChart(this.plot);
    chartPanel.setChart(chart);
    dmkl = new DomainMarkerKeyListener(this.plot);
    dmkl.setPlot(this.plot);
    chartPanel.addKeyListener(dmkl);
    addAxisListener();
    //add available chart overlays
    ArrayList<? extends Overlay> overlays = new ArrayList<>(getLookup().lookupAll(Overlay.class));
    Collections.sort(overlays, new Comparator<Overlay>() {
        @Override
        public int compare(Overlay o1, Overlay o2) {
            if (o1 instanceof ChartOverlay && o2 instanceof ChartOverlay) {
                ChartOverlay co1 = (ChartOverlay) o1;
                ChartOverlay co2 = (ChartOverlay) o2;
                return Integer.compare(co1.getLayerPosition(), co2.getLayerPosition());
            } else {
                return 0;
            }
        }
    });
    for (Overlay overlay : overlays) {
        if (!(overlay instanceof SelectionOverlay)) {
            chartPanel.removeOverlay(overlay);
            if (overlay instanceof AxisChangeListener) {
                AxisChangeListener axisChangeListener = (AxisChangeListener) overlay;
                if (domain != null) {
                    domain.addChangeListener(axisChangeListener);
                }
                if (range != null) {
                    range.addChangeListener(axisChangeListener);
                }
            }
            if (overlay instanceof ISelectionChangeListener) {
                ISelectionChangeListener isl = (ISelectionChangeListener) overlay;
                mouseSelectionHandler.addSelectionChangeListener(isl);
                mouseSelectionHandler.addSelectionChangeListener(selectionHandler);
                selectionOverlay.addChangeListener(chartPanel);
            }
            chartPanel.addOverlay(overlay);
            overlay.addChangeListener(chartPanel);
        }
    }
    //add selection overlay last
    chartPanel.removeOverlay(selectionOverlay);
    chartPanel.addOverlay(selectionOverlay);
    setViewPortAround((double) jSlider2.getValue());
    double rangeValue = chartPanel.getChart().getXYPlot().getDomainAxis().getAutoRangeMinimumSize();
    ((NumberAxis) chartPanel.getChart().getXYPlot().getDomainAxis()).setAutoRange(false);
    //        chartPanel.getChart().getXYPlot().getDomainAxis().setFixedDimension(rangeValue / 10.0d);
    ((NumberAxis) chartPanel.getChart().getXYPlot().getDomainAxis()).setAutoRangeIncludesZero(false);
    ((NumberAxis) chartPanel.getChart().getXYPlot().getDomainAxis()).setRangeType(RangeType.POSITIVE);
}

From source file:msi.gama.outputs.layers.charts.ChartJFreeChartOutputScatter.java

@Override
protected void createNewSerie(final IScope scope, final String serieid) {

    final ChartDataSeries dataserie = chartdataset.getDataSeries(scope, serieid);
    final XYIntervalSeries serie = new XYIntervalSeries(dataserie.getSerieLegend(scope), false, true);
    final XYPlot plot = (XYPlot) this.chart.getPlot();

    final XYIntervalSeriesCollection firstdataset = (XYIntervalSeriesCollection) plot.getDataset();

    if (!IdPosition.containsKey(serieid)) {

        if (firstdataset.getSeriesCount() == 0) {
            firstdataset.addSeries(serie);
            plot.setDataset(0, firstdataset);

        } else {// ww  w  .  j  a  v  a  2 s . co  m

            final XYIntervalSeriesCollection newdataset = new XYIntervalSeriesCollection();
            newdataset.addSeries(serie);
            jfreedataset.add(newdataset);
            plot.setDataset(jfreedataset.size() - 1, newdataset);

        }
        plot.setRenderer(jfreedataset.size() - 1, (XYItemRenderer) getOrCreateRenderer(scope, serieid));
        IdPosition.put(serieid, jfreedataset.size() - 1);
        // DEBUG.LOG("new serie"+serieid+" at
        // "+IdPosition.get(serieid)+" fdsize "+plot.getSeriesCount()+" jfds
        // "+jfreedataset.size()+" datasc "+plot.getDatasetCount());
        // TODO Auto-generated method stub

    }
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private boolean updateMainTraceInfo(Point2D point) {
    if (point == null) {
        return false;
    }/*from ww w . j  ava  2 s.  com*/

    final ChartPanel chartPanel = this.chartJDialog.getChartPanel();
    // Top most plot.
    final XYPlot plot = this.chartJDialog.getPlot();

    if (plot.getDataset() instanceof org.jfree.data.xy.DefaultHighLowDataset) {
        return this._updateMainTraceInfoForCandlestick(point);
    }

    final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) plot.getDataset();
    // 0 are the main chart. 1, 2, 3... are TA.
    final TimeSeries timeSeries = timeSeriesCollection.getSeries(0);

    // I also not sure why. This is what are being done in Mouse Listener Demo 4.
    //
    // Don't use it. It will cause us to lose precision.
    //final Point2D p2 = chartPanel.translateScreenToJava2D((Point)point);

    /* Try to get correct main chart area. */
    final Rectangle2D _plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0)
            .getDataArea();

    final ValueAxis domainAxis = plot.getDomainAxis();
    final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    final ValueAxis rangeAxis = plot.getRangeAxis();
    final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    // Don't use it. It will cause us to lose precision.
    //final double coordinateX = domainAxis.java2DToValue(p2.getX(), _plotArea,
    //        domainAxisEdge);
    final double coordinateX = domainAxis.java2DToValue(point.getX(), _plotArea, domainAxisEdge);
    //double coordinateY = rangeAxis.java2DToValue(mousePoint2.getY(), plotArea,
    //        rangeAxisEdge);

    int low = 0;
    int high = timeSeries.getItemCount() - 1;
    Date date = new Date((long) coordinateX);
    final long time = date.getTime();
    long bestDistance = Long.MAX_VALUE;
    int bestMid = 0;

    while (low <= high) {
        int mid = (low + high) >>> 1;

        final TimeSeriesDataItem timeSeriesDataItem = timeSeries.getDataItem(mid);
        final Day day = (Day) timeSeriesDataItem.getPeriod();
        final long search = day.getFirstMillisecond();
        final long cmp = search - time;

        if (cmp < 0) {
            low = mid + 1;
        } else if (cmp > 0) {
            high = mid - 1;
        } else {
            bestDistance = 0;
            bestMid = mid;
            break;
        }

        final long abs_cmp = Math.abs(cmp);
        if (abs_cmp < bestDistance) {
            bestDistance = abs_cmp;
            bestMid = mid;
        }
    }

    final TimeSeriesDataItem timeSeriesDataItem = timeSeries.getDataItem(bestMid);
    final double xValue = timeSeriesDataItem.getPeriod().getFirstMillisecond();
    final double yValue = timeSeriesDataItem.getValue().doubleValue();
    final double xJava2D = domainAxis.valueToJava2D(xValue, _plotArea, domainAxisEdge);
    final double yJava2D = rangeAxis.valueToJava2D(yValue, _plotArea, rangeAxisEdge);

    final int tmpIndex = bestMid;
    // translateJava2DToScreen will internally convert Point2D.Double to Point.
    final Point2D tmpPoint = chartPanel.translateJava2DToScreen(new Point2D.Double(xJava2D, yJava2D));
    this.mainDrawArea.setRect(_plotArea);

    if (this.mainDrawArea.contains(tmpPoint)) {
        // 0 indicates main plot.
        this.mainTraceInfo = TraceInfo.newInstance(tmpPoint, 0, 0, tmpIndex);
        return true;
    }
    return false;
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private void updateIndicatorTraceInfos(int mainPointIndex) {
    this.indicatorTraceInfos.clear();
    if (this.mainTraceInfo == null) {
        return;/* w w  w  .ja v a 2  s.co m*/
    }

    final ChartPanel chartPanel = this.chartJDialog.getChartPanel();

    Day day = null;

    final XYDataset xyDataset = this.chartJDialog.getPlot().getDataset();
    if (xyDataset instanceof TimeSeriesCollection) {
        // Get the date.
        day = (Day) ((TimeSeriesCollection) xyDataset).getSeries(0).getDataItem(mainPointIndex).getPeriod();

        // 0 means main plot.
        final XYPlot plot = this.chartJDialog.getPlot();
        final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) plot.getDataset();
        // Start with 1. We are not interested in main series.
        for (int j = 1, size = timeSeriesCollection.getSeriesCount(); j < size; j++) {
            final TimeSeries timeSeries = timeSeriesCollection.getSeries(j);
            /* Time consuming. */
            final int dataIndex = getDataIndex(timeSeries, day);

            if (dataIndex < 0) {
                continue;
            }

            final Point2D point = this.getPoint(0, j, dataIndex);
            final String name = this.getLegendName(0, j);
            final Number value = this.getValue(0, j, dataIndex);

            if (point == null || name == null || value == null) {
                continue;
            }

            // We will never draw ball for SMA, EMA...
            this.indicatorTraceInfos.add(TraceInfo.newInstance(null, 0, j, dataIndex));
        }
    } else {
        final Date date = ((org.jfree.data.xy.DefaultHighLowDataset) xyDataset).getXDate(0, mainPointIndex);
        // OK to do so? Is "day" only used to compare day, excluding time information?
        // Will day 13th September 2009, 1:00pm same as another day 13th September 2009, 3:00pm?
        day = new Day(date);

        // 0 means main plot.
        final XYPlot plot = this.chartJDialog.getPlot();
        final int count = plot.getDatasetCount();
        for (int i = 1; i < count; i++) {
            final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) plot.getDataset(i);

            /* Not ready. */
            if (timeSeriesCollection == null) {
                continue;
            }

            final TimeSeries timeSeries = timeSeriesCollection.getSeries(0);
            /* Time consuming. */
            final int dataIndex = getDataIndex(timeSeries, day);

            if (dataIndex < 0) {
                continue;
            }

            final Point2D point = this.getPoint(0, i, dataIndex);
            final String name = this.getLegendName(0, i);
            final Number value = this.getValue(0, i, dataIndex);

            if (point == null || name == null || value == null) {
                continue;
            }

            // We will never draw ball for SMA, EMA...
            this.indicatorTraceInfos.add(TraceInfo.newInstance(null, 0, i, dataIndex));
        }
    }

    // Begin with 1. 0 is main plot.
    for (int i = 1, size = this.chartJDialog.getPlotSize(); i < size; i++) {
        final XYPlot plot = this.chartJDialog.getPlot(i);
        final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) plot.getDataset();
        // So far, for subplot, each of them only have 1 series.
        assert (1 == timeSeriesCollection.getSeriesCount());
        for (int j = 0, size2 = timeSeriesCollection.getSeriesCount(); j < size2; j++) {
            final TimeSeries timeSeries = timeSeriesCollection.getSeries(j);
            /* Time consuming. */
            final int dataIndex = getDataIndex(timeSeries, day);

            if (dataIndex < 0) {
                continue;
            }

            final Point2D point = this.getPoint(i, j, dataIndex);
            final String name = this.getLegendName(i, j);
            final Number value = this.getValue(i, j, dataIndex);

            if (point == null || name == null || value == null) {
                continue;
            }

            final Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(i)
                    .getDataArea();
            if (plotArea.contains(point)) {
                this.indicatorTraceInfos.add(TraceInfo.newInstance(point, i, j, dataIndex));
            } else {
                this.indicatorTraceInfos.add(TraceInfo.newInstance(null, i, j, dataIndex));
            }
        }
    }
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private boolean _updateMainTraceInfoForCandlestick(Point2D point) {
    if (point == null) {
        return false;
    }/*w  ww. j av a 2 s  .com*/

    final ChartPanel chartPanel = this.chartJDialog.getChartPanel();
    // Top most plot.
    final XYPlot plot = this.chartJDialog.getPlot();

    final org.jfree.data.xy.DefaultHighLowDataset defaultHighLowDataset = (org.jfree.data.xy.DefaultHighLowDataset) plot
            .getDataset();

    // I also not sure why. This is what are being done in Mouse Listener Demo 4.
    //
    // Don't use it. It will cause us to lose precision.
    //final Point2D p2 = chartPanel.translateScreenToJava2D((Point)point);

    /* Try to get correct main chart area. */
    final Rectangle2D _plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0)
            .getDataArea();

    final ValueAxis domainAxis = plot.getDomainAxis();
    final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    final ValueAxis rangeAxis = plot.getRangeAxis();
    final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    // Don't use it. It will cause us to lose precision.
    //final double coordinateX = domainAxis.java2DToValue(p2.getX(), _plotArea,
    //        domainAxisEdge);
    final double coordinateX = domainAxis.java2DToValue(point.getX(), _plotArea, domainAxisEdge);
    //double coordinateY = rangeAxis.java2DToValue(mousePoint2.getY(), plotArea,
    //        rangeAxisEdge);

    int low = 0;
    int high = defaultHighLowDataset.getItemCount(0) - 1;
    Date date = new Date((long) coordinateX);
    final long time = date.getTime();
    long bestDistance = Long.MAX_VALUE;
    int bestMid = 0;

    while (low <= high) {
        int mid = (low + high) >>> 1;

        final Date d = defaultHighLowDataset.getXDate(0, mid);
        final long search = d.getTime();
        final long cmp = search - time;

        if (cmp < 0) {
            low = mid + 1;
        } else if (cmp > 0) {
            high = mid - 1;
        } else {
            bestDistance = 0;
            bestMid = mid;
            break;
        }

        final long abs_cmp = Math.abs(cmp);
        if (abs_cmp < bestDistance) {
            bestDistance = abs_cmp;
            bestMid = mid;
        }
    }

    final double xValue = defaultHighLowDataset.getXDate(0, bestMid).getTime();
    final double yValue = defaultHighLowDataset.getCloseValue(0, bestMid);
    final double xJava2D = domainAxis.valueToJava2D(xValue, _plotArea, domainAxisEdge);
    final double yJava2D = rangeAxis.valueToJava2D(yValue, _plotArea, rangeAxisEdge);

    final int tmpIndex = bestMid;
    // translateJava2DToScreen will internally convert Point2D.Double to Point.
    final Point2D tmpPoint = chartPanel.translateJava2DToScreen(new Point2D.Double(xJava2D, yJava2D));
    this.mainDrawArea.setRect(_plotArea);

    if (this.mainDrawArea.contains(tmpPoint)) {
        // 0 indicates main plot.
        this.mainTraceInfo = TraceInfo.newInstance(tmpPoint, 0, 0, tmpIndex);
        return true;
    }
    return false;
}

From source file:net.bioclipse.model.ScatterPlotMouseHandler.java

public void mouseClicked(MouseEvent me) {
    Point2D p = null;/*w w w . j  a v  a2  s .c o m*/
    ChartDescriptor cd = null;
    int[] indices = null;
    JFreeChart selectedChart = null;

    ChartPanel chartPanel = getChartPanel(me);
    p = chartPanel.translateScreenToJava2D(new Point(me.getX(), me.getY()));
    selectedChart = chartPanel.getChart();

    cd = ChartUtils.getChartDescriptor(selectedChart);
    indices = cd.getSourceIndices();

    XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();

    XYItemRenderer plotRenderer = plot.getRenderer();

    if (!(plotRenderer instanceof ScatterPlotRenderer)) {
        throw new IllegalStateException(
                "Charts using ScatterPlotMouseHandler must use ScatterPlotRenderer as their renderer");
    }
    renderer = (ScatterPlotRenderer) plot.getRenderer();

    // now convert the Java2D coordinate to axis coordinates...
    Number xx = getDomainX(chartPanel, plot, p);
    Number yy = getRangeY(chartPanel, plot, p);

    //Find the selected point in the dataset
    //If shift is down, save old selections
    if (!me.isShiftDown() || currentSelection == null) {
        currentSelection = new ChartSelection();
    }

    for (int j = 0; j < plot.getDataset().getItemCount(plot.getDataset().getSeriesCount() - 1); j++) {
        for (int i = 0; i < plot.getDataset().getSeriesCount(); i++) {
            Number xK = plot.getDataset().getX(i, j);
            Number yK = plot.getDataset().getY(i, j);
            Number xKCheck = xK.doubleValue() - xx.doubleValue();
            Number yKCheck = yK.doubleValue() - yy.doubleValue();
            Number xxCheck = xKCheck.doubleValue() * xKCheck.doubleValue();
            Number yyCheck = yKCheck.doubleValue() * yKCheck.doubleValue();
            //Check distance from click and point, don't want to mark points that are too far from the click
            if (Math.sqrt(xxCheck.doubleValue()) <= 0.1 && Math.sqrt(yyCheck.doubleValue()) <= 0.1) {
                //Create a new selection
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                cp.setDataPoint(j, i);
                currentSelection.addPoint(cp);
                if (!me.isShiftDown())
                    renderer.clearMarkedPoints();
                renderer.addMarkedPoint(j, i);
                selectedChart.plotChanged(new PlotChangeEvent(plot));

            }
        }
    }
    currentSelection.setDescriptor(cd);
    ChartUtils.updateSelection(currentSelection);
}

From source file:GUI.GUIModel.java

public void PutPoint(double key, double x, double y, JPanel GraphHerePanel) {
    Component[] a = GraphHerePanel.getComponents();
    ChartPanel chartpanel = (ChartPanel) a[0];
    JFreeChart chart = chartpanel.getChart();

    XYPlot plot = (XYPlot) chart.getPlot();
    XYSeriesCollection data = (XYSeriesCollection) plot.getDataset();
    XYSeries XYseries = data.getSeries(key);
    XYseries.add(x, y);//  ww  w .jav  a2 s  .  c  o  m
    this.revalidate();
    this.repaint();

}