我正在使用 jfree Chart 绘制一些点进行定位。
问题是图表是在灰色表面上绘制的,而不是在我的平面图上绘制的。所以我尝试使用背景图像,但这将图像放在背景中并且无法使用,因为我需要它。
我想用图像替换灰色背景。我怎样才能做到这一点?
我使用 ChartPanel 在其上绘制图表。问题是这仅允许颜色作为背景而不允许图像。我尝试为图表设置图片,如下面的代码所示,但这仅设置背景图像并在灰色区域的前景中绘制图表。
JFreeChart chart = ChartFactory.createScatterPlot("XY Chart", // Title
"x-axis", // x-axis Label
"y-axis", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
false, // Show Legend
false, // Use tooltips
false // Configure chart to generate URLs?
);
BufferedImage image = null;
try {
File url = new File(System.getProperty("user.dir").toString()+"\\1.jpg");
image = ImageIO.read(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
chart.setBackgroundImage(image);
最佳答案
我尝试过jfreechart-1.0.19。当您希望避免灰色背景时,更改图形区域的透明度会有所帮助(请注意行 chart.getPlot().setBackgroundAlpha(0);
):
public class MyChart extends ApplicationFrame {
public MyChart() throws Exception {
super("My Chart");
final XYSeries series1 = new XYSeries("First");
series1.add(1.0, 1.0);
series1.add(2.0, 4.0);
series1.add(3.0, 3.0);
final XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
JFreeChart chart = ChartFactory.createScatterPlot("XY Chart", // Title
"x-axis", // x-axis Label
"y-axis", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
false, // Show Legend
false, // Use tooltips
false // Configure chart to generate URLs?
);
BufferedImage image = null;
File url = new File("/home/me/Pictures/test.png");
image = ImageIO.read(url);
chart.setBackgroundImage(image);
chart.getPlot().setBackgroundAlpha(0);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
public static void main(String args[]) throws Exception {
final MyChart myChart = new MyChart();
myChart.pack();
RefineryUtilities.centerFrameOnScreen(myChart);
myChart.setVisible(true);
}
}
借助这个技巧,我得到了:
关于java - jfree Chart 在图像上绘制绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19651815/