我只想從JavaFX圖表API生成圖表圖像。我不想顯示應用程序窗口,或啓動應用程序(如果沒有必要)。使用JavaFX圖表API繪製圖表圖形
public class LineChartSample extends Application {
private List<Integer> data;
@Override public void start(Stage stage) {
stage.setTitle("Line Chart Sample");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Month");
final LineChart<String,Number> lineChart =
new LineChart<String,Number>(xAxis,yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
series.getData().add(new XYChart.Data("Jan", 23));
series.getData().add(new XYChart.Data("Feb", 14));
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
WritableImage image = scene.snapshot(null);
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", chartFile);
//stage.setScene(scene);
//stage.show();
}
public static void main(String[] args) {
launch(args);
}
public setData(List<Integer> data) {this.data = data;}
}
裏面的啓動方法,我真的需要,以建立一系列的數據來訪問外部的數據,但似乎沒有辦法從啓動方法來訪問外部的數據,如果我的數據存儲在成員變量中data
,當啓動被調用時它是空的。我其實不關心舞臺和場景對象,只要圖表圖像可以渲染,我應該如何解決問題?我想構建一個可以用輸入數據調用的API,並用數據繪製圖表,然後返回文件。
public File toLineChart(List<Integer> data) {
...
}
我想你想要做的是首先:用掃描儀從文件中讀取數據並存儲到一個數組或一些其他數據類型。下一步:您應該將存儲的信息傳遞給toLineChart方法。 toLineChart方法應該使用下面列出的一些想法來打印圖表。最重要的問題是如何存儲數據?它是在文本文件還是數據庫中? – Sedrick