我有以下代码用于为我的数据生成图表:
import // ...
import javax.swing.JFrame;
import de.erichseifert.gral.data.DataTable;
import de.erichseifert.gral.io.plots.DrawableWriterFactory;
import de.erichseifert.gral.plots.XYPlot;
import de.erichseifert.gral.plots.lines.DefaultLineRenderer2D;
import de.erichseifert.gral.plots.lines.LineRenderer;
import de.erichseifert.gral.ui.InteractivePanel;
public class Plot extends JFrame {
private static final long serialVersionUID = 8083971727860355817L;
private int width = 1366;
private int height = 768;
public Plot(String filename) {
super(filename);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(width - 10, height -40 );
}
@SuppressWarnings("unchecked")
public void showGraph(List<Integer> x, List<Double> y, boolean saveFile){
DataTable data = new DataTable(Integer.class, Double.class);
for (int i = 0; i < x.size(); i++) {
data.add(x.get(i), y.get(i));
}
XYPlot plot = new XYPlot(data);
// Draw a tick mark and a grid line every 10 units along x axis
int x_scale = 1;
if (x.size() > 20){
Collections.sort(x);
x_scale = x.get(y.size()-1) / 10;
}
plot.getAxisRenderer(XYPlot.AXIS_X).setTickSpacing(x_scale);
// Draw a tick mark and a grid line every 20 units along y axis
plot.getAxisRenderer(XYPlot.AXIS_Y).setTickSpacing(0.05);
LineRenderer lines = new DefaultLineRenderer2D();
plot.setLineRenderer(data, lines);
Color color = new Color(0.0f, 0.3f, 1.0f);
plot.getPointRenderer(data).setColor(color);
plot.getLineRenderer(data).setColor(color);
getContentPane().add(new InteractivePanel(plot));
// Zooming results in empty plot :(
//plot.getNavigator().setZoom(2f);
}
// ...
}
如果我取消注释设置缩放的最后一行
plot.getNavigator().setZoom(2f);
我会得到一个空的情节。有任何建议如何使它工作?