How to perform big numbers on axis (for example, 10^3 format) in chart?

℡╲_俬逩灬. 提交于 2019-12-25 17:36:36

问题


Using a big space performing long numbers is not a good decision. If I have a graph with a medium scale about 50 000, on the Y axis will be written "50 000, 60 000, 10 000". Can I perform it there like 50 * 10^3 or something else? It's necessary for charts with several scales like here


回答1:


Use a tickLabelFormatter on the axis. For example:

    NumberFormat format = new DecimalFormat("#.#E0");
    yAxis.setTickLabelFormatter(new StringConverter<Number>() {

        @Override
        public String toString(Number number) {
            return format.format(number.doubleValue());
        }

        @Override
        public Number fromString(String string) {
            try {
                return format.parse(string);
            } catch (ParseException e) {
                e.printStackTrace();
                return 0 ;
            }
        }

    });

Complete example:

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

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

public class LineChartWithFormattedNumbers extends Application {

    @Override
    public void start(Stage primaryStage) {
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();

        NumberFormat format = new DecimalFormat("#.#E0");
        yAxis.setTickLabelFormatter(new StringConverter<Number>() {

            @Override
            public String toString(Number number) {
                return format.format(number.doubleValue());
            }

            @Override
            public Number fromString(String string) {
                try {
                    return format.parse(string);
                } catch (ParseException e) {
                    e.printStackTrace();
                    return 0 ;
                }
            }

        });

        xAxis.setLabel("Number of Month");
        //creating the chart
        final LineChart<Number,Number> lineChart = 
                new LineChart<Number,Number>(xAxis,yAxis);

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

        Scene scene  = new Scene(lineChart,800,600);
        lineChart.getData().add(series);

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

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


来源:https://stackoverflow.com/questions/30257787/how-to-perform-big-numbers-on-axis-for-example-103-format-in-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!