Linechart with mouse-draggable areas

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
liferiot
Posts: 6
Joined: Thu Feb 18, 2010 1:44 pm
antibot: No, of course not.

Linechart with mouse-draggable areas

Post by liferiot » Mon May 30, 2011 10:52 am

Hi!

i would like to build a normal line chart with areas that can be dragged with the mouse.
the areas should be represented by a bar and a line for separation. with the mouse over the line o the area separator there should appear a left/right mouse curser. clicking and dragging to the side should move the line and the area/bar.
how can i do that with jfreechart?


Image
Uploaded with ImageShack.us

liferiot
Posts: 6
Joined: Thu Feb 18, 2010 1:44 pm
antibot: No, of course not.

Re: Linechart with mouse-draggable areas

Post by liferiot » Wed Jun 15, 2011 3:25 pm

ok, found the solution by myself - but it's a little bit tricky:

- for the color fields i just added a XYAreaRenderer

- the draggable lines were not very easy:
first of all you need a entity for a mouse-listener. that's why a line chart with two points and a line inbetween doesn't work.
two things work but both arent perfect: a) a slim area-chart works fine but you can't specify a specific line-width. you need to specify 2 x-valuues here. if you take a %age from the x-range the line width variies by zooming in and out.
b) a fixed line with can bre realized by a simple point with a very big shape. that works for me - however a shape height (given in pixels) is a dirty implementation (i just added a 2000px shape height)

then i realized that the ChartMouseListener only uses mousemoved and mouseclicked, but not mouse dragged.
so i decided to use the standard mouselistener and translate the coordinates:

Code: Select all

public void mouseDragged(MouseEvent e) {	
		if (!canDrag)
			return;
		Point2D p = e.getPoint();
		Rectangle2D plotArea = getScreenDataArea();
		XYPlot plot = (XYPlot) this.getChart().getPlot();
		double xValue = plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge());
...
}
I check the xValue with the values in my data-model which are used to fill the chartmodel/the vertical lines and alter the mouse-cursor, implement the dragging etc.

the good thing now is: i dont need an entity for the mouselistener. so i switched back to a line with two points to draw the vertical lines,
due to not needing an entity any more you could also youse ValueMarker

so i actually go by coordinates and not by object but that makes it a lot more easy in this case.
the only problem here is that you need to specify a range for hte mouseover (can drag flag). but using 1% o the range ireally like the look%feel making it easy to "grab" the lines

felbabz
Posts: 3
Joined: Sun Apr 08, 2012 5:56 pm
antibot: No, of course not.

Re: Linechart with mouse-draggable areas

Post by felbabz » Sun Apr 08, 2012 6:24 pm

Hey Liferiot.
Would you mind sending me your sourcecode please.
I am working on a similar project

Thanks in advance

Locked