有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java装饰JFreeChart

我想在角落里用一个时间戳装饰我生成的所有JFreeCharts。JFreeChart框架中是否有一种方法可以在图表生成后在图像上绘制

编辑:请注意,这些图表是在后台线程中生成的,并通过servlet分发,因此没有GUI,我不能只在单独的组件上显示时间戳


共 (5) 个答案

  1. # 1 楼答案

    请看以下论坛帖子:

    http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=27939

    使用图像图标作为水印的:

    ImageIcon icon = new ImageIcon(new URL(watermarkUrl));
    Image image = icon.getImage();
    chart.setBackgroundImage(image);
    chart.setBackgroundImageAlignment(Align.CENTER);
    chart.getPlot().setBackgroundAlpha(0.2f);
    
  2. # 2 楼答案

    一种方法是将ChartPanel子类化,并重写paint(Graphics)方法以首先链接到super.paint(Graphics),然后在图表顶部呈现附加文本

    不过,我觉得这有点老套,我个人倾向于简单地将ChartPanel添加到另一个容器JPanel以及表示时间戳的JLabel

  3. # 3 楼答案

    根据我的经验,向JFreeChart添加自定义信息的最佳方法是: -实例化同一类型图表的BuffereImage; -在BuffereImage上绘制; -使用XYImageAnnotation将图像添加到当前打印

    这是本规范的指南:

    // retrieve image type and create another BufferedImage
    int imgType = chart.createBufferedImage(1,1).getType();
    BufferedImage bimg = new BufferedImage(width, height, bimg.getType);
    
    // here you can draw inside the image ( relative x & y  )
    Graphics2D g2 = (Graphics2D) bimg.getGraphics();
    g2.drawString("Hello, JFreeChart " + timestamp, posX, posY );
    
    // instantiate the image annotation, then add to the plot
    XYImageAnnotation a = new XYImageAnnotation( x, y, bimg, RectangleAnchor.LEFT );
    chart.getPlot().addAnnotation( a );
    
  4. # 4 楼答案

    在与它闲逛了一段时间后,我发现了一个解决方案,可以让你在JFreeChart完成后在图像上任意绘制,所以我将把它发布在这里供后人使用。在我的例子中,我正在将图表写入OutputStream,因此我的代码如下所示:

    BufferedImage chartImage = chart.createBufferedImage(width, height, null);
    Graphics2D g = (Graphics2D) chartImage.getGraphics();
    /* arbitrary drawing happens here */
    EncoderUtil.writeBufferedImage(chartImage, ImageFormat.PNG, outputStream);
    
  5. # 5 楼答案

    ^{}^{}方法可能是合适的替代方法