A discussion forum for JFreeChart (a 2D chart library for the Java platform).


Postby Anakin » Fri Mar 30, 2012 1:41 pm

I'm trying to display a heat map using jfreechart, but i can't make it work. I tried to create a defaultheatmapdataset and then use HeatMapUtilities.createHeatMapImage to create an image which i then tried to add to the plot as a XYDataImageAnnotation (a method someone suggested in another post on this forum). Here's the code:
Code: Select all
        double x=pct.getX();
        double y=pct.getY();
        double max=Double.NEGATIVE_INFINITY,min=Double.POSITIVE_INFINITY;
        DefaultHeatMapDataset series = new DefaultHeatMapDataset(100,100,x-10,x+10,y-10,y+10);
        double delta = 0.2;
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                Point pct2 = new Point(x - 10 + (double)i * delta, y - 10 + (double)j * delta);
                double val=f(pct2);
                if(val>max) max=val;
                if(val<min) min=val;
        NumberAxis xAxis = new NumberAxis("x");
        NumberAxis yAxis = new NumberAxis("y");
        LookupPaintScale scale=new LookupPaintScale(min,max,Color.GRAY);
        XYDataImageAnnotation ann=new XYDataImageAnnotation(HeatMapUtilities.createHeatMapImage(series, scale),x-10,y-10,20,20);
        XYSeriesCollection XYSeries=new XYSeriesCollection();
        chart = ChartFactory.createXYLineChart(
        XYPlot plot = (XYPlot) chart.getPlot();

where Point is a class that memorizes the x and y values.
But i only get a gray image (so the default color used in LookupPaintScale). The function f that i use for z value works fine (i tested it). I also tried with a DefaultXYZDataset() and i get exactly the same result.
Could you help me please? Thank you.
Posts: 4
Joined: Sun Feb 26, 2012 9:37 am

Re: HeatMap

Postby Anakin » Sat Mar 31, 2012 2:56 pm

I created the image myself in the end and i added it as a XYDataImageAnnotation.
But now can you tell me please if i can make it transparent? Or at least if i can make it stay in the background? Because i have a XYLineChart in which i need to add a XYSeries, but i can't display it because the image that i add as an annotation stays in the foreground, so the plot is not visible. Also, i can't add it as a backgroundImage (chart.setBackgroundImage) because i need it to ocupy a fixed amount of space in the plot (resize when i zoom in etc just like a XYDataImageAnnotation does).
Thank you.
Posts: 4
Joined: Sun Feb 26, 2012 9:37 am

Re: HeatMap

Postby Anakin » Tue Apr 03, 2012 7:39 pm

I managed to do it by setting the foreground alpha to 0.5 (neither 0 nor 1 don't work, wierd). Anyway, the support on this forum is amazing, 3 questions, no answer. I guess if i bought the manual things would be different. But to pay 65$ just to do my homework...I prefer paying 10 $ on a Stephen King book.
Posts: 4
Joined: Sun Feb 26, 2012 9:37 am

Re: HeatMap

Postby paradoxoff » Thu Apr 05, 2012 12:54 pm

support in a forum run by volunteers might by occasionally weak, in particular during a weekend.
Here are a couple of comments:
1. A LookupPaintScale will only return the default paint, unless you use the method add(double, Paint). You haven´t done that, thus you are getting only grey "tiles". Before you start using workarouns such as annotations, I would suggest to revisit the HeatMap of XYZDataset approach.
2. If you want to stick with the annotation: add the annotation not to the plot, but to the renderer. This allows to specify a Layer. On your case, Layer.BACKGROUND should work
Posts: 1622
Joined: Sat Feb 17, 2007 1:51 pm

Re: HeatMap

Postby aav » Fri May 04, 2012 7:31 pm

Hi Anakin.

i need to implement Heatmap, is it possible to help me by providing more code snippets....

my email is - aav987@gmail.com
Posts: 3
Joined: Fri May 04, 2012 6:58 pm

Re: HeatMap

Postby matinh » Thu Dec 14, 2017 10:44 pm

You can find a complete example of a heatmap on StackOverflow.

- martin
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Return to JFreeChart

Who is online

Users browsing this forum: Google [Bot] and 14 guests