I plotted two data series onto one XYPlot. Since their data ranges were different, I used two range axes on both sides. Now I am trying to add XYShapeAnnotation to some points on both series. The sizes for those shape annotations look consistent within each series, but different between the two. I think they are scaled according to their own range axes. How do I get a fixed-size shape annotation on both series?
This is the code I use to create annotations:
XYShapeAnnotation xys = new XYShapeAnnotation(
new Ellipse2D.Double(x, y, 0.2f, 0.2f),
new BasicStroke(1.0f),
Color.red);
plot.addAnnotation(xys);
Thanks,
Sandy
How to create fixed-size shape annotation?
If you want a point [shape] on each point on the graph then use what you have done ,But if you want only one/two... points then try the following
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);
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);
Zzzzz....