0

I am trying to obtain the equivalent of a matlab Pcolor added on a polar() function but in java.

I am pretty new to the language, but managed to obtain a polar plot already, with the following code:

public class Polar extends JFrame {

 public Polar(double[][] vec){

  XYDataset dataset = getXYDataset(vec);
  JFreeChart chart = ChartFactory.createPolarChart("test", dataset, true, true, true);
  PolarPlot plot = (PolarPlot) chart.getPlot();
  DefaultPolarItemRenderer render = (DefaultPolarItemRenderer)
  plot.getRenderer();
  render.setFillComposite(...);
  render.setSeriesFilled(0,true);
  ChartPanel panel = new ChartPanel(chart);
  panel.setMouseZoomable(false);
  setContentPane(panel);

 }

 private XYDataset getXYDataset(double[][] vec){
  XYSeriesCollection dataset = new XYSeriesCollection();
  XYSeries faultDP =new XYSeries("Serie1");
  for(int i = 0; i<vec.length; i++){
   faultDP.add(vec[i][1],vec[i][0]);
  }
  dataset.addSeries(faultDP);
  return dataset;
 }
}

The array vec contains the speed and angle of my variable, and should be plotted on the polar plot. this works fine.

The next step would be to pass a new variable, a double[][] vector of dimension 90x360. Each cell should be plotted on the polar plot with background color value, a bit like in the picture below.

Example of colormap

Any idea on how to do so ?

Cheers, Flo

FloF5ve
  • 63
  • 7
  • I've looked that way, I am just quite confused on how to implement it. – FloF5ve Dec 04 '18 at 08:48
  • Maybe a `PaintScaleLegend`, for [example](https://stackoverflow.com/a/37235165/230513). – trashgod Dec 04 '18 at 09:13
  • looked at that example as well before. It's a good one, I am just wondering how display my colormap in my case. Sorry, I am really new to java... – FloF5ve Dec 04 '18 at 12:37
  • Si I tried to use the `PainScaleLegend` class, but when I try to use it over a polar renderer I need to cast the `SpectrumPaintScale ps` into a Paint object (`r.setPaintScale((Paint)ps);`) , to get my code compiled, and even so, it doesn't work. I am making progress but still not quite there. – FloF5ve Dec 06 '18 at 13:57
  • For reference, here's a few more `PolarPlot` [examples](https://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjfreechart%5d%20PolarPlot), but you may need to look at a custom polar plot that can render a suitable `XYZDataset` in the way you need. Sorry, I don't know of any examples other than [tag:jfreechart] itself. – trashgod Dec 06 '18 at 18:15

1 Answers1

0

So, I've looked into what trashgod offered me and came with this code:

public class PolarTest extends JFrame {

public PolarTest(double[][] dipaz, double[][] val){

JFrame f = new JFrame("Title");
ChartPanel panel = new ChartPanel(createChart(val, dipaz));
panel.setPreferredSize(new Dimension(500,500));
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);


}

private JFreeChart createChart(double[][] val,double[][] dipaz){
    XYSeriesCollection dataset = new XYSeriesCollection ();
    double step, min, max;
    min  = val[0][2];
    max = val[val.length-1][2];
    step = (max-min)/10; //creation of 10 areas of similar heat range
    dataset = (XYSeriesCollection) createDataset(val,step);

    JFreeChart chart = ChartFactory.createPolarChart("test", dataset, true, true, true);
    PolarPlot plot = (PolarPlot) chart.getPlot();
    DefaultPolarItemRenderer r = (DefaultPolarItemRenderer) plot.getRenderer();
    r.setFillComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f));

    for(int i =0; i<dataset.getSeriesCount(); i++){
        r.setSeriesFilled(i, true);
        r.setShapesVisible(false);
        r.setDrawOutlineWhenFilled(false);
    }

    NumberAxis rangeAxis = (NumberAxis) plot.getAxis();
    rangeAxis.setTickLabelsVisible(false);
    return chart;
}  

private XYDataset createDataset(double[][] val, double step){
 XYSeriesCollection result = new XYSeriesCollection();
 double dpTmp, breakPoint;
 int cpt=1;
 boolean t;

 t= true;

 dpTmp = val[0][2];
 breakPoint = dpTmp + step;
 XYSeries series = null ;

 for(int i = 0; i< val.length; i++){
    if(val[i][2] < breakPoint){
        if(t){
         series = new XYSeries(String.valueOf(cpt));
         t = false;
        }
       series.add(val[i][1],val[i][0]);
       cpt++;
    }else{
       result.addSeries(series);
       i--;
       t=true;
       breakPoint = val[i][2] + step;
    }
 }
 return result;
}

The array dipaz contains my angle and dip (of X,Y dots I'd like to later plot on the polar representation), while val a 3 by N array: val[][0] = dip val[][1] = angle val[][2] = value of "heat" , if we see that problem as a heatmap.

val is ascending sorted by the third column. My idea was to split the whole array in 10 (or more) areas of similar heat values.

if I run this code, I obtain the following output (cf pic). We can see that it's getting there, but not quite. I am not sure that this code is able to map correctly my heat on the polar plot>

enter image description here

How could I get closer to the picture I posted in my first post?

Thank you for your help. Flo

FloF5ve
  • 63
  • 7