Advertisement
tmax

Untitled

May 20th, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.collections.FXCollections;
  3. import javafx.collections.ObservableList;
  4. import javafx.event.EventHandler;
  5. import javafx.scene.Scene;
  6. import javafx.scene.chart.LineChart;
  7. import javafx.scene.chart.NumberAxis;
  8. import javafx.scene.chart.XYChart;
  9. import javafx.scene.input.MouseEvent;
  10. import javafx.scene.layout.Pane;
  11. import javafx.stage.Stage;
  12.  
  13. import java.util.Random;
  14.  
  15. public class Main extends Application {
  16.  
  17.     @Override
  18.     public void start(final Stage primaryStage) throws Exception{
  19.  
  20.         final LineChart lineChart = new LineChart(new NumberAxis(), new NumberAxis());
  21.         final XYChart.Series<Integer, Integer> series = new XYChart.Series<Integer, Integer>();
  22.         ObservableList<XYChart.Series<Integer, Integer>> seriesDataSet = FXCollections.observableArrayList();
  23.  
  24.         lineChart.setAnimated(false);
  25.         lineChart.setPrefWidth(800);
  26.         lineChart.setPrefHeight(600);
  27.         seriesDataSet.add(series);
  28.         lineChart.setData(seriesDataSet);
  29.  
  30.         for (int n = 0; n < 50; ++n) {
  31.             series.getData().add(new XYChart.Data<Integer, Integer>(n, new Random().nextInt(20)));
  32.         }
  33.  
  34.         Pane pane = new Pane();
  35.         pane.getChildren().add(lineChart);
  36.  
  37.         primaryStage.setScene(new Scene(pane, 800, 600));
  38.         primaryStage.show();
  39.  
  40.         // Get a display
  41.         System.out.println(lineChart.getXAxis().getDisplayPosition(2));
  42.         // The value is now correct and different from the previous one
  43.         System.out.println(lineChart.getXAxis().getDisplayPosition(2));
  44.  
  45.         // Add data to the chart
  46.         for (int n = 51; n < 80; ++n) {
  47.             series.getData().add(new XYChart.Data<Integer, Integer>(n, new Random().nextInt(20)));
  48.         }
  49.  
  50.         // Get a display
  51.         System.out.println(lineChart.getXAxis().getDisplayPosition(2));
  52.         // The value is now correct and different from the previous one
  53.         System.out.println(lineChart.getXAxis().getDisplayPosition(2));
  54.  
  55.         pane.setOnMouseClicked(new EventHandler<MouseEvent>() {
  56.             @Override
  57.             public void handle(MouseEvent mouseEvent) {
  58.                 System.out.println(lineChart.getXAxis().getDisplayPosition(2));
  59.             }
  60.         });
  61.     }
  62.  
  63.  
  64.     public static void main(String[] args) {
  65.         launch(args);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement