Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show only numbers that are divisible by tick unit on javafx chart axis

Here's what I have now

enter image description here

As you can see, numbers on the left are not divisible by tick mark (in this situation by 50). I would like to see only numbers like 2950, 3000, 3050 etc. That includes top marker by the way (float up there is bothering me)

like image 380
Meegoo Avatar asked Nov 23 '25 08:11

Meegoo


1 Answers

If the auto ranging function does not provide the values you require for tick marks, you can configure the tick range manually.

yAxis.setAutoRanging(false);
yAxis.setLowerBound(1000);
yAxis.setUpperBound(5000);
yAxis.setTickUnit(500);
yAxis.setMinorTickVisible(false);

Sample Application

Numbers on the Y Axis are only divisible by the tick unit (500) as specified for the axis. Also the upper and lower bounds of the Y Axis are explicitly set, providing exact control over these values.

sample app

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class LineChartRanging extends Application {

    @Override public void start(Stage stage) {
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        final LineChart<Number,Number> lineChart =
                new LineChart<>(xAxis, yAxis);

        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        series.getData().add(new XYChart.Data(1, 2317));
        series.getData().add(new XYChart.Data(2, 1427));
        series.getData().add(new XYChart.Data(3, 1573));
        series.getData().add(new XYChart.Data(4, 2452));
        series.getData().add(new XYChart.Data(5, 3495));
        series.getData().add(new XYChart.Data(6, 3663));
        series.getData().add(new XYChart.Data(7, 2215));
        series.getData().add(new XYChart.Data(8, 4541));
        series.getData().add(new XYChart.Data(9, 4393));
        series.getData().add(new XYChart.Data(10, 1772));
        series.getData().add(new XYChart.Data(11, 2994));
        series.getData().add(new XYChart.Data(12, 2508));

        Scene scene  = new Scene(lineChart);
        lineChart.getData().add(series);
        lineChart.setPrefSize(500, 340);

        yAxis.setAutoRanging(false);
        yAxis.setLowerBound(1000);
        yAxis.setUpperBound(5000);
        yAxis.setTickUnit(500);
        yAxis.setMinorTickVisible(false);

        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
like image 110
jewelsea Avatar answered Nov 24 '25 22:11

jewelsea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!