启用/禁用 JFreeChart 的绘制

发布于 2024-10-31 19:31:31 字数 623 浏览 1 评论 0原文

我创建了一个图表,如下所示,并且我正在向 TimeSeries 添加值(在程序中的不同位置)。 ChartPanel 实际上包含在 JTabbedPane 中,我不想重绘图表,除非它的选项卡正在显示。有什么方法可以让我表明,当新数据进入 TimeSeries 时,不应进行渲染,除非该选项卡是当前显示的选项卡?我猜测有一些调用表明数据已更新并且需要新的渲染,所以基本上我想拦截该调用,并且如果未显示选项卡则不执行任何操作,如果选项卡正在显示则让调用通过显示,并在用户切换到该选项卡时手动调用一次。这对于后台的一个 ChartPanel 来说并不是一个主要问题,但我在不同的选项卡上有几个,它开始消耗 CPU,不断更新 4-5 个图表。

    sAccuracy = new TimeSeries("a");
    TimeSeriesCollection dataset = new TimeSeriesCollection(sAccuracy);
    JFreeChart c = ChartFactory.createTimeSeriesChart("Accuracy",
            "", "Percent", dataset, false, false, false);

    ChartPanel cp = new ChartPanel(c);

I have a chart created as shown below, and I am adding values to the TimeSeries (in a different location in my program). The ChartPanel is actually contained within a JTabbedPane, and I would like to not redraw the chart unless it's tab is being displayed. Is there any way for me to signal that rendering should not occur when new data comes into the TimeSeries unless that tab is the one currently being shown? I'm guessing there is some call that signals the data has been updated and a new rendering is needed, so basically I want to intercept that call and do nothing if the tab is not being shown, let the call through if the tab is being shown, and call it once manually when the user switches to that tab. This isn't a major issue with one ChartPanel in the background, but I have a few on different tabs and it's starting to eat CPU like nasty to update 4-5 charts constantly.

    sAccuracy = new TimeSeries("a");
    TimeSeriesCollection dataset = new TimeSeriesCollection(sAccuracy);
    JFreeChart c = ChartFactory.createTimeSeriesChart("Accuracy",
            "", "Percent", dataset, false, false, false);

    ChartPanel cp = new ChartPanel(c);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┈┾☆殇 2024-11-07 19:31:31

我也遇到过同样的问题,即 JFreechart API 相当笨重,每当添加单个数据点时都会简单地重新绘制整个图表,从而导致大量的渲染开销。

我解决这个问题的方法是实现我自己的底层模型(例如 XYDataset 实现),该模型知道何时显示包含它的图表,并且仅在该图表时传播事件可见 - 如果图表不可见,则模型应将事件的触发推迟到稍后;例如

public class MyXYDataset extends AbstractXYDataset {
  private boolean shown;
  private boolean pendingEvent;

  /**
   * Called when the chart containing this dataset is being displayed
   * (e.g. hook this into a selection listener that listens to tab selection events).
   */
  public void setShown(boolean shown) {
    this.shown = shown;

    if (this.shown && this.pendingEvent) {
      this.pendingEvent = false;
      fireDatasetChanged();
    }
  }

  public void addDatapoint(double x, double y) {
    // TODO: Add to underlying collection.

    if (this.shown) {
      // Chart is currently displayed so propagate event immediately.
      fireDatasetChanged();
    } else {
      // Chart is hidden so delay firing of event but record that we need to fire one.
      this.pendingEvent = true;
    }
  }
}

I've faced the same problem whereby the JFreechart API is fairly clunky and simply repaints the entire chart whenever a single datapoint is added resulting in a large rendering overhead.

The way I've solved this is to implement my own underlying model (e.g. XYDataset implementation) that is aware of when the chart containing it is being displayed, and to only propagate events when that chart is visible - If the chart is not visible then the model should defer the firing of events until later; e.g.

public class MyXYDataset extends AbstractXYDataset {
  private boolean shown;
  private boolean pendingEvent;

  /**
   * Called when the chart containing this dataset is being displayed
   * (e.g. hook this into a selection listener that listens to tab selection events).
   */
  public void setShown(boolean shown) {
    this.shown = shown;

    if (this.shown && this.pendingEvent) {
      this.pendingEvent = false;
      fireDatasetChanged();
    }
  }

  public void addDatapoint(double x, double y) {
    // TODO: Add to underlying collection.

    if (this.shown) {
      // Chart is currently displayed so propagate event immediately.
      fireDatasetChanged();
    } else {
      // Chart is hidden so delay firing of event but record that we need to fire one.
      this.pendingEvent = true;
    }
  }
}
櫻之舞 2024-11-07 19:31:31

另一种可能性是设置c.setNotify(false);,这将阻止图表监听ChartChangeEvent

http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart /JFreeChart.html#setNotify(布尔值)

Another possibility is to set c.setNotify(false);, which will prevent the chart to listen to the ChartChangeEvent:

http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html#setNotify(boolean)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文