JFreeChart的弧线画在图表上 [英] JFreechart draw arc on chart

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

问题描述

我有2个问题

1)我试图使用形状标注的XYplot划出一道弧线。我用xy线图注解画一条线,我想电弧的地方开始行结束。我有一些问题与parameters.I希望电弧有17高度,宽度44,并在情节点(3.0,17)开始(这是该行结束)。但是,code以下无法正常工作。有人可以告诉我什么是错的code?

  Arc2D.Double弧=新Arc2D.Double(3.0,
                        16.9,
                        44.0,
                        17.04,
                        180.0,
                        180.0,
                        Arc2D.OPEN
                );
plot.addAnnotation(新XYShapeAnnotation(弧,
                        的新BasicStroke(2.0F),Color.white));
XYLineAnnotation A1 =新XYLineAnnotation(3.0,0.0,3.0,
                        16.9,新的BasicStroke(2.0F),Color.white);

2)我怎样才能在极坐标图绘制类似的数字?

感谢


解决方案

  1. 关于的Arc2D 关键就是边框。为了使半弧 ^ h 单位高,范围必须 2 * H 单位高。


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


 进口java.awt.BasicStroke中;
进口java.awt.Color中;
进口java.awt.geom.Arc2D中;
进口了java.util.Random;
进口org.jfree.chart.ChartFactory;
进口org.jfree.chart.ChartFrame;
进口org.jfree.chart.JFreeChart;
进口org.jfree.chart.annotations.XYLineAnnotation;
进口org.jfree.chart.annotations.XYShapeAnnotation;
进口org.jfree.chart.plot.PlotOrientation;
进口org.jfree.chart.plot.XYPlot;
进口org.jfree.data.xy.XYDataset;
进口org.jfree.data.xy.XYSeries;
进口org.jfree.data.xy.XYSeriesCollection;/ ** @see http://stackoverflow.com/questions/6604211 * /
公共类ArcTest {    私有静态最终随机R =新的随机();
    私有静态最后双PI = 180D;
    私有静态最终诠释X = 3;
    私有静态最终诠释Y = 0;
    私有静态最终诠释W = 44;
    私有静态最终诠释H = 17;    公共静态无效的主要(字串[] args){
        JFreeChart的图表= ChartFactory.createXYLineChart(
            ArcTest,X,Y,createDataset(),
            PlotOrientation.VERTICAL,真,真,假);
        XYPlot情节= chart.getXYPlot();
        XYLineAnnotation行=新XYLineAnnotation(
            的X,Y,X,H,新的BasicStroke(2F),Color.blue);
        plot.addAnnotation(线);
        Arc2D.Double弧=新Arc2D.Double(
            的X,Y,W,2 * H,PI,PI Arc2D.OPEN);
        plot.addAnnotation(新XYShapeAnnotation(弧,
            的新BasicStroke(2.0F),Color.blue));
        ChartFrame帧=新ChartFrame(第一,图表);
        frame.pack();
        frame.setVisible(真);
    }    私有静态XYDataset createDataset(){
        XYSeriesCollection结果=新XYSeriesCollection();
        XYSeries系列=新XYSeries(ArcTest);
        series.add(0,0);
        series.add(W,W);
        result.addSeries(系列);
        返回结果;
    }
}

I have 2 questions

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)How can I draw a similar figure on a polar plot?

Thanks

解决方案

  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.

  2. 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天全站免登陆