是否可以在JAVAFX上创建带有锁定y轴的水平滚动折线图?

时间:2018-11-30 10:01:30

标签: java css javafx linechart scrollpane

我想在 JAVAFX 上使用锁定的y轴创建水平滚动linechart。我一直在搜索示例或类似内容,但未使用JAVAFX找到任何内容。我尝试将linechart放到scrollpane中,但是当我滚动时(当然),所有的东西都在向右和向左移动,我希望至少总是看到图例和y轴...

有什么想法吗?谢谢!

例如在chart.js上这样的内容:

enter image description here

1 个答案:

答案 0 :(得分:0)

您好,我创建了一个新的应用程序,可以完成您想要的操作。诀窍在于,将图表的minHeight设置为小于ScrollPane的大小,因为ScrollPane需要滚动条的空间。这是我的示例:

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.ScrollPane;
import javafx.stage.Stage;


public class LineChartSample extends Application {

    @Override public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        //creating the chart
        final LineChart<Number,Number> lineChart =
                new LineChart<>(xAxis, yAxis);

        lineChart.setTitle("Stock Monitoring, 2010");
        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));
        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));

        ScrollPane root = new ScrollPane(lineChart);
        root.setMinSize(1000,600);
        lineChart.setMinSize(root.getMinWidth(),root.getMinHeight()-20);
        Scene scene  = new Scene(root,800,600);
        lineChart.getData().add(series);

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

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