2

Is it possible to add GUID to each point on scatter plot so when user will click the point I can handle the GUID and retrieve some information?

Edited: add sample:

package demo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.entity.XYItemEntity;
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;

public class ScatterMouseClick extends JFrame {

    private static final long serialVersionUID = 1L;
    private static final int N = 24;
    private static final Random rand = new Random(System.currentTimeMillis());
    private final XYSeries series = new XYSeries("Scatter Series");

    public ScatterMouseClick(String s) {
        super(s);
        final ChartPanel chartPanel = createDemoPanel();
        this.add(chartPanel, BorderLayout.CENTER);
    }

    private ChartPanel createDemoPanel() {
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            "Scatter Mouse click Demo", "X", "Y", createSampleData(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        adjustAxis((NumberAxis) xyPlot.getDomainAxis(), true);
        adjustAxis((NumberAxis) xyPlot.getRangeAxis(), false);
        xyPlot.setBackgroundPaint(Color.white);
        ChartPanel chartPanel = new ChartPanel(jfreechart);
        chartPanel.addChartMouseListener(new ChartMouseListener() {
            public void chartMouseClicked(ChartMouseEvent e) {
                if (e.getEntity() instanceof XYItemEntity) {
                    XYItemEntity item = (XYItemEntity) e.getEntity();
                    System.out.println("You clicked at point of series:["
                        + item.getSeriesIndex() + "] at location: ["
                        + item.getItem() + "] with UTL Text: "
                        + item.getURLText());
                }
            }

            public void chartMouseMoved(ChartMouseEvent e) {
            }
        });
        return chartPanel;
    }

    private void adjustAxis(NumberAxis axis, boolean vertical) {
        axis.setRange(-3.0, 3.0);
        axis.setTickUnit(new NumberTickUnit(0.5));
        axis.setVerticalTickLabels(vertical);
    }

    private XYDataset createSampleData() {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        for (int i = 0; i < N * N; i++) {
            //TODO HOW ADD URL text information here
            series.add(rand.nextGaussian(), rand.nextGaussian());
        }
        xySeriesCollection.addSeries(series);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ScatterMouseClick demo = new ScatterMouseClick("Scatter Mouse click Demo");
                demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                demo.pack();
                demo.setLocationRelativeTo(null);
                demo.setVisible(true);
            }
        });
    }
}

this is another post on how to display point information

Community
  • 1
  • 1
JavaSheriff
  • 7,074
  • 20
  • 89
  • 159

1 Answers1

1

Use a ChartMouseListener, as shown here. You can examine the ChartEntity as needed. If you enable URL generation in your ChartFactory, getURLText() may be convenient.

Addendum: The simplest modification to generates URLs with your example is to enable them in the ChartFactory by setting the final parameter, urls, to true:

JFreeChart jfreechart = ChartFactory.createScatterPlot(
    "Scatter Mouse click Demo", "X", "Y", createSampleData(),
    PlotOrientation.VERTICAL, true, true, true);

You can see how the factory adds the generator to the renderer here. You can specify alternate constructor parameters, or you can override the generateURL() method to customize the result.

Console:

You clicked at point of series:[0] at location: [0]
    with URL Text: index.html?series=0&item=0
You clicked at point of series:[0] at location: [1]
    with URL Text: index.html?series=0&item=1
You clicked at point of series:[0] at location: [2]
    with URL Text: index.html?series=0&item=2

Addendum: Based on a now deleted comment, note that getURLText() retrieves only what the generator puts there. Alternatively, note that XYItemEntity provides complete access to the parent XYDataset, although you may have to cast it to your subclass to retrieve specific data.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I have code to get the ChartEntity But How do you extract the GUID form it? – JavaSheriff Apr 03 '13 at 17:22
  • The `ChartEntity` can access the URL text for the entity, where your `XYURLGenerator` left it. – trashgod Apr 03 '13 at 19:10
  • I see this demo doing something similar http://ww.nothingbutclouds.com/websvn/filedetails.php?repname=repo&path=/JFreeChartDemo/src/demo/ScatterPlotDemo3.java&peg=211 but i am still unable to figure out how to extract the tool tip or URL text... if you can help with piece of code - that will be great – JavaSheriff Apr 05 '13 at 20:59
  • Please edit your question to include an [sscce](http://sscce.org/) that shows your `ChartMouseListener ` & `XYURLGenerator`. – trashgod Apr 06 '13 at 09:10
  • edited to add sscce, there is a system out to print the URL text, – JavaSheriff Apr 08 '13 at 13:58
  • I saw your down-vote, but you deleted your comment, so I;m not sure what to suggest. I've elaborated above, in case you change your mind. – trashgod Apr 09 '13 at 15:30
  • question is very simple, I am still looking for solution and didn't find one.. how do i add hash code or GUID to each scatter point and get the hash or GUID when i click the scatter point, Sorry for my ignorance but I don't see how to do it using the URL code. – JavaSheriff Apr 17 '13 at 20:59
  • See how the standard generator returns the series and item numbers? If you add your own, custom generator with a `generateURL()` method that returns something else, the `ChartMouseListener` will then see the new value. Here's an [example](http://stackoverflow.com/a/6295405/230513). If you've changed your mind, you can click the orange button to reverse the down-vote. – trashgod Apr 18 '13 at 01:23