代码之家  ›  专栏  ›  技术社区  ›  igorludi

如何在JavaFX中创建自定义折线图控件?

  •  0
  • igorludi  · 技术社区  · 6 年前

    我在JavaFX项目中有一个自定义的LineChartWithMarkers控件。 我在Java(FX)编程中暂停了两年,发现Java 10中不再存在LineChartBuilder。 我能找到一些文档说它在版本8中已被弃用,但没有找到如何替换它。

    如何修复代码以使用Java 10?

    这就是我所拥有的:

    public class LineChartWithMarkersBuilder extends LineChartBuilder {
        private Axis<Number> xAxis ;
        private Axis<Number> yAxis ;        
        private ObservableList<Series<Number,Number>> data ;
    
        public static LineChartWithMarkersBuilder create() {
            return new LineChartWithMarkersBuilder();
        }
    
        public LineChartWithMarkersBuilder xAxis(Axis<Number> xAxis) {
            this.xAxis = xAxis ;
            return this ;
        }
    
        public LineChartWithMarkersBuilder yAxis(Axis<Number> yAxis) {
            this.yAxis = yAxis ;
            return this ;
        }
    
    
        public LineChartWithMarkers<Number, Number> build() {
            xAxis = new NumberAxis();
            yAxis = new NumberAxis();
            return new LineChartWithMarkers<Number, Number>( xAxis, yAxis);
    
        }
    }
    

    以及带有标记的自定义折线图:

    public class LineChartWithMarkers<X extends Number, Y extends Number> extends LineChart<X, Y>  {
    
        private ObservableList<Data<Number, Number>> verticalMarkers;
    
    
        public LineChartWithMarkers(Axis<X> xAxis, Axis<Y> yAxis) {
            super(xAxis, yAxis);
            this.setCreateSymbols(false);
            verticalMarkers = FXCollections.observableArrayList(data -> new Observable[] {data.XValueProperty()});
            verticalMarkers.addListener((InvalidationListener)observable -> layoutPlotChildren());
        }
        (...)
    }
    

    我用的是。fxml如下:

    ...
                    <LineChartWithMarkers fx:id="chartFit" createSymbols="false" layoutX="14.0" layoutY="54.0" prefHeight="499.0" prefWidth="987.0" AnchorPane.bottomAnchor="162.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="54.0">
                    <xAxis>
                        <NumberAxis side="BOTTOM" />
                    </xAxis>
                    <yAxis>
                        <NumberAxis side="LEFT" />
                    </yAxis>
                </LineChartWithMarkers>
    ...
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   James_D    6 年前

    所有JavaFX构建器类在Java 8中都被弃用,在Java 9中被删除。只需完全删除builder类。允许 FXMLLoader top实例化没有无参数构造函数的类,使用 @NamedArg 构造函数参数注释:

    public class LineChartWithMarkers<X extends Number, Y extends Number> extends LineChart<X, Y>  {
    
        private ObservableList<Data<Number, Number>> verticalMarkers;
    
    
        public LineChartWithMarkers(
                @NamedArg("xAxis") Axis<X> xAxis, 
                @NamedArg("yAxis") Axis<Y> yAxis) {
            super(xAxis, yAxis);
            this.setCreateSymbols(false);
            verticalMarkers = FXCollections.observableArrayList(data -> new Observable[] {data.XValueProperty()});
            verticalMarkers.addListener((InvalidationListener)observable -> layoutPlotChildren());
        }
        (...)
    }
    

    完整讨论 @名称达格 注释可在中找到 What is the purpose of @NamedArg annotation in javaFX 8?