how to implement selecting one line(series) in line chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
xtsu
Posts: 4
Joined: Sun Aug 12, 2007 4:09 am

how to implement selecting one line(series) in line chart

Post by xtsu » Thu Jan 14, 2010 4:30 am

Hi,
I am question about how to implement a function for selecting one line(series) in line chart.

specifically, if I have a line chart, there are several lines in the chart, how can I implement selection of one line(series)
by double(single) click mouse. And then the selected line is highlighted all.

Appreciate for any comments!!

Tain

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: how to implement selecting one line(series) in line chart

Post by MitchBuell » Thu Jan 14, 2010 4:39 pm

There is no highlighting functionality built in that I know of, but you can simulate a highlight by changing the line color and line thickness on a mouse click. The only downside is that the user must click on a datapoint, a click on the line between datapoints wont register.

Code: Select all

chartPanel.addChartMouseListener(new ChartMouseListener()
    	{
    		public void chartMouseClicked(ChartMouseEvent event)
    		{
    			ChartEntity entity = event.getEntity();
    			if (entity instanceof PlotEntity)
    			{
    				// User clicked an empty space on the plot so clear all highlights.
    				
    				// This requires some logic to restore the original series colors and thickness,
    				// such as using a Vector to store the original values and reapplying them here.
    				// Here I am just reverting the first series back to red and normal thickness.
    				chartPanel.getChart().getXYPlot().getRenderer().setSeriesPaint(0, Color.RED);
    				chartPanel.getChart().getXYPlot().getRenderer().setSeriesStroke(0, new BasicStroke(1.0f));
    			}
    			if (entity instanceof XYItemEntity)
    			{
    				// User clicked on an XYDataset
    				XYItemEntity xyEntity = (XYItemEntity) entity;
    				int seriesIndex = xyEntity.getSeriesIndex();

    				// Here I am setting the clicked series to red and increasing the line thickness.
    				chartPanel.getChart().getXYPlot().getRenderer().setSeriesPaint(seriesIndex, Color.BLUE);
    				chartPanel.getChart().getXYPlot().getRenderer().setSeriesStroke(seriesIndex, new BasicStroke(2.0f));
    			}
    		}

    		public void chartMouseMoved(ChartMouseEvent event) {}
    	});

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: how to implement selecting one line(series) in line chart

Post by paradoxoff » Thu Jan 14, 2010 6:16 pm

A "selection mechanism" for individual data points has been requested before: see here.
Maybe you can get some additional hints on how to implement a "selection aware" renderer.

xtsu
Posts: 4
Joined: Sun Aug 12, 2007 4:09 am

Re: how to implement selecting one line(series) in line chart

Post by xtsu » Fri Jan 15, 2010 2:24 am

Appreciate it!!!

Tain

Locked