Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6527947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:29:21+00:00 2026-05-25T09:29:21+00:00

I have array of elements and they belong to one Series,with these elements I

  • 0

I have array of elements and they belong to one Series,with these elements I calculated Centroids. Problem is when I display them with “ScatterPlot” I need to show “Array Elements” with “One Color” and the Centroid of these points in “Different Color”.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;

public class Scatteradd extends JFrame {

    int i, x = 0, n1 = 0;

    public Scatteradd(String title, final double[][] samples) {
        super(title);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final DefaultXYDataset dataset = new DefaultXYDataset();
        dataset.addSeries("Series0", createSeries(0, samples));
        //dataset.addSeries("Series1", createSeries(1,trainingset3));
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(640, 480));
        this.add(chartPanel, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel();
        JButton addButton = new JButton("Add Series");

        buttonPanel.add(addButton);
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int n = dataset.getSeriesCount();
                System.out.println("N-SIZE" + n);
                dataset.addSeries("Series" + n, createSeries(n, samples));
                System.exit(1);
            }
        });
        JButton remButton = new JButton("Remove Series");
        buttonPanel.add(remButton);
        remButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int n = dataset.getSeriesCount() - 1;
                dataset.removeSeries("Series" + n);
            }
        });
        this.add(buttonPanel, BorderLayout.SOUTH);
    }

    /**
     * Create a series
     * @param samples 
     * 
     * @ return the series
     */
    private double[][] createSeries(int mean, double[][] samples) {

        double[][] series = new double[2][samples.length + 1];

        System.out.println("SSSKSKSValue" + series.length);
        double p = 0, q = 0;
        for (i = 0; i < samples.length; i++) {

            series[0][i] = samples[i][0];
            p = p + samples[i][0];
            series[1][i] = samples[i][1];
            q = q + samples[i][1];
            //System.out.println("Series Values"+series[0][i]+","+series[1][i]);
        }


        series[0][samples.length] = p / samples.length;//Centroid Calculation
        series[1][samples.length] = q / samples.length;//Centroid Calculation
        //Printing All Points in Series Array and the Last Row is the Centroid Values
        //which I want display in different Color on Scatter Plot
        for (int v = 0; v < series[0].length; v++) {
            System.out.println("Series Values" + series[0][v] + "," + series[1][v]);
        }

        return series;

    }

    private JFreeChart createChart(XYDataset dataset) {

        // create the chart...
        JFreeChart chart = ChartFactory.createScatterPlot(
            "Scatter Plot Demo", "X", "Y", dataset,
            PlotOrientation.VERTICAL, true, true, false);

        // set chart background
        chart.setBackgroundPaint(Color.white);

        // set a few custom plot features
        XYPlot plot = (XYPlot) chart.getPlot();
        Shape[] cross = DefaultDrawingSupplier.createStandardSeriesShapes();
        plot.setBackgroundPaint(new Color(0xffffe0));
        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.lightGray);
        XYItemRenderer renderer = (XYItemRenderer) plot.getRenderer();
        renderer.setSeriesShape(0, cross[0]);
        plot.setRenderer(renderer);

        return chart;
    }

    /** Main method **/
    public static void main(String[] args) {

        double[][] trainingset3 = {
            {0.428053, 0.409742,},
            {0.415487, 0.401414,},
            {0.404834, 0.400493,},};
        Scatteradd demo = new Scatteradd("JFreeChartDemo", trainingset3);

        demo.pack();
        demo.setLocationRelativeTo(null);
        demo.setVisible(true);

    }
}
SSSKSKSValue2
Series Values0.428053,0.409742
Series Values0.415487,0.401414
Series Values0.404834,0.400493
Series Values0.4161246666666667,0.403883
//Centroids of above 3 Rows

