Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create hollow shapes in JFreeChart Scatter Plot

JFreeChart version used: 1.5.0

I tried the following to obtain hollow shapes in a scatter plot:

PlotFrame.java file content:

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

It did not work, the shapes are still filled.

like image 854
Tarik Avatar asked Mar 03 '23 04:03

Tarik


2 Answers

Recall that Graphics2D can draw() the outline of a Shape as well as fill() its interior. By limiting rendering to draw(), @micro's approach is sound in this regard, but it assumes version 1.0.19. When migrating to version 1.5 and later, note that "many methods getBaseXXX()/setBaseXXX() have been renamed setDefaultXXX()/getDefaultXXX()." The correct formulation is illustrated below:

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

image

Other changes:

  • Construct and manipulate Swing GUI objects only on the event dispatch thread.

  • Override getPreferredSize() to set the chart's initial preferred size.

As tested:

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 https://stackoverflow.com/q/58812592/230513
 * @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;
    }
}
like image 71
trashgod Avatar answered Apr 29 '23 01:04

trashgod


From source code:

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

For the first if there is no else, so we need the getItemShapeFilled to return false. It looks like this:

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

Easiest way is to use this method:

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

So the actual code is:

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

My result is:

enter image description here

Isn't that what you meant?

like image 35
mico Avatar answered Apr 29 '23 00:04

mico