Changing the shapes of points in scatter plot
Asked Answered
E

2

13

I have thousands of points to Plot on a JFreeChart scatter plot. The problem right now is that my program is plotting points with "squares", but I need some help on how to change the Shape of points from "squares" to "dots/circles". Any help would be appreciated.

//*I am using ShapeUtilities,but its not changing the shape of point to "DaigonalCross" when I am Using for XYItemRenderer/XYDotRenderer--Any corrections please if anythng wrong in the code ..*///

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.util.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.ShapeUtilities;

public class SPlotfinal extends ApplicationFrame {

    public SPlotfinal(String s) {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    public static JPanel createDemoPanel() {

        JFreeChart jfreechart = ChartFactory.createScatterPlot("Scatter Plot Demo",
            "X", "Y", samplexydataset2(), PlotOrientation.VERTICAL, true, true, false);
        Shape cross = ShapeUtilities.createDiagonalCross(3, 1);

        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setBaseShape(cross);
        renderer.setBasePaint(Color.red);
        //changing the Renderer to XYDotRenderer
        //xyPlot.setRenderer(new XYDotRenderer());
        XYDotRenderer xydotrenderer = new XYDotRenderer();
        xyPlot.setRenderer(xydotrenderer);
        xydotrenderer.setSeriesShape(0, cross);

        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);

        return new ChartPanel(jfreechart);
    }

    private static XYDataset samplexydataset2() {
        int cols = 20;
        int rows = 20;
        double[][] values = new double[cols][rows];

        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("Random");
        Random rand = new Random();
        for (int i = 0; i < values.length; i++) {
            for (int j = 0; j < values[i].length; j++) {
                double x = Math.round(rand.nextDouble() * 500);
                double y = Math.round(rand.nextDouble() * 500);

                series.add(x, y);
            }
        }
        xySeriesCollection.addSeries(series);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        SPlotfinal scatterplotdemo4 = new SPlotfinal("Scatter Plot Demo 4");
        scatterplotdemo4.pack();
        RefineryUtilities.centerFrameOnScreen(scatterplotdemo4);
        scatterplotdemo4.setVisible(true);
    }
}
Erastus answered 12/7, 2011 at 13:57 Comment(1)
+1 for sscce.Berryberryhill
B
20

A ScatterRenderer inherits getItemShape() from AbstractRenderer. You can override getItemShape() to supply your own shapes.

Addendum: One advantage to this approach is that you can specify a Shape for each item in each series.

Addendum: To use ShapeUtilities.createDiagonalCross(), do something like this:

Shape cross = ShapeUtilities.createDiagonalCross(3, 1);
plot = (XYPlot) chart.getPlot();
renderer = plot.getRenderer();
renderer.setSeriesShape(0, cross);

Addendum: Just switch to setSeriesShape(). Also, skip the XYDotRenderer and Math.round().

Scatter Plot Demo

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.util.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.ShapeUtilities;

public class SPlotfinal extends ApplicationFrame {

    public SPlotfinal(String s) {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(640, 480));
        add(jpanel);
    }

    public static JPanel createDemoPanel() {
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            "Scatter Plot Demo", "X", "Y", samplexydataset2(),
            PlotOrientation.VERTICAL, true, true, false);
        Shape cross = ShapeUtilities.createDiagonalCross(3, 1);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesShape(0, cross);
        renderer.setSeriesPaint(0, Color.red);
        return new ChartPanel(jfreechart);
    }

    private static XYDataset samplexydataset2() {
        int cols = 20;
        int rows = 20;
        double[][] values = new double[cols][rows];
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("Random");
        Random rand = new Random();
        for (int i = 0; i < values.length; i++) {
            for (int j = 0; j < values[i].length; j++) {
                double x = rand.nextGaussian();
                double y = rand.nextGaussian();
                series.add(x, y);
            }
        }
        xySeriesCollection.addSeries(series);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        SPlotfinal scatterplotdemo4 = new SPlotfinal("Scatter Plot Demo 4");
        scatterplotdemo4.pack();
        RefineryUtilities.centerFrameOnScreen(scatterplotdemo4);
        scatterplotdemo4.setVisible(true);
    }
}
Berryberryhill answered 12/7, 2011 at 18:56 Comment(7)
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE is handy, too.Berryberryhill
Can You Please post the Code of above "ScatterPlotDemo" ,That is what I am exactly Looking for....ThanksErastus
Too long; I've adapted your example, above.Berryberryhill
Great....Thanks Very Much for Editing the code;)..If Possible can u please post the Code of Previous Diagram-"ScatterPlotDemo" which was having all the shapes..ThanksErastus
The extra shapes are supplied automatically by the DefaultDrawingSupplier, mentioned above, each time you add another series to your XYSeriesCollection. Please consider accepting this answer by clicking on the empty check mark on the left.Berryberryhill
@Berryberryhill I am using Shape cross = ShapeUtilities.createRegularCross(2, 1); plot.getRenderer().setSeriesShape(0, cross); for a LineChart. Could you please let me know how can I draw a circle and also how do I change the color of the shape only instead of the line. I had asked this question: #14953212Amputee
See also this example that overrides getItemShape().Berryberryhill
S
7

You can get the plot (for this example its a XYPlot) and then the renderer and change the base shape like this:

// 5x5 red pixel circle
Shape shape  = new Ellipse2D.Double(0,0,5,5);
XYPlot xyPlot = (XYPlot) jFreeCHart.getPlot();
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setBaseShape(shape);
renderer.setBasePaint(Color.red);

// set only shape of series with index i
renderer.setSeriesShape(i, shape);

You can change much more things, like outline paint/stroke

Stoddard answered 12/7, 2011 at 20:8 Comment(3)
Sadly, this won't work, as setBaseShape() and setSeriesShape() appear unimplemented.Berryberryhill
Fortunately, there is usable setSeriesShape() in the base class, AbstractRenderer.Berryberryhill
I tried with ur code it worked...I have other question.I have a method createDiagonalCross(float l, float t) in "Class ShapeUtilities" which Creates a diagonal cross shape for points,how can i use this method with the above code..need help regarding this..ThanksErastus

© 2022 - 2024 — McMap. All rights reserved.