Example usage for org.jfree.chart ChartPanel getChart

List of usage examples for org.jfree.chart ChartPanel getChart

Introduction

In this page you can find the example usage for org.jfree.chart ChartPanel getChart.

Prototype

public JFreeChart getChart() 

Source Link

Document

Returns the chart contained in the panel.

Usage

From source file:org.jfree.chart.demo.selection.SelectionDemo8.java

/**
 * A demonstration application showing how to create a simple time series
 * chart. This example uses monthly data.
 *
 * @param title  the frame title./*  w ww  .j av a2 s .  c o m*/
 */
public SelectionDemo8(String title) {
    super(title);
    ChartPanel chartPanel = (ChartPanel) createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    chartPanel.setRangeZoomable(false);

    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    this.dataset = (TimeSeriesCollection) plot.getDataset();
    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    split.add(chartPanel);

    this.model = new DefaultTableModel(new String[] { "Series:", "Item:", "Period:", "Value:" }, 0);
    this.table = new JTable(this.model);
    TableColumnModel tcm = this.table.getColumnModel();
    tcm.getColumn(3).setCellRenderer(new NumberCellRenderer());
    JPanel p = new JPanel(new BorderLayout());
    JScrollPane scroller = new JScrollPane(this.table);
    p.add(scroller);
    p.setBorder(BorderFactory.createCompoundBorder(new TitledBorder("Selected Items: "),
            new EmptyBorder(4, 4, 4, 4)));
    split.add(p);
    setContentPane(split);

}

From source file:org.knime.knip.core.ui.imgviewer.panels.HistogramBC.java

/**
 * Refreshes the JFreeChart based on newBundle.
 *
 * @param newBundle//from   www .j ava2  s .c om
 */
public void refreshChart(final HistogramBundle newBundle) {
    final ChartPanel newChartPanel = makeChartPanel(newBundle);
    final JFreeChart chart = newChartPanel.getChart();
    chartPanel.setChart(chart);
    chartPanel.setDomainZoomable(false);
    chartPanel.setRangeZoomable(false);
}

From source file:net.sf.maltcms.chromaui.charts.overlay.Peak2DOverlay.java

/**
 *
 * @param g2/*from   ww  w.j  a va  2 s . co  m*/
 * @param chartPanel
 */
@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    if (isVisible()) {
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        if (plot.getDataset() instanceof Chromatogram2DDataset) {
            dataset = (ADataset2D<IChromatogram2D, IScan2D>) plot.getDataset();
        } else {
            throw new IllegalArgumentException("Unsupported dataset type: " + plot.getDataset().getClass());
        }
        if (shapes == null) {
            shapes = renderer.generatePeak2DShapes(peakAnnotations, dataset, non2DPeaks);
        } else {
            XYDataset xyds = plot.getDataset();
            if (xyds != dataset || drawOutlines) {
                shapes = renderer.generatePeak2DShapes(peakAnnotations, dataset, non2DPeaks);
            }
        }
        Color fillColor = peakAnnotations.getColor();
        if (fillColor == null || fillColor.equals(Color.WHITE)
                || fillColor.equals(new Color(255, 255, 255, 0))) {
            Logger.getLogger(getClass().getName())
                    .fine("Peak annotation color was null or white, using color from treatment group!");
            fillColor = peakAnnotations.getChromatogram().getTreatmentGroup().getColor();
        }
        if (isVisible()) {
            renderer.draw(g2, chartPanel, plot, fillColor, shapes, selectedPeaks);
        }
    }
}

From source file:net.sf.maltcms.common.charts.ui.XYChartTopComponent.java

private void createChartPanel(final XYChartBuilder cb, final ADataset1D<?, TARGET> dataset,
        final InstanceContent content) {
    content.add(dataset);//w  w  w  . j a va  2s.c  o m
    ChartPanel customPanel = cb.buildPanel();
    SelectionOverlay so = new SelectionOverlay(Color.RED, Color.BLUE, 1.75f, 1.75f, 0.66f);
    customPanel.getChart().getXYPlot().getRangeAxis().addChangeListener(so);
    customPanel.getChart().getXYPlot().getDomainAxis().addChangeListener(so);
    InstanceContentSelectionHandler selectionHandler = new InstanceContentSelectionHandler(content, so,
            InstanceContentSelectionHandler.Mode.ON_CLICK, dataset);
    XYMouseSelectionHandler<TARGET> sl = new XYMouseSelectionHandler<>(dataset);
    sl.addSelectionChangeListener(so);
    sl.addSelectionChangeListener(selectionHandler);
    customPanel.addChartMouseListener(sl);
    customPanel.addOverlay(so);
    so.addChangeListener(customPanel);
    customizeChart(customPanel);
    content.add(customPanel);
    content.add(selectionHandler);
    content.add(sl);
}

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

