Is it possible to mark a visible point on a graph?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

Is it possible to mark a visible point on a graph?

Post by deepujain » Tue Mar 25, 2008 12:01 pm

Hi,
I have a time series graph , I want to highlight a particular point in the graph which will indicate the peak value in the graph , Is it possible to highlight a particular point say with green circle?
Please help.
Thanks
Deepak

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Tue Mar 25, 2008 1:39 pm

Add one of these to your plot

Code: Select all

org.jfree.chart.annotations.XYShapeAnnotation

deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

Post by deepujain » Tue Mar 25, 2008 5:37 pm

Thanks it was a nice pointer , Though i had to do a lot of google after the start you gave.
Here is what worked for me
1) Write a custom class
class CircleDrawer implements Drawable {

/** The outline paint. */
private Paint outlinePaint;

/** The outline stroke. */
private Stroke outlineStroke;

/** The fill paint. */
private Paint fillPaint;

/**
* @param outlinePaint the outline paint.
* @param outlineStroke the outline stroke.
* @param fillPaint the fill paint.
*/
public CircleDrawer(final Paint outlinePaint,
final Stroke outlineStroke,
final Paint fillPaint) {
this.outlinePaint = outlinePaint;
this.outlineStroke = outlineStroke;
this.fillPaint = fillPaint;
}

/**
* Draws the circle.
*
* @param g2 the graphics device.
* @param area the area in which to draw.
*/
public void draw(final Graphics2D g2, final Rectangle2D area) {
final Ellipse2D circle = new Ellipse2D.Double(area.getX(), area.getY(),
area.getWidth(), area.getHeight());
if (fillPaint != null) {
g2.setPaint(fillPaint);
g2.fill(circle);
}
if (this.outlinePaint != null && this.outlineStroke != null) {
g2.setStroke(outlineStroke);
g2.setPaint(outlinePaint);
g2.draw(circle);
}
}
}

2) Add the custom drawer as a annotation to XYPlot
final CircleDrawer cd = new CircleDrawer(Color.red, new BasicStroke(1.0f), Color.red);
final XYAnnotation point = new XYDrawableAnnotation(<VALUE_IN_XAXIS>,<VALUE_IN_YAXIS>, 5, 5, cd);
(XYPlot) chart.getPlot().addAnnotation(point);

Hope this will help others as well.
Thanks
Deepak
Zzzzz....

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Tue Mar 25, 2008 6:53 pm

Did you try this?

Code: Select all

chart.getXYPlot().addAnnotation(new XYShapeAnnotation(new Ellipse2D.Double(...)...));

deepujain
Posts: 21
Joined: Sat Jun 02, 2007 10:52 am
Location: Bangalore

Post by deepujain » Wed Mar 26, 2008 7:46 am

Yes i did. My problem was that i want a specifc number of point[shape] and not a shape on each data point in data set. Adding XYShapeAnnotation will mark each data point in the dataset.
Hope i used was correct. It did work for me.
Zzzzz....

Locked