xing
xing

Reputation: 105

JavaFX Class LineChart zoom operation doesn't work

I have a problem I can not figure out. Please let me put the code first for better explanation:

    public class TestXYChart extends Application {
        NumberAxis xAxis = new NumberAxis();     //     1
        NumberAxis yAxis = new NumberAxis();     //     2
        XYChart.Series series1 = new XYChart.Series();
        @Override
        public void start(Stage primaryStage) {

        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);

        LineChart lineChart = new LineChart<Number, Number>(xAxis, yAxis);
        root.setOnScroll(scrollHandler);
        root.getChildren().addAll(lineChart);

        series1.getData().add(new XYChart.Data(1, 20));
        series1.getData().add(new XYChart.Data(2, 40));
        series1.getData().add(new XYChart.Data(3, 100));
        series1.getData().add(new XYChart.Data(4, 50));
        series1.getData().add(new XYChart.Data(5, 170));
        series1.getData().add(new XYChart.Data(6, 190));
        series1.getData().add(new XYChart.Data(7, 30));
        series1.getData().add(new XYChart.Data(8, 39));
        series1.getData().add(new XYChart.Data(9, 44));
        series1.getData().add(new XYChart.Data(10, 78));
        series1.getData().add(new XYChart.Data(11, 93));
        series1.getData().add(new XYChart.Data(12, 75));

//        this.loadIntoDataBaseFromFile();
        lineChart.getData().addAll(series1);
        lineChart.setAnimated(true);
        lineChart.setAlternativeColumnFillVisible(false);
        lineChart.setAlternativeRowFillVisible(false);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    EventHandler scrollHandler = new EventHandler<ScrollEvent>(){

        @Override
        public void handle(ScrollEvent event) {
           System.out.println("Handling Scroll Event");
           xAxis.setLowerBound(xAxis.getLowerBound() + 1);
           xAxis.setUpperBound(xAxis.getUpperBound() - 1);
        }

    };
    }

I use the code above to test whether I can operate the LineChart, especially when I scroll mouse, the LineChart will zoom in/out. I found the fact that exactly as above code, it doesn't work. But if we just change the first two line codes, change from:

    NumberAxis xAxis = new NumberAxis();     //     1
    NumberAxis yAxis = new NumberAxis();     //     2

to:

    NumberAxis xAxis = new NumberAxis(0,12,0.5);     //     1
    NumberAxis yAxis = new NumberAxis(0,300,10);     //     2

Everything works well. So, why it happens like this?Thank you.

And, if I seperate the new NumberAxis(...) instance from the declaration NumberAxis xAxis/yAxis, it doesn't work too.

Upvotes: 0

Views: 520

Answers (1)

Jos&#233; Pereda
Jos&#233; Pereda

Reputation: 45456

The difference between using an empty constructor or with arguments is clear if you have a look at NumberAxis JavaDoc:

public NumberAxis() Create a auto-ranging NumberAxis

and

public NumberAxis(double lowerBound, double upperBound, double tickUnit) Create a non-auto-ranging NumberAxis with the given upper bound, lower bound and tick unit

Basically, the first one has autoRanging set to true, what means it will try to show the data, no matter how you modify the bounds of the axis.

On the contrary, the second one has autoRanging set to false, so any change in the axis bounds will be reflected on the chart.

Note that you can set this in the first case to change the default behaviour:

xAxis.setAutoRanging(false);

Upvotes: 1

Related Questions