@Override
public void mouseDragged(MouseEvent e) {
    super.mouseDragged(e);

    ChartPanel chartPanel = getChartPanel(e);
    JFreeChart selectedChart = chartPanel.getChart();
    ChartDescriptor cd = ChartUtils.getChartDescriptor(selectedChart);
    int[] indices = cd.getSourceIndices();

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

    //Create double buffer
    Image buffer = chartPanel.createImage(chartPanel.getWidth(), chartPanel.getHeight());
    Graphics bufferGraphics = buffer.getGraphics();
    chartPanel.paint(bufferGraphics);//from  w  ww  . j a  va 2s .  c o m

    if (lastX == 0 && lastY == 0) {
        lastX = e.getX();
        lastY = e.getY();
    }

    drawRect = new Rectangle();
    int x1 = Math.min(Math.min(e.getX(), lastX), startX);
    int y1 = Math.min(Math.min(e.getY(), lastY), startY);
    int x2 = Math.max(Math.max(e.getX(), lastX), startX);
    int y2 = Math.max(Math.max(e.getY(), lastY), startY);

    drawRect.x = x1;
    drawRect.y = y1;
    drawRect.width = x2 - drawRect.x;
    drawRect.height = y2 - drawRect.y;

    //Create a clipping rectangle
    Rectangle clipRect = new Rectangle(drawRect.x - 100, drawRect.y - 100, drawRect.width + 200,
            drawRect.height + 200);

    //Check for selected points
    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);
            Point2D datasetPoint2D = new Point2D.Double(domainValueTo2D(chartPanel, plot, xK.doubleValue()),
                    rangeValueTo2D(chartPanel, plot, yK.doubleValue()));

            if (drawRect.contains(datasetPoint2D)) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointAdded = mouseDragSelection.addPoint(cp);
                if (pointAdded) {
                    ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(j, i);
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            } else if (!mouseDragSelection.isEmpty()) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointRemoved = mouseDragSelection.removePoint(cp);
                if (pointRemoved) {
                    ((ScatterPlotRenderer) plot.getRenderer()).removeMarkedPoint(new Point(j, i));
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            }
        }
    }

    Iterator<PlotPointData> iterator = currentSelection.iterator();
    while (iterator.hasNext()) {
        PlotPointData next = iterator.next();
        Point dataPoint = next.getDataPoint();
        ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(dataPoint);
    }

    lastX = e.getX();
    lastY = e.getY();

    Graphics graphics = chartPanel.getGraphics();
    graphics.setClip(clipRect);

    //Draw selection rectangle
    bufferGraphics.drawRect(drawRect.x, drawRect.y, drawRect.width, drawRect.height);

    graphics.drawImage(buffer, 0, 0, chartPanel.getWidth(), chartPanel.getHeight(), null);
}

From source file:net.sf.maltcms.chromaui.charts.overlay.Peak1DOverlay.java

/**
 *
 * @param g2//from w w  w  .  ja  v a  2s.c  o  m
 * @param chartPanel
 */
@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    JFreeChart chart = chartPanel.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    ADataset1D<IChromatogram1D, IScan> newDataset;
    if (plot.getDataset() instanceof EIC1DDataset || plot.getDataset() instanceof Chromatogram1DDataset
            || plot.getDataset() instanceof TopViewDataset) {
        newDataset = (ADataset1D<IChromatogram1D, IScan>) plot.getDataset();
    } else {
        throw new IllegalArgumentException("Unsupported dataset type: " + plot.getDataset().getClass());
    }
    Color fillColor = peakAnnotations.getColor();
    if (fillColor == null || fillColor.equals(Color.WHITE) || fillColor.equals(new Color(255, 255, 255, 0))) {
        fillColor = peakAnnotations.getChromatogram().getTreatmentGroup().getColor();
    }
    if (shapes == null) {
        shapes = renderer.generatePeakShapes(peakAnnotations, newDataset);
        this.dataset = newDataset;
    } else {
        XYDataset xyds = plot.getDataset();
        if (xyds != dataset || drawOutlines) {
            shapes = renderer.generatePeakShapes(peakAnnotations, newDataset);
            this.dataset = newDataset;
        }
    }
    if (isVisible()) {
        renderer.draw(g2, chartPanel, plot, fillColor, shapes, selectedPeaks);
    }
}

