Chinelo
Chinelo

Reputation: 61

java jfreechart library bubble size issue

I have an a jfreechart application that shows the values of three variables in a bubble chart. I have issues with make the bubbles on the graph show. It is only visible when you try to zoom out. But if Ii change the values in the third column/array to whole numbers like 1, 2, 3, 4, 5, the bubbles start becoming visible. Please can anyone help me out. I want the bubbles to have a specific size for all the plotted points.

Below is my source code:

package javaapplication2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
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;
import org.jfree.util.ShapeUtilities;

public class JavaApplication2 extends ApplicationFrame {

    public JavaApplication2(String s) {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(560, 370));
        setContentPane(jpanel);
    }

    private static JFreeChart createChart(XYZDataset xyzdataset) {
        JFreeChart jfreechart = ChartFactory.createBubbleChart(
            "AGE vs WEIGHT vs WORK",
            "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();

        double ad[] = {426081.759473, 426095.238564, 426109.490941, 426121.967623, 426130.564392, 426138.69481};
        double ad1[] = {113322.88289, 113271.915074, 113221.586484, 113172.511533, 113128.370338, 113085.254484};
        double ad2[] = {0.460819752574555, 0.850726080807893, 0.571903719860673, -0.013658463858901, -0.231945599223777, -0.256564887488579};
        double ad3[][] = {ad, ad1, ad2};
        defaultxyzdataset.addSeries("Series 1", 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[]) {
        JavaApplication2 bubblechart = new JavaApplication2("Bubble Chart_frame");

        bubblechart.pack();
        RefineryUtilities.centerFrameOnScreen(bubblechart);
        bubblechart.setVisible(true);
    }
}

Upvotes: 0

Views: 446

Answers (1)

trashgod
trashgod

Reputation: 205785

By default, ChartFactory.createBubbleChart() specifies an XYBubbleRenderer having SCALE_ON_RANGE_AXIS, but your x and y data are several orders of magnitude larger that your z data. As a result, no pixels are visible. You'll probably need to scale the data to get the desired effect. You can see the effect on your data by using a default renderer and zooming in on both axes using the context menu (right-click):

XYBubbleRenderer renderer = new XYBubbleRenderer(); //SCALE_ON_BOTH_AXES
xyPlot.setRenderer(renderer);

zoomed bubble chart

In the reference example below, I've used simpler data in a VERTICAL plot to make experimentation easier. Note that the sizes of the bubbles at (1, 1), (2, 2) and (3, 3) are in the ratio of the z coordinates: 2/3:3/3:4/3

XYZ bubble chart

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.XYBubbleRenderer;
import org.jfree.data.xy.DefaultXYZDataset;
import org.jfree.data.xy.XYZDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import static org.jfree.chart.renderer.xy.XYBubbleRenderer.*;

/** @see http://stackoverflow.com/a/41575283/230513 */
public class BubbleChartTest extends ApplicationFrame {

    public BubbleChartTest(String s) {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(560, 370));
        setContentPane(jpanel);
    }

    private static JFreeChart createChart(XYZDataset xyzDataset) {
        JFreeChart jfreechart = ChartFactory.createBubbleChart(
            "X vs Y vs Z", "X", "Y", xyzDataset,
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setForegroundAlpha(0.5F);
        XYBubbleRenderer renderer = new XYBubbleRenderer(SCALE_ON_BOTH_AXES);
        xyPlot.setRenderer(renderer);
        renderer.setSeriesPaint(0, Color.blue);
        NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
        domain.setLowerMargin(0.2);
        domain.setUpperMargin(0.4);
        NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
        range.setLowerMargin(0.2);
        range.setUpperMargin(0.4);

        return jfreechart;
    }

    public static XYZDataset createDataset() {
        DefaultXYZDataset dataset = new DefaultXYZDataset();
        double a[][] = {
            {1, 2, 3},
            {1, 2, 3},
            {2 / 3d, 3 / 3d, 4 / 3d}
        };
        dataset.addSeries("Series", a);
        return dataset;
    }

    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setDomainZoomable(true);
        chartPanel.setRangeZoomable(true);
        return chartPanel;
    }

    public static void main(String args[]) {
        BubbleChartTest test = new BubbleChartTest("Bubble Chart");
        test.pack();
        RefineryUtilities.centerFrameOnScreen(test);
        test.setVisible(true);
    }
}

Upvotes: 2

Related Questions