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.