启用/禁用JFreeChart的图形 [英] Enabling/Disabling drawing of a JFreeChart

查看:56
本文介绍了启用/禁用JFreeChart的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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.

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

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;
    }
  }
}

这篇关于启用/禁用JFreeChart的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