【问题标题】:How can I draw a filled rectangle in JFreeChart?如何在 JFreeChart 中绘制填充矩形?
【发布时间】:2019-02-06 10:04:04
【问题描述】:

我正在使用 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);

我认为问题在于填充的矩形位置与图表无关,但我真的不知道如何解决这个问题。我还想知道是否可以在图表中的矩形上方显示线条,因为我没有找到任何方法。

【问题讨论】:

    标签: java swing jfreechart


    【解决方案1】:

    使用XYShapeAnnotation 构造函数,它允许您指定both 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);
    

    【讨论】:

    • 成功了,谢谢。你还知道如何在矩形上显示图表线吗?
    • @novayhulk14:很高兴它有帮助;关于“矩形上的线条”,您可能已经看到在我的update 之前缓存的页面。
    • 非常感谢!这正是我想要的:)
    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    相关资源
    最近更新 更多