我正在使用Swing和JFreeChart编写一个小应用程序。我必须显示一个XYLineChart
,并且要在其上绘制一些填充的矩形。我已经使用XYShapeAnnotation
绘制了矩形,并且尝试用Graphics2D
填充矩形,但这是行不通的。我在图表上显示了矩形,但未填充。代码如下:
Shape rectangle = new Rectangle2D.Double(0, 0, 7, 1);
g2.fill(rectangle);
XYShapeAnnotation shapeAnnotation = new XYShapeAnnotation(rectangle, new BasicStroke(2.f), Color.BLACK);
shapeAnnotation.setToolTipText("1");
plot.addAnnotation(shapeAnnotation);
我认为问题在于填充的矩形位置与图表无关,但我真的不知道如何解决。我还想知道是否可以在图表中的矩形上方显示线条,因为我找不到任何方法。
答案 0 :(得分:1)
使用XYShapeAnnotation
构造函数,该构造函数允许您同时指定和 outlinePaint
和 fillPaint
。您可能想要这样的东西:
XYShapeAnnotation shapeAnnotation = new XYShapeAnnotation(
rectangle, new BasicStroke(2.f), Color.BLACK, Color.BLACK);
作为基于此answer的具体示例,以下更改产生了显示的结果:
renderer.addAnnotation(new XYShapeAnnotation(ellipse, stroke, color, color));
要在图表上方上的矩形中显示线条,请指定注释的背景层,如here所示。
renderer.addAnnotation(new XYShapeAnnotation(
ellipse, stroke, color, color), Layer.BACKGROUND);