如何在 JFreeChart 散点图中创建空心形状

How to create hollow shapes in JFreeChart Scatter Plot

使用的 JFreeChart 版本:1.5.0

我尝试了以下方法来获得散点图中的空心形状:

PlotFrame.java 文件内容:

package javaapplication1;


import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author tarik
 */
public class PlotFrame extends javax.swing.JFrame {    
    public static void main(String[] argv) {
        PlotFrame plotFrame = new PlotFrame();
        plotFrame.setVisible(true);
    }


    /**
     * Creates new form PlotFrame
     */
    public PlotFrame() {    
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JFreeChart chart = createChart();    
        ChartPanel panel = new ChartPanel(chart);

        this.add(panel);
        pack();
    }

    private JFreeChart createChart() {
        // Create dataset  
        XYDataset dataset = createDataset();

        // Create chart  
        JFreeChart chart = ChartFactory.createScatterPlot(
                "Boys VS Girls weight comparison chart",
                "X-Axis", "Y-Axis", dataset);

        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer();
        //renderer.setBaseShapesFilled(false); 
        renderer.setUseFillPaint(false);

        return chart;
    }

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

没用,形状还在填充。

来自源代码:

 if (getItemShapeFilled(series, item)) {
                if (this.useFillPaint) {
                    g2.setPaint(getItemFillPaint(series, item));
                }
                else {
                    g2.setPaint(getItemPaint(series, item));
                }
                g2.fill(shape);
   }

对于第一个 if 没有别的,所以我们需要将 getItemShapeFilled 设置为 return false。它看起来像这样:

      public boolean getItemShapeFilled(int series, int item) {
            Boolean flag = getSeriesShapesFilled(series);
            if (flag != null) {
                return flag;
            }
            return this.baseShapesFilled;
      }

最简单的方法就是使用这个方法:

       public void setBaseShapesFilled(boolean flag) {
              this.baseShapesFilled = flag;
              fireChangeEvent();
        }

所以实际代码是:

import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author tarik
 */
public class PlotFrame extends javax.swing.JFrame {    
    public static void main(String[] argv) {
        PlotFrame plotFrame = new PlotFrame();
        plotFrame.setVisible(true);
    }


    /**
     * Creates new form PlotFrame
     */
    public PlotFrame() {    
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JFreeChart chart = createChart();    
        ChartPanel panel = new ChartPanel(chart);

        this.add(panel);
        pack();
    }

    private JFreeChart createChart() {
        // Create dataset  
        XYDataset dataset = createDataset();

        // Create chart  
        JFreeChart chart = ChartFactory.createScatterPlot(
                "Boys VS Girls weight comparison chart",
                "X-Axis", "Y-Axis", dataset);

        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer();
        renderer.setBaseShapesFilled(false); 
        renderer.setUseFillPaint(false);

        return chart;
    }

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

我的结果是:

你不是这个意思吗?

回想一下,Graphics2D 可以 draw() Shape 的轮廓以及 fill() 它的内部。通过将渲染限制为 draw(),@micro 的 is sound in this regard, but it assumes version 1.0.19. When migrating 为 1.5 及更高版本,请注意 "many methods getBaseXXX()/setBaseXXX() have been renamed setDefaultXXX()/getDefaultXXX()." 正确的公式如下所示:

renderer.setDefaultShapesFilled(false); 
renderer.setUseFillPaint(false);

其他变化:

  • event dispatch thread.

  • 构建和操作 Swing GUI 对象
  • 覆盖 getPreferredSize() 以设置图表的初始首选大小。

经测试:

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see 
 * @author trashgod
 * @author tarik
 */
public class PlotFrame extends javax.swing.JFrame {    
    public static void main(String[] argv) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                PlotFrame plotFrame = new PlotFrame();
                plotFrame.setLocationRelativeTo(null);
                plotFrame.setVisible(true);
            }
        });
    }

    /**
     * Creates new form PlotFrame
     */
    public PlotFrame() {    
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFreeChart chart = createChart();    
        ChartPanel panel = new ChartPanel(chart){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        this.add(panel);
        this.pack();
    }

    private JFreeChart createChart() {
        // Create dataset  
        XYDataset dataset = createDataset();

        // Create chart  
        JFreeChart chart = ChartFactory.createScatterPlot(
                "Boys VS Girls weight comparison chart",
                "X-Axis", "Y-Axis", dataset);

        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer();
        renderer.setDefaultShapesFilled(false); 
        renderer.setUseFillPaint(false);

        return chart;
    }

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