Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.scene.Scene;
- import javafx.scene.chart.LineChart;
- import javafx.scene.chart.NumberAxis;
- import javafx.scene.chart.XYChart;
- import javafx.scene.layout.Pane;
- import javafx.stage.Stage;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- public class Main extends Application {
- @Override
- public void start(final Stage primaryStage) throws Exception{
- final LineChart lineChart = new LineChart(new NumberAxis(), new NumberAxis());
- final XYChart.Series<Integer, Integer> series = new XYChart.Series<Integer, Integer>();
- ObservableList<XYChart.Series<Integer, Integer>> seriesDataSet = FXCollections.observableArrayList();
- lineChart.setAnimated(false);
- lineChart.setPrefWidth(800);
- lineChart.setPrefHeight(600);
- seriesDataSet.add(series);
- lineChart.setData(seriesDataSet);
- // Add data to the chart
- for(int n = 0; n < 50; ++n){
- series.getData().add(new XYChart.Data<Integer, Integer>(n, new Random().nextInt(20)));
- }
- Pane pane = new Pane();
- pane.getChildren().add(lineChart);
- primaryStage.setScene(new Scene(pane, 800, 600));
- primaryStage.show();
- // Get a display
- System.out.println(lineChart.getXAxis().getDisplayPosition(2));
- // Get the same display position after some time so the data is displayed in screen
- new Timer().schedule(new TimerTask() {
- @Override
- public void run() {
- Platform.runLater(new Runnable() {
- @Override
- public void run() {
- // The value is now correct and different from the previous one
- System.out.println(lineChart.getXAxis().getDisplayPosition(2));
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // Add data to the chart
- for(int n = 51; n < 80; ++n){
- series.getData().add(new XYChart.Data<Integer, Integer>(n, new Random().nextInt(20)));
- }
- // Get a display
- System.out.println(lineChart.getXAxis().getDisplayPosition(5));
- // Get the same display position after some time so the data is displayed in screen
- new Timer().schedule(new TimerTask() {
- @Override
- public void run() {
- Platform.runLater(new Runnable() {
- @Override
- public void run() {
- // The value is now correct and different from the previous one
- System.out.println(lineChart.getXAxis().getDisplayPosition(5));
- }
- });
- }
- }, 100);
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- }
- });
- }
- }, 100);
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement