Advertisement
tmax

Untitled

May 20th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.application.Application;
  4. import javafx.application.Platform;
  5. import javafx.collections.FXCollections;
  6. import javafx.collections.ObservableList;
  7. import javafx.scene.Scene;
  8. import javafx.scene.chart.LineChart;
  9. import javafx.scene.chart.NumberAxis;
  10. import javafx.scene.chart.XYChart;
  11. import javafx.scene.layout.Pane;
  12. import javafx.stage.Stage;
  13.  
  14. import java.util.Random;
  15. import java.util.Timer;
  16. import java.util.TimerTask;
  17.  
  18. public class Main extends Application {
  19.  
  20.     @Override
  21.     public void start(final Stage primaryStage) throws Exception{
  22.  
  23.         final LineChart lineChart = new LineChart(new NumberAxis(), new NumberAxis());
  24.         final XYChart.Series<Integer, Integer> series = new XYChart.Series<Integer, Integer>();
  25.         ObservableList<XYChart.Series<Integer, Integer>> seriesDataSet = FXCollections.observableArrayList();
  26.  
  27.         lineChart.setAnimated(false);
  28.         lineChart.setPrefWidth(800);
  29.         lineChart.setPrefHeight(600);
  30.         seriesDataSet.add(series);
  31.         lineChart.setData(seriesDataSet);
  32.  
  33.         // Add data to the chart
  34.         for(int n = 0; n < 50; ++n){
  35.             series.getData().add(new XYChart.Data<Integer, Integer>(n, new Random().nextInt(20)));
  36.         }
  37.  
  38.         Pane pane = new Pane();
  39.         pane.getChildren().add(lineChart);
  40.  
  41.         primaryStage.setScene(new Scene(pane, 800, 600));
  42.         primaryStage.show();
  43.  
  44.         // Get a display
  45.         System.out.println(lineChart.getXAxis().getDisplayPosition(2));
  46.  
  47.         // Get the same display position after some time so the data is displayed in screen
  48.         new Timer().schedule(new TimerTask() {
  49.             @Override
  50.             public void run() {
  51.                 Platform.runLater(new Runnable() {
  52.                     @Override
  53.                     public void run() {
  54.                         // The value is now correct and different from the previous one
  55.                         System.out.println(lineChart.getXAxis().getDisplayPosition(2));
  56.  
  57.                         ////////////////////////////////////////////////////////////////////////////////////////////////////
  58.                         // Add data to the chart
  59.                         for(int n = 51; n < 80; ++n){
  60.                             series.getData().add(new XYChart.Data<Integer, Integer>(n, new Random().nextInt(20)));
  61.                         }
  62.  
  63.                         // Get a display
  64.                         System.out.println(lineChart.getXAxis().getDisplayPosition(5));
  65.  
  66.                         // Get the same display position after some time so the data is displayed in screen
  67.                         new Timer().schedule(new TimerTask() {
  68.                             @Override
  69.                             public void run() {
  70.                                 Platform.runLater(new Runnable() {
  71.                                     @Override
  72.                                     public void run() {
  73.                                         // The value is now correct and different from the previous one
  74.                                         System.out.println(lineChart.getXAxis().getDisplayPosition(5));
  75.                                     }
  76.                                 });
  77.                             }
  78.                         }, 100);
  79.                         ////////////////////////////////////////////////////////////////////////////////////////////////////
  80.                     }
  81.                 });
  82.             }
  83.         }, 100);
  84.  
  85. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  86.     }
  87.  
  88.  
  89.     public static void main(String[] args) {
  90.         launch(args);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement