2013-07-28 55 views
1

我需要在XYPlot上绘制大量数据(150-250点/秒* 180secs)。所以如果我使用自动量程方法,它有点粗糙。如果我放大图,我会看到一系列的数据(例如10.25到14.50)..它很好,它工作得很好,但是如果我看到全范围,但是在更好的分辨率下会更好。JFreeChart在缩放时自动调整大小

是否有可能放大绘图并另外调整绘图区域的大小(以便您有更多空间来打印绘图),以便显示整个范围(例如从0到180秒),而不仅仅是一节?

我到目前为止所尝试的是固定巨大的情节没有缩放,但它不可用(大小为1200x15000)。

在此先感谢!

回答

3

here所示,覆盖getPreferredSize()以返回所需大小的ChartPanelpack()封闭帧。通过在setExtendedState()中应用JFrame.MAXIMIZED_BOTH获得的结果将最大限度地按照用户选择的分辨率使用屏幕,但如果可用,您可以尝试不同的display mode

+0

感谢你的答案和链接。我将尝试调整缩放事件中的情节 - 这可能是可能的。谢谢! – lhlmgr

1

我使用getPreferredSize()方法作为垃圾建议。我从org.jfree.chart.MouseWheelHandler扩展并实现了一个新的handleZoomable方法如下。


package org.jfree.chart; 

import java.awt.Dimension; 
import java.awt.event.MouseWheelEvent; 
import java.awt.event.MouseWheelListener; 
import java.awt.geom.Point2D; 
import java.io.Serializable; 

import org.jfree.chart.plot.PiePlot; 
import org.jfree.chart.plot.Plot; 
import org.jfree.chart.plot.PlotRenderingInfo; 
import org.jfree.chart.plot.Zoomable; 

/** http://stackoverflow.com/questions/17908498/jfreechart-auto-resize-on-zoom */ 
class MouseWheelHandlerResize extends MouseWheelHandler { 

    /** The chart panel. */ 
    private ChartPanel chartPanel; 

    /** The zoom factor. */ 
    double zoomFactor; 

    /** minimum size */ 
    final int MIN_SIZE = 300; 

    /** maximal size */ 
    final int MAX_SIZE = 20000; 

    public MouseWheelHandlerResize(ChartPanel chartPanel) { 
     super(chartPanel); 
     this.chartPanel = chartPanel; 
     this.zoomFactor = 0.05; 
    } 

    @Override 
    public void mouseWheelMoved(MouseWheelEvent e) { 
     JFreeChart chart = this.chartPanel.getChart(); 
     if (chart == null) { 
      return; 
     } 
     Plot plot = chart.getPlot(); 
     if (plot instanceof Zoomable) { 
      Zoomable zoomable = (Zoomable) plot; 
      handleZoomable(zoomable, e); 
     } 
     else if (plot instanceof PiePlot) { 
      PiePlot pp = (PiePlot) plot; 
      pp.handleMouseWheelRotation(e.getWheelRotation()); 
     } 
    } 

    private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) { 
     // don't zoom unless the mouse pointer is in the plot's data area 
     ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo(); 
     PlotRenderingInfo pinfo = info.getPlotInfo(); 
     Point2D p = this.chartPanel.translateScreenToJava2D(e.getPoint()); 
     if (!pinfo.getDataArea().contains(p)) { 
      return; 
     } 

     Plot plot = (Plot) zoomable; 
     // do not notify while zooming each axis 
     boolean notifyState = plot.isNotify(); 
     plot.setNotify(false); 
     int clicks = e.getWheelRotation(); 
     double zf = 1.0 + this.zoomFactor; 
     if (clicks < 0) { 
      zf = 1.0/zf; 
     } 
     final Dimension dim = this.chartPanel.getPreferredSize(); 
     this.chartPanel.setPreferredSize(new Dimension((int)(Math.min(Math.max(MIN_SIZE, dim.width)*zf, MAX_SIZE)), (int)(dim.height)));   
     this.chartPanel.validate(); 
     this.chartPanel.updateUI(); 
     plot.setNotify(notifyState); // this generates the change event too 
    } 

} 
 

而在org.jfree.chart.ChartPanel课,我只是setMouseWheelEnabled法修改:


public void setMouseWheelEnabled(boolean flag) { 
     if (flag && this.mouseWheelHandler == null) { 
      this.mouseWheelHandler = new MouseWheelHandlerResize(this); 
     } 
     else if (!flag && this.mouseWheelHandler != null) { 
      removeMouseWheelListener(this.mouseWheelHandler); 
      this.mouseWheelHandler = null; 
     } 
    } 

而现在,CharPanel它位于一个滚动视图调整大小,并放大

+1

另请参阅此相关的[示例](https://stackoverflow.com/a/41544007/230513)。 – trashgod