Java 如何在JFreechart中添加其他文本?
我只想添加有关图表的其他详细信息。我怎样才能包括其他细节,如在下面的图片 尝试注释(例如XYDrawableAnnotation)。下面是一个例子:Java 如何在JFreechart中添加其他文本?,java,swing,jfreechart,Java,Swing,Jfreechart,我只想添加有关图表的其他详细信息。我怎样才能包括其他细节,如在下面的图片 尝试注释(例如XYDrawableAnnotation)。下面是一个例子: 34,00000是计数器值。根据需要设置计数器值。在(x,y)轴上 您可以使用图形对象修改图表。 要获取图表的图形对象,请执行以下操作: 创建图表 获取图形的缓冲图像 获取缓冲图像图形并修改它们 将修改后的缓冲图像转换为png或jpg格式 以下是代码片段: // fetch chart as buffered image Buffered
34,00000是计数器值。根据需要设置计数器值。在(x,y)轴上 您可以使用图形对象修改图表。 要获取图表的图形对象,请执行以下操作:
// 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();
}
@Radu Murzea,在我没有与JFreechart合作之前。我只是希望解决方案可能与XYTEXTAnnotation有关。这意味着你需要添加标签,是吗??是指包含文本的标签吗?
// 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();
}