如何在JFreechart中添加附加文本?

4

我想补充关于图表的额外细节。我该如何添加类似下面图片中的额外细节。

enter image description here


@Radu Murzea,之前我没有使用过JFreechart。我只是希望解决方案可能与XYTextAnnotiation有关。 - A.Mohamed Bilal
你的意思是需要添加标签吗?也就是包含文本的标签,对吗? - Kishan Bheemajiyani
3个回答

3
    final Marker start = new ValueMarker(3400000.0);
    start.setPaint(Color.red);
    start.setLabel("Current Value");
    start.setLabelAnchor(RectangleAnchor.BOTTOM_LEFT);
    start.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    plot.addRangeMarker(start);

34,00,000是计数器的值。根据您的需要设置计数器值。在(x,y)轴上。


1
您可以使用图形对象修改图表。 获取图表的图形对象的方法如下:
  1. 创建图表。
  2. 获取图表的缓冲图像。
  3. 获取缓冲图像的图形并进行修改。
  4. 将修改后的缓冲图像转换为png或jpg格式。
以下是代码片段:
// fetch chart as buffered image    
BufferedImage image = chart.createBufferedImage(width, height);
// fetch graphics from the buffered image for perform modifications.
Graphics2D g2 = (Graphics2D) image.getGraphics();
g2.setFont(g2.getFont().deriveFont(30f));
g2.setColor(Color.red);
g2.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
String str = "Test String";
float location_x = 200;
float location_y = 200;
// will draw string horizontally
TextUtilities.drawAlignedString(str, g2, location_x, 
        location_y, TextAnchor.CENTER_LEFT);
// will draw string Vertically
TextUtilities.drawRotatedString(str, g2, -Math.PI / 2,
        location_x, location_y);
g2.dispose();
// generate png file from the modified buffered image
String path = "/sample/test.png";
try {
  ImageIO.write(image, "png", new File(path));
} catch (IOException e) {
  System.out.println("Error While Creating chart");
  e.printStackTrace();
}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接