annotations - JFreechart在图表上绘制圆弧

标签 annotations draw jfreechart geometric-arc

我有 2 个问题

1)我试图使用形状注释在 XYplot 上绘制圆弧。我使用 XYLine 注释来绘制一条线,并且希望弧线从该线结束的地方开始。我在参数方面遇到一些问题。我希望弧的高度为 17,宽度为 44,并从绘图的点 (3.0, 17) 开始(这是线条结束的地方)。但下面的代码不起作用。有人可以告诉我代码有什么问题吗?

Arc2D.Double arc = new Arc2D.Double(3.0, 
                        16.9,
                        44.0,
                        17.04, 
                        180.0,
                        180.0,
                        Arc2D.OPEN 
                );
plot.addAnnotation(new XYShapeAnnotation(arc,
                        new BasicStroke(2.0f), Color.white));
XYLineAnnotation a1 = new XYLineAnnotation(3.0, 0.0, 3.0,
                        16.9, new BasicStroke(2.0f), Color.white);

2)如何在极坐标图上绘制相似的图形?

谢谢

最佳答案

  1. Arc2D 的关键是边界矩形。要使半弧 H 单位高,边界必须为 2 * H 单位高。

  2. 据我所知,PolarPlot 不支持注释。

enter image description here

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Arc2D;
import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYLineAnnotation;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see http://stackoverflow.com/questions/6604211 */
public class ArcTest {

    private static final Random r = new Random();
    private static final double PI = 180d;
    private static final int X = 3;
    private static final int Y = 0;
    private static final int W = 44;
    private static final int H = 17;

    public static void main(String[] args) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            "ArcTest", "X", "Y", createDataset(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();
        XYLineAnnotation line = new XYLineAnnotation(
            X, Y, X, H, new BasicStroke(2f), Color.blue);
        plot.addAnnotation(line);
        Arc2D.Double arc = new Arc2D.Double(
            X, Y, W, 2 * H, PI, PI, Arc2D.OPEN);
        plot.addAnnotation(new XYShapeAnnotation(arc,
            new BasicStroke(2.0f), Color.blue));
        ChartFrame frame = new ChartFrame("First", chart);
        frame.pack();
        frame.setVisible(true);
    }

    private static XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries("ArcTest");
        series.add(0, 0);
        series.add(W, W);
        result.addSeries(series);
        return result;
    }
}

关于annotations - JFreechart在图表上绘制圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604211/

相关文章:

java - 找不到类型 : java. lang.Long :No validator could be found for type: java. lang.Long 的 validator

java - 带有Around建议和@annotation的Spring AOP无法正常工作

javascript - Google Apps 脚本中出现错误消息 "Domado: canvas drawImage not yet implemented"- 不允许绘制图像?

python - 如何将 tkinter 表保存为图像

iOS:使用 Core Graphics 绘制 "Movable Polygon"

ios - 如何在 GMSMarker Google 上制作自定义 UIView 或标签

java - 'nested'注释的Spring AOP切入点

java - 如何禁用 JFreeChart 中的缩放?

java - JFreechart XYline 连接数据库

java - JFreeChart 中是否可以有两个(X 和 Y)CategoryAxes?