3

I'm trying to create a pie chart with Vaadin Charts.

This piece of code adds nice labels to the chart, but two digits after the decimal point t would be enough.

 dataLabels.setFormatter("''+ this.point.name +': '+ this.percentage +' %'");

See here picture of current chart.

Any ideas how to print just two digits after the decimal point?

I already tried

dataLabels.setFormatter("''+ this.point.name +': '+ this.percentage%02.2f +' %'");

and

dataLabels.setFormat("{this.percentage:%02.2f}");
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • I haven't used the charts yet but the last attempt seems correct according to the Vaadin book. Perhaps you can also try [using a javascript function to format the values](https://vaadin.com/book/-/page/charts.configuration.html#charts.configuration.format.formatter) – Morfic Dec 10 '15 at 17:30
  • I also tried to use a function: `dataLabels.setFormatter("function() {return String.format(\"%02d\", this.percentage) + ' %';}");` However it is unfortunately not working :( – Edelstoff990 Dec 11 '15 at 15:17

1 Answers1

4

If you use setFormat you shouldn't use this:

dataLabels.setFormat("{point.name}: {percentage:%02.2f}%");

And if you want to use setFormatter use the toFixed() Javascript function:

dataLabels.setFormatter("'' + this.point.name + ': ' + this.percentage.toFixed(2) + '%'");
nyg
  • 2,380
  • 3
  • 25
  • 40