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
Asked
Active
Viewed 1,304 times
-1
-
1Use a [`tickLabelFormatter`](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/ValueAxis.html#setTickLabelFormatter-javafx.util.StringConverter-) on the axis – James_D May 15 '15 at 11:25
-
You may change the magnitude of shown numbers. For example if the 50,000 is a millisecond value, you may show it 50 and change the axis description from "In milliseconds" to "In seconds". – Uluk Biy May 15 '15 at 13:07
1 Answers
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);
}
}

James_D
- 201,275
- 16
- 291
- 322