2

我正在尝试创建简单的 XYSplineRenderer,但代码只有在我编写 XYDotRenderer a = new XYDotRenderer(); 时才有效 或 XYLineAndShapeRenderer a = new XYLineAndShapeRenderer(); 同一个人可以告诉我做错了什么吗?我只是编程的初学者。

这是我的代码:

package kubas;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.ApplicationFrame;

public class spausdink {

    public static void main(String[] args) {

XYSeries series = new XYSeries("XY grafikas");
        series.add(1, 2);
        series.add(2, 4);
        series.add(3, 8);
        series.add(4, 16);
        series.add(5, 32);
        series.add(6, 64);
        series.add(7, 128);
        series.add(8, 256);
        series.add(9, 512);
        series.add(10, 1024);

XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);



ApplicationFrame frame = new ApplicationFrame("Mano grafikas");


        NumberAxis xax = new NumberAxis("x");
        NumberAxis yax = new NumberAxis("y");
        XYSplineRenderer a = new XYSplineRenderer();
        a.setPrecision(10);
        XYPlot xyplot = new XYPlot(dataset, xax, yax, a);

        JFreeChart chart = new JFreeChart(xyplot);

        ChartPanel chartPanel = new ChartPanel(chart);
        frame.setContentPane(chartPanel);
        frame.pack();
        frame.setVisible(true);


    }

}

编辑消息:

两个代码都可以正常工作,然后我创建了新项目。

4

1 回答 1

5

我不确定出了什么问题,但下面有一个工作示例。我倾向于为精度选择小的奇数值;默认为5. 相同的代码适用于XYLineAndShapeRendereror

XYDotRenderer r = new XYDotRenderer();
r.setDotWidth(5);
r.setDotHeight(5);

图片

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.*;

public class Test {

    public static final String Title = "f(x) = 2^x";

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }

    private static void display() {
        XYSeries series = new XYSeries(Title);
        for (int i = 0; i <= 10; i++) {
            series.add(i, Math.pow(2, i));
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        NumberAxis domain = new NumberAxis("x");
        NumberAxis range = new NumberAxis("f(x)");
        XYSplineRenderer r = new XYSplineRenderer(3);
        XYPlot xyplot = new XYPlot(dataset, domain, range, r);
        JFreeChart chart = new JFreeChart(xyplot);
        ChartPanel chartPanel = new ChartPanel(chart){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        JFrame frame = new JFrame(Title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(chartPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
于 2013-02-09T16:33:48.293 回答