jfree图表绘制图形

时间:2013-10-29 07:08:24

标签: java image jfreechart

我正在使用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);

1 个答案:

答案 0 :(得分:0)

我已尝试使用 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);
    }
}

借助这个技巧,我得到了:

screenshot