bartosz.baczek
bartosz.baczek

Reputation: 1516

Highcharts - show decimal places only if needed

How do i make highchart's y label show decimal values only if needed?

Right now I am using

yAxis: { title: { text: null, }, tickAmount: 5, labels: { format: "{value:.2f}", // this stands for showing two decimal places }, };

The effect of this configuration, is that I see decimal places, even when they are not needed to represent number:

enter image description here

I want to see decimal places only if needed, for example: 3,14.

Upvotes: 2

Views: 2316

Answers (2)

Emre Bolat
Emre Bolat

Reputation: 4552

You should use formatter instead of format.

labels: {
            formatter: function () {
                return parseFloat(this.value);
            }
        }

Example: jsFiddle

Upvotes: 2

pawel_d
pawel_d

Reputation: 3070

You can perform a modulo operation inside yAxis.labels.formatter to check if number has decimals and if so, use Highcharts.numberFormat function to display this number with two decimals. Take a look at the example below.

API Reference:
http://api.highcharts.com/highcharts/yAxis.labels.formatter
http://api.highcharts.com/highcharts/Highcharts.numberFormat

Example:
http://jsfiddle.net/y5s7vsvm/

Upvotes: 2

Related Questions