1

I'm using jfree Chart to draw some points for positioning.

The problem is that the diagram is drawn on a grey surface instead on my floor plan. So I tried to use the background image, but this put the image in the background and is not usable as I would need it.

I would like to replace the the grey background by an image. How can I do this?

I use a ChartPanel to draw the chart on it. The problem is that this allows only colors as background an no images. I tried to set a picture to the chart as can be seen in the code below, but this set only background image and draw the chart in the foreground on a grey area.

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);
Irgendw Pointer
  • 1,770
  • 3
  • 30
  • 67
  • You should add more detail to your question, including some of your code and a screenshot if you can get one, with a better explanation of what you expect to be drawn. – Brian Oct 29 '13 at 07:11
  • I think you are drawing on panel. Try to set background to the panel and then draw on it. – AJ. Oct 29 '13 at 07:12

1 Answers1

0

I've tried with jfreechart-1.0.19. As you would like to avoid gray background changing transparency of the graph area helps (pay attention to line 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);
    }
}

With aid of this trick I've got:

screenshot

flaz14
  • 826
  • 1
  • 13
  • 25