logo图片
JFreechart教程

JFreeChart 散点图

JFreeChart-散点图

散点图或散点图包含一组绘制在水平轴和垂直轴上的数据点。它显示来自 XYDataset 的数据。
下图显示了 JFreeChart 库中包含的一些散点图演示版本:
散点图演示 1:
JFreeChart 散点图演示 1
散点图演示 2:
JFreeChart 散点图演示 2
散点图演示 3:
JFreeChart 散点图演示 3

散点图示例:

让我们考虑以下散点图示例数据。
男孩(系列 1) 女孩(系列 2)
年龄 重量 年龄 重量
1 72.9 1 72.5
2 81.6 2 80.1
3 88.9 3 87.2
4 96 4 94.5
5 102.1 5 101.4
6 108.5 6 107.4
7 113.9 7 112.8
8 119.3 8 118.2
9 123.8 9 122.9
10 124.4 10 123.4
以下代码根据上述示例数据创建散点图:

ScatterPlotExample.java

import java.awt.Color;
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.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ScatterPlotExample extends JFrame {
  private static final long serialVersionUID = 6294689542092367723L;
  public ScatterPlotExample(String title) {
    super(title);
    // Create dataset
    XYDataset dataset = createDataset();
    // Create chart
    JFreeChart chart = ChartFactory.createScatterPlot(
        "Boys VS Girls weight comparison chart", 
        "X-Axis", "Y-Axis", dataset);
    
    //Changes background color
    XYPlot plot = (XYPlot)chart.getPlot();
    plot.setBackgroundPaint(new Color(255,228,196));
    
   
    // Create Panel
    ChartPanel panel = new ChartPanel(chart);
    setContentPane(panel);
  }
  private XYDataset createDataset() {
    XYSeriesCollection dataset = new XYSeriesCollection();
    //Boys (Age,weight) series
    XYSeries series1 = new XYSeries("Boys");
    series1.add(1, 72.9);
    series1.add(2, 81.6);
    series1.add(3, 88.9);
    series1.add(4, 96);
    series1.add(5, 102.1);
    series1.add(6, 108.5);
    series1.add(7, 113.9);
    series1.add(8, 119.3);
    series1.add(9, 123.8);
    series1.add(10, 124.4);
    dataset.addSeries(series1);
    
   //Girls (Age,weight) series
    XYSeries series2 = new XYSeries("Girls");
    series2.add(1, 72.5);
    series2.add(2, 80.1);
    series2.add(3, 87.2);
    series2.add(4, 94.5);
    series2.add(5, 101.4);
    series2.add(6, 107.4);
    series2.add(7, 112.8);
    series2.add(8, 118.2);
    series2.add(9, 122.9);
    series2.add(10, 123.4);
    dataset.addSeries(series2);
    return dataset;
  }
  public static void main(String[] args) {
    SwingUtilities.invokeLater(()-> {
      ScatterPlotExample example = new ScatterPlotExample("Scatter Chart Example");
      example.setSize(800, 400);
      example.setLocationRelativeTo(null);
      example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      example.setVisible(true);
    });
  }
}
    
输出:
男孩与女孩的体重对比图
昵称: 邮箱:
Copyright © 2020 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4