2016-03-04 68 views
0

我正在使用JavaFX XYChart程序編寫一個快速圖表應用程序。我希望XYChart代碼生成實時更新圖。根據我的理解,一種方法可能會使用併發或隊列進行。下面是一個JavaFX XYChart的例子,你可以提供關於如何使它實時(移動)更新到XYchart的建議嗎?實時JavaFX XYChart

 import java.util.Arrays; 
    import javafx.application.Application; 
    import javafx.collections.FXCollections; 
    import javafx.scene.Scene; 
    import javafx.scene.chart.CategoryAxis; 
    import javafx.scene.chart.NumberAxis; 
    import javafx.scene.chart.XYChart; 
    import javafx.scene.paint.Color; 
    import javafx.stage.Stage; 
    import chart.gantt_04.GanttChart.ExtraData; 

    // TODO: use date for x-axis public class GanttChartSample extends Application { 

@Override public void start(Stage stage) { 

    stage.setTitle("Gantt Chart Sample"); 

    String[] machines = new String[] { "Machine 1", "Machine 2", "Machine 3" }; 

    final NumberAxis xAxis = new NumberAxis(); 
    final CategoryAxis yAxis = new CategoryAxis(); 

    final GanttChart<Number,String> chart = new GanttChart<Number,String>(xAxis,yAxis); 
    xAxis.setLabel(""); 
    xAxis.setTickLabelFill(Color.CHOCOLATE); 
    xAxis.setMinorTickCount(4); 

    yAxis.setLabel(""); 
    yAxis.setTickLabelFill(Color.CHOCOLATE); 
    yAxis.setTickLabelGap(10); 
    yAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(machines))); 

    chart.setTitle("Machine Monitoring"); 
    chart.setLegendVisible(false); 
    chart.setBlockHeight(50); 
    String machine; 

    machine = machines[0]; 
    XYChart.Series series1 = new XYChart.Series(); 
    series1.getData().add(new XYChart.Data(0, machine, new ExtraData(1, "status-red"))); 
    series1.getData().add(new XYChart.Data(1, machine, new ExtraData(1, "status-green"))); 
    series1.getData().add(new XYChart.Data(2, machine, new ExtraData(1, "status-red"))); 
    series1.getData().add(new XYChart.Data(3, machine, new ExtraData(1, "status-green"))); 

    machine = machines[1]; 
    XYChart.Series series2 = new XYChart.Series(); 
    series2.getData().add(new XYChart.Data(0, machine, new ExtraData(1, "status-green"))); 
    series2.getData().add(new XYChart.Data(1, machine, new ExtraData(1, "status-green"))); 
    series2.getData().add(new XYChart.Data(2, machine, new ExtraData(2, "status-red"))); 

    machine = machines[2]; 
    XYChart.Series series3 = new XYChart.Series(); 
    series3.getData().add(new XYChart.Data(0, machine, new ExtraData(1, "status-blue"))); 
    series3.getData().add(new XYChart.Data(1, machine, new ExtraData(2, "status-red"))); 
    series3.getData().add(new XYChart.Data(3, machine, new ExtraData(1, "status-green"))); 

    chart.getData().addAll(series1, series2, series3);   

    chart.getStylesheets().add(getClass().getResource("ganttchart.css").toExternalForm()); 

    Scene scene = new Scene(chart,620,350); 
    stage.setScene(scene); 
    stage.show(); 
} 

public static void main(String[] args) { 
    launch(args); 
} 

回答

0

的可能重複:Line Chart Live update

雖然因爲這些解決方案都相當污染,我創建了一個更簡約的例子:

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.scene.Scene; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
import javafx.stage.Stage; 

import java.util.concurrent.atomic.AtomicInteger; 

public class Main extends Application { 

    private AtomicInteger tick = new AtomicInteger(0); 

    @Override 
    public void start(Stage stage) { 
    final NumberAxis xAxis = new NumberAxis(); 
    final NumberAxis yAxis = new NumberAxis(); 

    xAxis.setAnimated(false); 
    xAxis.setLabel("Tick"); 

    yAxis.setAnimated(false); 
    yAxis.setLabel("Value"); 

    XYChart.Series<Number, Number> series = new XYChart.Series<>(); 
    series.setName("Values"); 

    LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis); 
    chart.setAnimated(false); 
    chart.getData().add(series); 

    Scene scene = new Scene(chart, 620, 350); 
    stage.setScene(scene); 
    stage.show(); 

    Thread updateThread = new Thread(() -> { 
     while (true) { 
     try { 
      Thread.sleep(1000); 
      Platform.runLater(() -> series.getData().add(new XYChart.Data<>(tick.incrementAndGet(), (int) (Math.random() * 100)))); 
     } catch (InterruptedException e) { 
      throw new RuntimeException(e); 
     } 
     } 
    }); 
    updateThread.setDaemon(true); 
    updateThread.start(); 
    } 

    public static void main(String[] args) { 
    launch(args); 
    } 
} 

圖表的數據可觀察到的列表,以便您可以添加數據從任何地方,它會自動更新。只要確保在UI線程中添加數據。 OK。

+0

OK。 XYCharts折線圖也有類似的例子。你能否爲條形圖實施解決方案?也許,線程內更新所述系列按照以下 - 螺紋updateThread =新主題(() - > { 而(真){ 嘗試{ 的Thread.sleep(50); Platform.runLater(() - > new.Xml.getData()。add(new XYChart.Data <>(tick.incrementAndGet(),(int)(Math.random()* 100)))); } catch(InterruptedException e){0} {0}拋出new RuntimeException (e); } } }); –