如何在JFreechart中添加其他文本?

时间:2014-01-30 07:19:27

标签: java swing jfreechart

我只想添加有关图表的其他详细信息。我如何在下面的图片中添加其他详细信息。

enter image description here

3 个答案:

答案 0 :(得分:1)

尝试注释(例如,XYDrawableAnnotation)。这是一个例子:

http://www.java2s.com/Code/Java/Chart/JFreeChartMarkerDemo1.htm

答案 1 :(得分:1)

    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)轴上。

答案 2 :(得分:1)

您可以使用图形对象修改图表。 获取图表的图形对象:

  1. 创建图表。
  2. 获取图表的缓冲图像。
  3. 获取缓冲的图像图像并进行修改。
  4. 将修改后的缓冲图像转换为png或jpg格式。
  5. 以下是代码段:

    // 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();
    }