即使在轴上进行自动调整,我仍可以保留整数刻度线吗? [英] Can I keep integral tick marks even with autoranging on an Axis?

查看:205
本文介绍了即使在轴上进行自动调整,我仍可以保留整数刻度线吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里窃取了一些代码有一个 AreaChart 带有smooth lines,我在我的FXML中使用它的工作原理:

 < SmoothedAreaChart fx:id =chartlegendVisible =false
title =tree depth by lineanimated =false>
< xAxis>
< NumberAxis fx:id =xAxistickUnit =1.0autoRanging =false
minorTickVisible =falseforceZeroInRange =false
label =Line number/> ;
< / xAxis>
< yAxis>
< NumberAxis fx:id =yAxisminorTickVisible =false
tickUnit =1.0forceZeroInRange =truelabel =Tree depth/>
< / yAxis>
< / SmoothedAreaChart>

但我的问题是Y轴。



我将 tickUnit 设置为 1.0 ,因为我想要整数单位, t quite work:



>



如果在 yAxis 的定义中设置 autoRanging false ,然后图形被裁剪(当我填充图形时,我手动设置上限):







基本上,我喜欢两者的行为:







  • 有可能做这个,或者我需要编写自己的/找到一个 Axis 的实现?

    解决方案

    在这里你可以看到只是几行格式的数字,但你想要的。 (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> convert = new StringConverter< Number>(){
    @Override public String toString(Number object){
    //对于isInteger
    是一个未经测试的想法return object.doubleValue 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& );
    });

    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 = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
    }

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

    }


    I "stole" some code from here to have an AreaChart with "smooth lines" which I use in my FXML and it 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 I have however is with the Y axis.

    I set the tickUnit to 1.0 because I want, well, integral units, but it doesn't quite work:

    If, in the definition of yAxis, I set autoRanging to false then the graph is cropped (I set the upper bound by hand when I fill the graph):

    And, well, there are too many tick marks as well.

    Basically, I'd like the behavior of both:

    • that the autoranging takes place,
    • but that the tick marks always be integral numbers.

    Is there a possibility to do this, or do I need to code my own/find an implementation of Axis which does that?

    解决方案

    Here you can see just a few lines to format the numbers however you want. (SO overlords asked for an explanation)

    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);
        }
    
    }
    

    这篇关于即使在轴上进行自动调整,我仍可以保留整数刻度线吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