java jfreechart库气泡大小问题

时间:2017-01-10 15:59:52

标签: java jfreechart

我有一个jfreechart应用程序,它在气泡图中显示三个变量的值。我有问题,使图表上的气泡显示。它仅在您尝试缩小时可见。 但是如果Ii将第三列/数组中的值更改为整数,如1,2,3,4,5,则气泡开始变得可见。请任何人帮助我。我希望气泡具有所有绘制点的特定大小。

以下是我的源代码:

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);
    }
}

1 个答案:

答案 0 :(得分:2)

默认情况下,ChartFactory.createBubbleChart()指定XYBubbleRenderer SCALE_ON_RANGE_AXIS,但您的 x y 数据是几个数量级大于你的 z 数据。结果,没有可见的像素。您可能需要scale数据才能获得所需的效果。您可以使用默认渲染器并使用上下文菜单(右键单击)放大两个轴来查看对数据的影响:

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

zoomed bubble chart

在下面的参考示例中,我在VERTICAL图中使用了更简单的数据,以便更轻松地进行实验。请注意,(1,1),(2,2)和(3,3)处气泡的大小是 z 坐标的比率: 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);
    }
}