我正在尝试创建简单的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);
}
}
已编辑消息:
两个代码都正常工作,然后我创建了新项目。
答案 0 :(得分:5)
我不确定是什么问题,但下面有一个有效的例子。我倾向于为精度选择小的奇数值;默认值为5
。相同的代码适用于XYLineAndShapeRenderer
或
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);
}
}