如何在图表中执行轴上的大数字(例如,10 ^ 3格式)? [英] How to perform big numbers on axis (for example, 10^3 format) in chart?

查看:204
本文介绍了如何在图表中执行轴上的大数字(例如,10 ^ 3格式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用大空间执行长数不是一个好的决定。
如果我的图表中等规模约为50 000,则在Y轴上将写成50 000,60000,10 000。我能在50 * 10 ^ 3或其他地方执行此操作吗?
对于具有多种尺度的图表,例如这里

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

推荐答案

使用 tickLabelFormatter 在轴上。例如:

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

    });

完整示例:

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

这篇关于如何在图表中执行轴上的大数字(例如,10 ^ 3格式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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