开发者

JFreechart alternative

I am using JFreeChart in one of my java applications but the problem with it is that it paints the whole chart again when you make any changes to the chart model.

I know that we can remove the ChartChangeListener and add them according to our needs so that it don'开发者_如何转开发t fire chartChangeEvent everytime but it doesn't solve my problem.

In my case, the XYSeries goes beyond the chart boundary on the X-axis. The visible x-axis is 6cm. So when the series goes beyond 6cm, I am discarding the 1cm initial visible data and plotting the next 6cm data again.

In that case, it starts blinking.


Plotting just visible area is a wrong idea. Generally what you really want is to create a BufferedImage which will contain the whole chart and then you don't need to repaint the whole chart 1000 times per second. If you can't buffer the whole chart (e.g. you're plotting a real-time data) you should at least save to buffer a single point if it's bigger than 1px. Filling shapes is very expensive operation in java.

Following code is terribly slow (when plotting too many points)

   for(int x=0; x < max; x++){
     Ellipse2D.Double ellipse = new Ellipse2D.Double(x - 1.5, f(x) -1.5, 3, 3);
     g.setPaint(color);
     g.draw(ellipse);
     g.fill(ellipse);
   }

It should be replaced by

   private BufferedImage createBufferedImage(Color color) {
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D buff = bufferedImage.createGraphics();
        Ellipse2D.Double ellipse = new Ellipse2D.Double(0, 0, 3, 3);
        buff.setPaint(color);
        buff.draw(ellipse);
        buff.fill(ellipse);
        return bufferedImage;
    }

   AffineTransform at = new AffineTransform();
   at.scale(1, 1);
   createBufferedImage(Color.RED);
   for(int x=0; x < max; x++){
            at.setToIdentity();
            at.translate(x - 2, y - 2);
            g.drawImage(bufferedImage, at, null);
   }

which does the same, just many times faster. AFAIK in JFreeChart they don't use buffering properly, which is one of reasons why it is so slow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