I am experimenting with using the BubbleChart functionality in JFreechart but I am having difficulty figuring out how to control the size of the bubbles themselves (their diameters).. It seems to be a function of where the bubbles are (their X, Y locations) which doesn't make sense to me.
I would greatly appreciate it if someone could help me get my bubbles all the same size : )!!!!
Here's what I see in 2 charts generated from some simple sample code which creates two bubble charts the exact same way.. The only difference is the x and y locations of the bubbles on each of the 2 charts.

Here is the code that generated the images above..
As you will see when running it, the sizes of the bubbles are very different : (.
Code: Select all
package com.bubbleChartsControllingSizeOfBubbles;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultXYZDataset;
import org.jfree.data.xy.XYZDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class BubbleTest extends ApplicationFrame
{
static int chartIndex;
public BubbleTest( String s,int chartIndex )
{
super( s );
BubbleTest.chartIndex=chartIndex;
JPanel jpanel = createDemoPanel( );
jpanel.setPreferredSize(new Dimension( 560 , 370 ) );
setContentPane( jpanel );
}
private static JFreeChart createChart( XYZDataset xyzdataset )
{
JFreeChart jfreechart = ChartFactory.createBubbleChart(
"Why Bubbles Different Sizes?",
"Weight",
"AGE",
xyzdataset,
PlotOrientation.HORIZONTAL,
true, true, false);
XYPlot xyplot = ( XYPlot )jfreechart.getPlot( );
xyplot.setForegroundAlpha( 0.65F );
XYItemRenderer xyitemrenderer = xyplot.getRenderer( );
xyitemrenderer.setSeriesPaint( 0 , Color.blue );
NumberAxis numberaxis = ( NumberAxis )xyplot.getDomainAxis( );
numberaxis.setLowerMargin( 0.2 );
numberaxis.setUpperMargin( 0.5 );
NumberAxis numberaxis1 = ( NumberAxis )xyplot.getRangeAxis( );
numberaxis1.setLowerMargin( 0.8 );
numberaxis1.setUpperMargin( 0.9 );
return jfreechart;
}
public static XYZDataset createDataset( )
{
DefaultXYZDataset defaultxyzdataset = new DefaultXYZDataset();
if (chartIndex==1) {
for (int team=1; team<=4; team++) {
double x=0;double y=0;
if (team==1) {x=16; y=2.6; }
if (team==2) {x=14; y=2.6; }
if (team==3) {x=24; y=2.6; }
if (team==4) {x=23; y=2.3; }
double ad[ ] = { x };
double ad1[ ] = { y };
double ad2[ ] = { 1 }; //bubble diameter??
double ad3[][] = { ad , ad1 , ad2 };
defaultxyzdataset.addSeries( "Team "+team , ad3 );
}
} else if (chartIndex==2) {
for (int team=1; team<=4; team++) {
double x=0;double y=0;
if (team==1) {x=27.8; y=2.6; }
if (team==2) {x=30; y=3.0; }
if (team==3) {x=21.8; y=2.3; }
if (team==4) {x=4; y=1.6; }
double ad[ ] = { x };
double ad1[ ] = { y };
double ad2[ ] = { 1 }; //bubble diameter??
double ad3[][] = { ad , ad1 , ad2 };
defaultxyzdataset.addSeries( "Team "+team , ad3 );
}
}
return defaultxyzdataset;
}
public static JPanel createDemoPanel( )
{
JFreeChart jfreechart = createChart( createDataset( ) );
ChartPanel chartpanel = new ChartPanel( jfreechart );
chartpanel.setDomainZoomable( true );
chartpanel.setRangeZoomable( true );
return chartpanel;
}
public static void main( String args[ ] )
{
BubbleTest bubblechart1 = new BubbleTest( "Bubble 1",1 );
BubbleTest bubblechart2 = new BubbleTest( "Bubble 2",2 );
bubblechart1.pack( );
bubblechart2.pack( );
RefineryUtilities.positionFrameOnScreen(bubblechart1,0,0);
RefineryUtilities.positionFrameOnScreen(bubblechart2,0.5,0.50);
bubblechart1.setVisible( true );
bubblechart2.setVisible( true );
}
}