Can I save integral marks even when authorizing on the axis?

I "stole" the code here to have AreaChartwith the "smooth lines" that I use in my FXML, and this works:

    <SmoothedAreaChart fx:id="chart" legendVisible="false"
        title="Tree depth by line" animated="false">
        <xAxis>
            <NumberAxis fx:id="xAxis" tickUnit="1.0" autoRanging="false"
                minorTickVisible="false" forceZeroInRange="false"
                label="Line number"/>
        </xAxis>
        <yAxis>
            <NumberAxis fx:id="yAxis" minorTickVisible="false"
                tickUnit="1.0" forceZeroInRange="true" label="Tree depth"/>
        </yAxis>
    </SmoothedAreaChart>

The problem that I have, however, is related to the Y axis.

I installed tickUnitin 1.0because I want, well, integral units, but this does not quite work:

enter image description here

If in the definition yAxisI set autoRangingto false, then the graph is cropped (I set the upper border manually when I fill the graph):

enter image description here

And, well, too many checkmarks.

Basically, I would like the behavior of both:

  • what is happening authorization
  • but that labels are always integers.

, / Axis, ?

+4
2

, , , .

FXML; , y ( - false):

display.yAxis.setUpperBound(maxDepth);
final int tickUnit = maxDepth / 15;
display.yAxis.setTickUnit(Math.max(tickUnit, 1));

, , , API, . , , ...

0

, . (SO- )

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.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class AxisFormatter extends Application {
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    final LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);
    double i = 0d;

    final StringConverter<Number> converter =  new StringConverter<Number>() {
        @Override public String toString(Number object) {
            //just an untested idea for isInteger
            return object.doubleValue() == object.intValue() 
                    ? object.intValue()+""
                    :"";
        }
        @Override public Number fromString(String string) { return 0;}
    };

    @Override public void start(Stage primaryStage) {

        chart.getData().add(new XYChart.Series<>());

        while (i++ < 10d)
            chart.getData().get(0).getData().add(new XYChart.Data<>(i,i/2));

        Button btn1 = new Button("increase max y value");
        btn1.setOnAction((evt)->{
            chart.getData().get(0).getData().add(new XYChart.Data<>(i++,i/2));
        });

        Button btn2 = new Button("change formatter");
        btn2.setOnAction((evt)->{
            yAxis.setTickLabelFormatter(yAxis.getTickLabelFormatter() == converter
                ? null : converter);
        });

        yAxis.setTickLabelFormatter(converter);

        VBox root = new VBox(chart, new HBox(5,btn1,btn2));
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
0

All Articles