Is there any method to show particular row in a series with different Color,Any example provided regarding this would be great and helpful.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:29:21+00:00Added an answer on May 25, 2026 at 9:29 am

    You can override getItemPaint in the scatter plot’s XYLineAndShapeRenderer and choose your color based on any desired combination of row and col. There’s a related example here, although it’s for a different renderer.

    Addendum: The general idea appears in MyRenderer, which extends XYLineAndShapeRenderer.

    Scatter plot custom renderer image

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Paint;
    import java.awt.Shape;
    import java.util.Arrays;
    import javax.swing.JFrame;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.DefaultDrawingSupplier;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
    import org.jfree.data.xy.DefaultXYDataset;
    import org.jfree.data.xy.XYDataset;
    
    public class ScatterColors extends JFrame {
    
        private static final Color centroidColor = Color.blue;
        private int centroidColumn;
    
        public ScatterColors(String title, final double[][] samples) {
            super(title);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            DefaultXYDataset dataset = new DefaultXYDataset();
            dataset.addSeries("Series", createSeries(0, samples));
            JFreeChart chart = createChart(dataset);
            ChartPanel chartPanel = new ChartPanel(chart, false);
            chartPanel.setPreferredSize(new Dimension(500, 400));
            this.add(chartPanel, BorderLayout.CENTER);
        }
    
        private double[][] createSeries(int mean, double[][] samples) {
            centroidColumn = samples.length;
            double[][] series = new double[2][samples.length + 1];
            double p = 0, q = 0;
            for (int i = 0; i < samples.length; i++) {
                series[0][i] = samples[i][0];
                p = p + samples[i][0];
                series[1][i] = samples[i][1];
                q = q + samples[i][1];
            }
            series[0][samples.length] = p / samples.length;
            series[1][samples.length] = q / samples.length;
            for (int i = 0; i < series.length; i++) {
                System.out.println(Arrays.toString(series[i]));
            }
            return series;
        }
    
        private JFreeChart createChart(XYDataset dataset) {
            JFreeChart chart = ChartFactory.createScatterPlot(
                "Scatter Plot Demo", "X", "Y", dataset,
                PlotOrientation.VERTICAL, true, true, false);
            chart.setBackgroundPaint(Color.white);
            XYPlot plot = (XYPlot) chart.getPlot();
            Shape[] cross = DefaultDrawingSupplier.createStandardSeriesShapes();
            plot.setBackgroundPaint(new Color(0xffffe0));
            plot.setDomainGridlinesVisible(true);
            plot.setDomainGridlinePaint(Color.lightGray);
            plot.setRangeGridlinePaint(Color.lightGray);
            MyRenderer renderer = new MyRenderer(false, true);
            plot.setRenderer(renderer);
            renderer.setSeriesShape(0, cross[0]);
            plot.setRenderer(renderer);
            return chart;
        }
    
        private class MyRenderer extends XYLineAndShapeRenderer {
    
            public MyRenderer(boolean lines, boolean shapes) {
                super(lines, shapes);
            }
    
            @Override
            public Paint getItemPaint(int row, int col) {
                if (col == centroidColumn) {
                    return centroidColor;
                } else {
                    return super.getItemPaint(row, col);
                }
            }
        }
    
        public static void main(String[] args) {
            double[][] trainingSet = {
                {0.428053, 0.409742,},
                {0.415487, 0.401414,},
                {0.404834, 0.400493,},
            };
            ScatterColors demo = new ScatterColors("JFreeChartDemo", trainingSet);
            demo.pack();
            demo.setLocationRelativeTo(null);
            demo.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a fixed-size array of elements and to call on them
I have an array with 16 elements. I would like to evaluate these to
I have an array Items which has 10 elements. I need to remove the
I have an array which i need to sort its elements by occurrence then
I have an array of elements that I need to serialize using XmlSerializer. The
Hello i have set of elements. They have several classes on one element. <div
Consider I have an array of elements out of which I want to create
VISUAL C++ Question Hi, I have array of 3 elements and I want to
I have an array ( arr ) of elements, and a function ( f
An array is defined of assumed elements like I have array like String[] strArray

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.