HeatMap

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Anakin
Posts: 4
Joined: Sun Feb 26, 2012 9:37 am
antibot: No, of course not.

HeatMap

Post by Anakin » Fri Mar 30, 2012 1:41 pm

Hello,
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);
                series.setZValue(i,j,val);
                if(val>max) max=val;
                if(val<min) min=val;
            }
        }
   
        NumberAxis xAxis = new NumberAxis("x");
        xAxis.setLowerBound(x-10);
        xAxis.setUpperBound(x+10);
        NumberAxis yAxis = new NumberAxis("y");
        yAxis.setLowerBound(y-10);
        yAxis.setUpperBound(y+10);
        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(
                "f(x,y)",
                "x",
                "y",
                XYSeries, 
                PlotOrientation.VERTICAL, 
                false, 
                true,
                false
                );
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.addAnnotation(ann);
        plot.setDomainAxis(xAxis);
        plot.setRangeAxis(yAxis);
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.

Anakin
Posts: 4
Joined: Sun Feb 26, 2012 9:37 am
antibot: No, of course not.

Re: HeatMap

Post by 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.

Anakin
Posts: 4
Joined: Sun Feb 26, 2012 9:37 am
antibot: No, of course not.

Re: HeatMap

Post by 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.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: HeatMap

Post by paradoxoff » Thu Apr 05, 2012 12:54 pm

Hi,
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

aav
Posts: 3
Joined: Fri May 04, 2012 6:58 pm
antibot: No, of course not.

Re: HeatMap

Post by 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

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

Re: HeatMap

Post by matinh » Thu Dec 14, 2017 10:44 pm

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

hth,
- martin

Locked