1

I want to plot some data on a scatter plot: x against y where each point in the series has a point size and a color. Is this possible?

say for example

int[] x = {1,2,3,4,5};
int[] y = {2,4,6,8,10};

int[] pointSize = {10,20,40,15,25}; //pixels
Color[] colors = {rgb1,rgb2,rgb3,rgb4,rgb5};

I'm quite new to JFree so if you could post some example code that would be perfect :D

Eduardo
  • 6,900
  • 17
  • 77
  • 121

1 Answers1

3

You can override XYShapeRenderer#XYShapeRenderer and XYShapeRenderer#getItemShape

final Shape[] pointSize = {createCircle(10),createCircle(20),createCircle(40),createCircle(15),createCircle(25)}; //pixels
final Color[] colors = {Color.red,Color.yellow,Color.pink,Color.blue,Color.cyan};

plot.setRenderer(new XYShapeRenderer() {
   @Override
   public Paint getItemPaint(int row, int column) {
     try {
       return colors[column];
     } catch (Exception e) {
       return colors[0];
     }
}

  @Override
  public Shape getItemShape(int row, int column) {
    try {
      return pointSize[column];
    } catch (Exception e) {
      return pointSize[0];
   }
}
});

I'm using a helper function to create the points:

private static Shape CreateCircle(double size){
     return new Ellipse2D.Double(-size/2,-size/2,size,size);
}

This will create a chart like this:

enter image description here

You could also get a similer result using a Bubble Chart

enter image description here

THis image is from the JFreeChart documentation

GrahamA
  • 5,875
  • 29
  • 39
  • Should it be return new Ellipse2D.Double(size/2,size/2,size,size); or return new Ellipse2D.Double(-size/2,-size/2,size,size); The circles appear to be off from their location using positive offsets – Eduardo Oct 23 '13 at 17:44