Formatting tooltip value with StandardXYToolTipGen

2019-07-24 01:35发布

问题:

In desktop Java project I used StandardXYToolTipGenerator for generating tooltips on chart.

Renderer was configured as below:

renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0} - {2} ({1})",new SimpleDateFormat("HH:mm:ss"), NumberFormat.getNumberInstance()));

Where {2} is value (example 1.149431) but when I am using NumberFormat it format itself and return value 1.149, I want get precisely rounding. How to get it? Any idea?

I tried extend NumberFormat class, but method format(double number) is final...

回答1:

NumberFormat.getNumberInstance() "Returns a general-purpose number format for the current default FORMAT locale." You can adjust the rounding mode and the number of digits as desired. Based on this example, the fragment below retains the default rounding mode and specifies five digits.

NumberFormat format = NumberFormat.getNumberInstance();
format.setMinimumFractionDigits(5);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
    "{2}", DateFormat.getDateInstance(), format));

As an aside, verify that your chosen SimpleDateFormat is appropriate for for your target locale.