From source file:lu.lippmann.cdb.ext.hydviga.ui.SimilarCasesFrame.java

/**
 * Constructor.//w  ww.j a va2  s.c  om
 */
SimilarCasesFrame(final Instances ds, final int dateIdx, final StationsDataProvider gcp, String attrname,
        final int gapsize, final int position, final double x, final double y, final int year,
        final String season, final boolean isDuringRising) throws Exception {
    LogoHelper.setLogo(this);
    this.setTitle("KnowledgeDB: Suggested configurations / similar cases");

    this.inputCaseTablePanel = new JXPanel();
    this.inputCaseTablePanel.setBorder(new TitledBorder("Present case"));
    this.inputCaseChartPanel = new JXPanel();
    this.inputCaseChartPanel.setBorder(new TitledBorder("Profile of the present case"));
    this.outputCasesTablePanel = new JXPanel();
    this.outputCasesTablePanel.setBorder(new TitledBorder("Suggested cases"));
    this.outputCasesChartPanel = new JXPanel();
    this.outputCasesChartPanel.setBorder(new TitledBorder("Profile of the selected suggested case"));

    getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
    getContentPane().add(inputCaseTablePanel);
    getContentPane().add(inputCaseChartPanel);
    getContentPane().add(outputCasesTablePanel);
    getContentPane().add(outputCasesChartPanel);

    final Instances res = GapFillingKnowledgeDB.findSimilarCases(attrname, x, y, year, season, gapsize,
            position, isDuringRising, gcp.findDownstreamStation(attrname) != null,
            gcp.findUpstreamStation(attrname) != null,
            GapsUtil.measureHighMiddleLowInterval(ds, ds.attribute(attrname).index(), position - 1));

    final Instances inputCase = new Instances(res);
    while (inputCase.numInstances() > 1)
        inputCase.remove(1);
    final JXTable inputCaseTable = buidJXTable(inputCase);
    final JScrollPane inputScrollPane = new JScrollPane(inputCaseTable);
    //System.out.println(inputScrollPane.getPreferredSize());
    inputScrollPane.setPreferredSize(
            new Dimension(COMPONENT_WIDTH, (int) (50 + inputScrollPane.getPreferredSize().getHeight())));
    this.inputCaseTablePanel.add(inputScrollPane);

    final ChartPanel inputcp = GapsUIUtil.buildGapChartPanel(ds, dateIdx, ds.attribute(attrname), gapsize,
            position);
    inputcp.getChart().removeLegend();
    inputcp.setPreferredSize(CHART_DIMENSION);
    this.inputCaseChartPanel.add(inputcp);

    final Instances outputCases = new Instances(res);
    outputCases.remove(0);
    final JXTable outputCasesTable = buidJXTable(outputCases);
    final JScrollPane outputScrollPane = new JScrollPane(outputCasesTable);
    outputScrollPane.setPreferredSize(
            new Dimension(COMPONENT_WIDTH, (int) (50 + outputScrollPane.getPreferredSize().getHeight())));
    this.outputCasesTablePanel.add(outputScrollPane);

    outputCasesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    outputCasesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                final int modelRow = outputCasesTable.getSelectedRow();

                final String attrname = outputCasesTable.getModel().getValueAt(modelRow, 1).toString();
                final int gapsize = (int) Double
                        .valueOf(outputCasesTable.getModel().getValueAt(modelRow, 4).toString()).doubleValue();
                final int position = (int) Double
                        .valueOf(outputCasesTable.getModel().getValueAt(modelRow, 5).toString()).doubleValue();

                try {
                    final ChartPanel cp = GapsUIUtil.buildGapChartPanel(ds, dateIdx, ds.attribute(attrname),
                            gapsize, position);
                    cp.getChart().removeLegend();
                    cp.setPreferredSize(CHART_DIMENSION);
                    outputCasesChartPanel.removeAll();
                    outputCasesChartPanel.add(cp);
                    getContentPane().repaint();
                    pack();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    });

    outputCasesTable.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(final MouseEvent e) {
            final InstanceTableModel instanceTableModel = (InstanceTableModel) outputCasesTable.getModel();
            final int row = outputCasesTable.rowAtPoint(e.getPoint());
            final int modelRow = outputCasesTable.convertRowIndexToModel(row);

            final String attrname = instanceTableModel.getValueAt(modelRow, 1).toString();
            final int gapsize = (int) Double.valueOf(instanceTableModel.getValueAt(modelRow, 4).toString())
                    .doubleValue();
            final int position = (int) Double.valueOf(instanceTableModel.getValueAt(modelRow, 5).toString())
                    .doubleValue();

            if (e.isPopupTrigger()) {
                final JPopupMenu jPopupMenu = new JPopupMenu("feur");

                final JMenuItem mi = new JMenuItem("Use this configuration");
                mi.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        System.out.println("not implemented!");
                    }
                });
                jPopupMenu.add(mi);
                jPopupMenu.show(outputCasesTable, e.getX(), e.getY());
            } else {
                // nothing?
            }
        }
    });

    setPreferredSize(new Dimension(FRAME_WIDTH, 900));

    pack();
    setVisible(true);

    /* select the first row */
    outputCasesTable.setRowSelectionInterval(0, 0);
}

