Fix X and Y axis width, JavaFX

How to achieve fixed axis width using JavaFx line plot.

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

public class Axis extends Application
{

    private Timeline animation;

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

    @Override
    public void start(final Stage stage) throws Exception
    {
        final NumberAxis x = new NumberAxis();
        final NumberAxis y = new NumberAxis();
        x.setAutoRanging(false);
        y.setAutoRanging(false);

        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(x, y);
        // final Series series = new LineChart.Series<Number, Number>();
        //
        // series.getData().add(new XYChart.Data(20, 23));
        // series.getData().add(new XYChart.Data(35, 14));
        // series.getData().add(new XYChart.Data(43, 15));
        // series.getData().add(new XYChart.Data(60, 24));
        // series.getData().add(new XYChart.Data(10, 34));
        //
        // lineChart.getData().add(series);
        lineChart.setPrefHeight(10);
        lineChart.setPrefWidth(10);
        lineChart.setMinHeight(10);
        lineChart.setMinWidth(10);
        lineChart.setMaxHeight(10);
        lineChart.setMaxWidth(10);

        final Scene scene = new Scene(lineChart, 800, 800);
        stage.setScene(scene);
        stage.show();

    }

    @Override
    public void stop()
    {
        animation.pause();
    }
}

      

enter image description here

I want the grid (swatch in red) not to scale when the window is resized. Currently the grid is resized when the window is resized as shown below.

enter image description here

I cannot achieve what I need using the minWidth and maxWidth APIs

+3


source to share


1 answer


Preventing graph resizing

You can prevent the graph from being resized by setting it to the preferred, minimum and maximum sizes:

lineChart.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
lineChart.setPrefSize(800, 600);
lineChart.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);

      

The only exception to this is that the chart is the root of the scene, in which case you must wrap the chart in a panel or group to prevent it from resizing to fit the scene (due to a quirk of layout in JavaFX 8).

Also note that wrapping any resizable node Group will resize the node (excluding transformations and effects) to the preferred sizes and never resize them.

Unfortunately setting the preferred chart size is not exactly the same as setting the preferred chart axis sizes directly, as this makes the entire chart a size and not a specific axis, which is probably what you are looking for with your question.



Preventing Plot Axis Resizing

I do not know how to do that.

You might think you can do the following:

xAxis.setMinWidth(Control.USE_PREF_SIZE);
xAxis.setPrefWidth(800);
xAxis.setMaxWidth(Control.USE_PREF_SIZE);

      

But this does not work in Java 8 when the axis is embedded inside the chart. This is because the XYChart layout routine ignores the preferred, minimum, and maximum axis sizes when it displays the contents of the chart.

This may be a bug, so you can log it as such at https://javafx-jira.kenai.com .

+2


source







All Articles