JFreeChart - 不能像exel那样绘制图形

时间:2017-05-17 09:51:21

标签: java graphics jfreechart

我在JFreeChart中绘制图形时遇到问题,我需要像这张图片一样绘制图形。
excel graphic,但在java中我得到的图像graphic in java是否有任何可能的解决方法。

我的java代码:

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;

public class MyGraph {
    private static double A=20, R0=-40;

    public static void main(String[] args) {
        XYSeries series = new XYSeries("my graphic");

        for (double fi = 0,step = 0.05; fi < 2*Math.PI; fi+=step) addCoordinate(series,fi);

        XYDataset xyDataset = new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory
                .createXYLineChart("my graphic 14", "x", "y",
                        xyDataset,
                        PlotOrientation.VERTICAL,
                        true, true, true);
        JFrame frame =
                new JFrame("MinimalStaticChart");
        frame.getContentPane()
                .add(new ChartPanel(chart));
        frame.setSize(400,300);
        frame.setVisible(true);
    }
    private static void addCoordinate(XYSeries series,double fi){
        double ro = Math.cos(fi)-0.5;
        series.add(ro*Math.cos(fi),ro*Math.sin(fi));
        System.out.printf("fi = %f   ro = %f   x = %f   y = %f\n", fi , ro, ro*Math.cos(fi), ro*Math.sin(fi));
    }
}

你怎么能在图像,形式和坐标上看到它,但是JFreeChart绘制的图形不像exel一样,我怎么能像exel一样用java绘制我的图形,我应该使用哪种方法?(如果你可以 - 给出例子,请)

1 个答案:

答案 0 :(得分:2)

您正在使用XYSeries(String)构造函数,用x对值进行排序。如果你在填写系列后添加它,你会看到它:

for (Object i : series.getItems()) {
    System.out.println(i);
}

所以你唯一需要改变的是series的初始化:

XYSeries series = new XYSeries("my graphic", false);