Access chart data with mouse click

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gecong
Posts: 1
Joined: Thu Jun 06, 2019 8:56 pm
antibot: No, of course not.

Access chart data with mouse click

Post by gecong » Thu Jun 06, 2019 9:52 pm

Hi, I used the following code to display a heatmap chart. Each data point has three values, x, y, and z. x, y is the coordinate, z is the "heat". For each data point, I also have a string "labe" associated with it. When the chart is shown, I would like the user to click on the chart, and display the "lable" associated with the data point where user clicked. But I do not know how to write the code. Can anyone help?

Code: Select all


       double[][] data= new double[3][];
	data[0] = new double[1000];
	data[1] = new double[1000];
	data[2] = new double[1000];
	String label[1000] = {"xxx", "yyy", ...};
	data[0][] = {....};
	data[1][] = {....};
	data[2][] = {....};
	
	DefaultXYZDataset dataset = new DefaultXYZDataset();
		dataset.addSeries("data", data);
		JFrame frame = new JFrame("gmapping");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ChartPanel chartPanel = new ChartPanel(createChart(dataset, title, yAxisString, minValue, maxValue)) {
        	private static final long serialVersionUID = 1L;
			public Dimension getPreferredSize() {
                return new Dimension(1600, 1600);
            }
        };
        
        chartPanel.addChartMouseListener(new ChartMouseListener() {
			
			@Override
			public void chartMouseMoved(ChartMouseEvent event) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void chartMouseClicked(ChartMouseEvent event) {
				
				//display the correct label...
			}
		});
        
        chartPanel.setMouseZoomable(true, false);
        frame.add(chartPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static JFreeChart createChart(XYDataset dataset, String title, String valueString, double lo, double hi) {
        NumberAxis xAxis = new NumberAxis("x");
        NumberAxis yAxis = new NumberAxis("y");
        XYPlot plot = new XYPlot(dataset, xAxis, yAxis, null);
        XYBlockRenderer r = new XYBlockRenderer();
        SpectrumPaintScale ps = new SpectrumPaintScale(lo, hi);
        r.setPaintScale(ps);
        r.setBlockHeight(10.0f);
        r.setBlockWidth(10.0f);
        plot.setRenderer(r);
        JFreeChart chart = new JFreeChart(title,
            JFreeChart.DEFAULT_TITLE_FONT, plot, false);
        NumberAxis scaleAxis = new NumberAxis(valueString);
        scaleAxis.setAxisLinePaint(Color.white);
        scaleAxis.setTickMarkPaint(Color.white);
        PaintScaleLegend legend = new PaintScaleLegend(ps, scaleAxis);
        legend.setSubdivisionCount(128);
        legend.setAxisLocation(AxisLocation.TOP_OR_RIGHT);
        legend.setPadding(new RectangleInsets(10, 10, 10, 10));
        legend.setStripWidth(20);
        legend.setPosition(RectangleEdge.RIGHT);
        legend.setBackgroundPaint(Color.WHITE);
        chart.addSubtitle(legend);
        chart.setBackgroundPaint(Color.white);
        
        
        return chart;
    }
    

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Access chart data with mouse click

Post by John Matthews » Fri Jun 14, 2019 9:42 pm

It looks like you're working from the examples examined here. Your listener can use the series index or item number to index your label array:

Code: Select all

if (e.getEntity() instanceof XYItemEntity) {
	XYItemEntity i = (XYItemEntity) e.getEntity();
	System.out.println(i.getSeriesIndex() + ":" + i.getItem());
}
Alternatively, extend AbstractXYZDataset to include the labels. Use the XYItemEntity method getDataset() to access the relevant label in your listener.

Locked