Java Code Examples for org.jfree.chart.ChartPanel#zoom()

The following examples show how to use org.jfree.chart.ChartPanel#zoom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChartPanelTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks that a call to the zoom() method generates just one
 * ChartChangeEvent.
 */
public void test2502355_zoom() {
    DefaultXYDataset dataset = new DefaultXYDataset();
    JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
            "Y", dataset, false);
    ChartPanel panel = new ChartPanel(chart);
    chart.addChangeListener(this);
    this.chartChangeEvents.clear();
    panel.zoom(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
    assertEquals(1, this.chartChangeEvents.size());
}
 
Example 2
Source File: ZoomHandler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void mouseReleased(MouseEvent e) {

        if (this.zoomRectangle == null) {
            return;
        }
        ChartPanel panel = (ChartPanel) e.getSource();
        boolean hZoom = false;
        boolean vZoom = false;
        if (panel.getOrientation() == PlotOrientation.HORIZONTAL) {
            hZoom = panel.isRangeZoomable();
            vZoom = panel.isDomainZoomable();
        }
        else {
            hZoom = panel.isDomainZoomable();
            vZoom = panel.isRangeZoomable();
        }

        boolean zoomTrigger1 = hZoom && Math.abs(e.getX()
                - this.zoomPoint.getX()) >= panel.getZoomTriggerDistance();
        boolean zoomTrigger2 = vZoom && Math.abs(e.getY()
                - this.zoomPoint.getY()) >= panel.getZoomTriggerDistance();
        if (zoomTrigger1 || zoomTrigger2) {
            if ((hZoom && (e.getX() < this.zoomPoint.getX()))
                    || (vZoom && (e.getY() < this.zoomPoint.getY()))) {
                panel.restoreAutoBounds();
            }
            else {
                double x, y, w, h;
                Rectangle2D screenDataArea = panel.getScreenDataArea(
                        (int) this.zoomPoint.getX(),
                        (int) this.zoomPoint.getY());
                double maxX = screenDataArea.getMaxX();
                double maxY = screenDataArea.getMaxY();
                // for mouseReleased event, (horizontalZoom || verticalZoom)
                // will be true, so we can just test for either being false;
                // otherwise both are true
                if (!vZoom) {
                    x = this.zoomPoint.getX();
                    y = screenDataArea.getMinY();
                    w = Math.min(this.zoomRectangle.getWidth(),
                            maxX - this.zoomPoint.getX());
                    h = screenDataArea.getHeight();
                }
                else if (!hZoom) {
                    x = screenDataArea.getMinX();
                    y = this.zoomPoint.getY();
                    w = screenDataArea.getWidth();
                    h = Math.min(this.zoomRectangle.getHeight(),
                            maxY - this.zoomPoint.getY());
                }
                else {
                    x = this.zoomPoint.getX();
                    y = this.zoomPoint.getY();
                    w = Math.min(this.zoomRectangle.getWidth(),
                            maxX - this.zoomPoint.getX());
                    h = Math.min(this.zoomRectangle.getHeight(),
                            maxY - this.zoomPoint.getY());
                }
                Rectangle2D zoomArea = new Rectangle2D.Double(x, y, w, h);
                panel.zoom(zoomArea);
            }
            this.zoomPoint = null;
            this.zoomRectangle = null;
            panel.setZoomRectangle(null);
            panel.clearLiveMouseHandler();
        }
        else {
            // erase the zoom rectangle
            Graphics2D g2 = (Graphics2D) panel.getGraphics();
            if (panel.getUseBuffer()) {
                panel.repaint();
            }
            else {
                drawZoomRectangle(panel, g2, true);
            }
            g2.dispose();
            this.zoomPoint = null;
            this.zoomRectangle = null;
            panel.setZoomRectangle(null);
            panel.clearLiveMouseHandler();
        }
    }