From source file:net.sf.maltcms.chromaui.charts.overlay.Peak1DHeatmapOverlay.java

/**
 *
 * @param g2/*from w ww.j a v a2 s . c  o m*/
 * @param chartPanel
 */
@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    if (isVisible()) {
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        g2.clip(dataArea);
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        ValueAxis xAxis = plot.getDomainAxis();
        RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
        ValueAxis yAxis = plot.getRangeAxis();
        RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
        Color c = g2.getColor();
        Color fillColor = peakAnnotations.getColor();
        if (fillColor == null || fillColor.equals(Color.WHITE)
                || fillColor.equals(new Color(255, 255, 255, 0))) {
            Logger.getLogger(getClass().getName())
                    .info("Peak annotation color was null or white, using color from treatment group!");
            fillColor = peakAnnotations.getChromatogram().getTreatmentGroup().getColor();
        }
        g2.setColor(ChartCustomizer.withAlpha(fillColor, 0.5f));
        for (IPeakAnnotationDescriptor descr : peakAnnotations.getMembers()) {
            double x = descr.getApexTime();
            double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
            double width = xAxis.valueToJava2D(1, dataArea, xAxisEdge);
            double mzRange = (descr.getMassValues()[descr.getMassValues().length - 1]
                    - descr.getMassValues()[0]);
            double y = mzRange / 2.0d;
            double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
            double height = yAxis.valueToJava2D(mzRange, dataArea, yAxisEdge);
            AffineTransform at = AffineTransform.getTranslateInstance(xx, yy);
            at.concatenate(AffineTransform.getTranslateInstance(-x, -y));
            Rectangle2D.Double r = new Rectangle2D.Double(xx - (width / 2.0d), yy, width, height);
            g2.fill(at.createTransformedShape(r));
        }
        g2.setColor(c);
        g2.setClip(savedClip);
    }
}

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

@Override
public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    ChartPanel chartPanel = getChartPanel(e);
    startX = e.getX();/*from   w w  w.  ja  v a 2 s  .  c om*/
    startY = e.getY();

    if (!e.isShiftDown()) {
        ((ScatterPlotRenderer) ((XYPlot) chartPanel.getChart().getPlot()).getRenderer()).clearMarkedPoints();
        currentSelection = new ChartSelection();
        chartPanel.getChart().plotChanged(new PlotChangeEvent(chartPanel.getChart().getPlot()));
    } else {
        ((ScatterPlotRenderer) ((XYPlot) chartPanel.getChart().getPlot()).getRenderer())
                .removeMarkedPoint(null);
    }
    if (currentSelection == null) {
        currentSelection = new ChartSelection();
    }

    mouseDragSelection = new ChartSelection();
    currentSelection.setDescriptor(ChartUtils.getChartDescriptor(chartPanel.getChart()));
}

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

public void mouseClicked(MouseEvent me) {
    Point2D p = null;//www  .j  a v a  2 s . c om
    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);
}