1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| package test;
import java.text.SimpleDateFormat;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
/**
*
* @author fabriceb
*/
public class Main extends Application {
final XYChart.Series series = new XYChart.Series();
public void start(Stage stage) {
stage.setTitle("Activités ascendant et descendant");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Temps");
yAxis.setLabel("Taille");
final LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Kb/sec");
series.setName("Download");
Scene scene = new Scene(lineChart, 800, 600);
stage.setScene(scene);
stage.show();
lineChart.getData().add(series);
stage.setScene(scene);
stage.show();
Service<Integer> service = new Service<Integer>() {
@Override
protected Task<Integer> createTask() {
return task;
}
};
service.start();
}
public static void main(String[] args) {
launch(args);
}
Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 100000; iterations++) {
if (isCancelled()) {
break;
}
final Date date = new Date();
final int i = iterations;
Platform.runLater(new Runnable() {
@Override
public void run() {
series.getData().add(new XYChart.Data(new SimpleDateFormat("HH:mm:ss").format(new Date()), i));
}
});
Thread.sleep(1000);
}
return iterations;
}
};
} |