JFreeChart scatter chart with varying point colours and sizes

三世轮回 提交于 2019-12-11 04:48:01

问题


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


回答1:


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:

You could also get a similer result using a Bubble Chart

THis image is from the JFreeChart documentation



来源:https://stackoverflow.com/questions/19526824/jfreechart-scatter-chart-with-varying-point-colours-and-sizes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!