I am using Jfreechart to draw x-y axis I want to draw a rectangle and a circle on this x-y axis. Do you know how is it possible? Is there any better way(rather than jfreechart) to draw x-y axis and its shapes on it?
Asked
Active
Viewed 3,169 times
1
-
Do you mean to highlight something on the chart? – trashgod Jul 06 '11 at 22:09
-
I want to draw a circle and a rectangle on a chart. How should I do it? – SunyGirl Jul 07 '11 at 16:32
-
Do you meant you want to construct a data set whose points form a circle and a square in two dimensions? – trashgod Jul 07 '11 at 17:32
-
Yes exactly. But I try different ways like creating some series but it does not work! – SunyGirl Jul 07 '11 at 20:12
-
"When you have decided which answer is the most helpful to you, mark it as the accepted answer by clicking on the check box outline to the left of the answer."—[faq]. Also, look at some of the examples mentioned [here](http://stackoverflow.com/tags/jfreechart/info) to get started. – trashgod Jul 07 '11 at 22:58
-
1As a starting point, you might look at this [answer](http://stackoverflow.com/questions/6604211/jfreechart-draw-arc-on-chart/6620017#6620017) and the comment about `Shape`. – trashgod Jul 08 '11 at 20:04
1 Answers
0
I draw circles or other items onto an XY chart chart with the annotations capability. To add a circle use "addAnnotation(XYShapeAnnotation(new Ellipse2D.Double(...)))" for example. Here's an example method:
public void markChartAt2( float fromX, float fromY, float toX, float toY, markType markerTyp, Color lineColor, String toolTip ) {
XYLineAnnotation lineAnnotation;
XYShapeAnnotation shapeAnnotation;
if (lineColor == null)
lineColor = Color.black;
maxY = yAxis.getUpperBound();
minY = yAxis.getLowerBound();
yFactor = (maxY - minY) * .005d;
maxX = xAxis.getUpperBound();
minX = xAxis.getLowerBound();
xFactor = (maxX - minX) * .0005d;
switch ( markerTyp ){
case CIRCLE:
shapeAnnotation = new XYShapeAnnotation(
new Ellipse2D.Double( fromX - xFactor, fromY - yFactor, xFactor+xFactor, yFactor+yFactor ),
markerLineWidth, lineColor ) ;
shapeAnnotation.setToolTipText( toolTip );
priceXYPlot.addAnnotation(shapeAnnotation);
break;
case SQUARE:
shapeAnnotation = new XYShapeAnnotation(
new Rectangle2D.Double( fromX - xFactor, fromY - yFactor, xFactor+xFactor, yFactor+yFactor ),
markerLineWidth, lineColor );
shapeAnnotation.setToolTipText( toolTip );
priceXYPlot.addAnnotation( shapeAnnotation );
break;
}
}

james_t
- 255
- 1
- 10
-
You should add an explanation to your code example that explains how it helps solve the OP's problem. – moggi Sep 06 '17 at 19:16
-
Updated my answer to explain how to draw a rectangle or a circle through the addAnnotation() method – james_t Sep 07 '17 at 17:57