本文概述
- 气泡图示例
- BubbleChartExample.java
下图显示了JFreeChart库中合并的Bubble图表的一些演示版本:
气泡图演示1:
文章图片
气泡图演示2:
文章图片
气泡图示例 让我们考虑以下气泡图示例数据。
国家 | 汽车(百万)X值 | 公交车(百万)Y值 | 卡车(百万)Z值 |
---|---|---|---|
India | 40 | 65 | 70 |
美国 | 30 | 20 | 50 |
China | 80 | 50 | 80 |
Japan | 11 | 50 | 20 |
BubbleChartExample.java
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.BubbleXYItemLabelGenerator;
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;
public class BubbleChartExample extends JFrame {private static final long serialVersionUID = 1L;
public BubbleChartExample(String title) {super(title);
// Create datasetXYZDataset dataset = createDataset();
// Create chartJFreeChart chart = ChartFactory.createBubbleChart("Country(Cars, Buses, Trucks)", "X-Values", "Y-Values", dataset);
// Set range for X-AxisXYPlot plot = chart.getXYPlot();
NumberAxis domain = (NumberAxis) plot.getDomainAxis();
domain.setRange(0, 100);
// Set range for Y-AxisNumberAxis range = (NumberAxis) plot.getRangeAxis();
range.setRange(0, 100);
//Format labelXYBubbleRenderer renderer=(XYBubbleRenderer)plot.getRenderer();
BubbleXYItemLabelGenerator generator=new BubbleXYItemLabelGenerator(" {0}:({1}, {2}, {3}) ", new DecimalFormat("0"), new DecimalFormat("0"), new DecimalFormat("0"));
renderer.setBaseItemLabelGenerator(generator);
renderer.setBaseItemLabelsVisible(true);
// Create PanelChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
}private XYZDataset createDataset() {DefaultXYZDataset dataset = new DefaultXYZDataset();
dataset.addSeries("INDIA", new double[][] { { 40 }, // X-Value { 65 }, // Y-Value { 70 }// Z-Value });
dataset.addSeries("USA", new double[][] { { 30 }, { 20 }, { 50 } });
dataset.addSeries("CHINA", new double[][] { { 80 }, { 50 }, { 80 } });
dataset.addSeries("JAPAN", new double[][] { { 11 }, { 50 }, { 20 } });
return dataset;
}public static void main(String[] args) {SwingUtilities.invokeLater(() ->
{BubbleChartExample example = new BubbleChartExample("Bubble Chart Example");
example.setSize(800, 400);
example.setLocationRelativeTo(null);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
example.setVisible(true);
});
}}
【JFreeChart-气泡图】输出:
文章图片
推荐阅读
- DefaultCategoryDataset类
- JFreeChart条形图
- JFreeChart架构
- 什么是JFreeChart()
- JFreeChart教程介绍
- PiePlot类介绍
- JFreeChart的关键类
- 安装JfreeChart
- Javafreechart功能