JFreechart 在图表上绘制圆弧 [英] JFreechart draw arc on chart

查看:35
本文介绍了JFreechart 在图表上绘制圆弧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题

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

1)I am trying to draw an arc on an XYplot using the shape annotation. I used the XYLine annotation to draw a line and I want the arc to start where the line ends. I am having some issues with the parameters.I want the arc to have a height of 17, width 44, and start at the point(3.0, 17) of the plot(this is where the line ends). But the code below does not work. Can someone please tell me what is wrong with the code?

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)如何在极坐标图上绘制相似的图形?

2)How can I draw a similar figure on a polar plot?

谢谢

推荐答案

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

  1. The critical thing about Arc2D is the bounding rectangle. To make the half-arc H units high, the bounds must be 2 * H units high.

AFAIK,PolarPlot 不支持注释.

AFAIK, PolarPlot does not support annotations.

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;
    }
}

这篇关于JFreechart 在图表上绘制圆弧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