Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.js & nvd3.js axis and label precision formatting

Tags:

d3.js

nvd3.js

Using the stacked area chart as seen in this example http://nvd3.com/ghpages/stackedArea.html

Trying to format the y-axis tick labels and the tooltip labels to be integers instead of floats. Tried changing the follow code from

chart.yAxis
        .axisLabel('Users')
        .tickFormat(d3.format(',.2f'));

to

chart.yAxis
        .axisLabel('Users')
        .tickFormat(d3.format(',.0d'));

Precision remains unchanged (still shows values to the hundredths place). I've followed the Github Wiki to no avail https://github.com/mbostock/d3/wiki/Formatting#wiki-d3_format

Any suggestions or hints will be greatly appreciated.

like image 645
Viet Avatar asked Jul 25 '12 19:07

Viet


People also ask

What is D3 js used for?

D3 is a JavaScript library and framework for creating visualizations. D3 creates visualizations by binding the data and graphical elements to the Document Object Model. D3 associates (binding) the data (stuff you want to visualize) with the DOM. This allows the user to manipulate, change or add to the DOM.

Is D3 js still used?

The JavaScript ecosystem has completely changed during this time, in terms of libraries, best practices and even language features. Nevertheless, D3 is still here. And it's more popular than ever.

Which is better D3 js or chart js?

Chart. js provides a selection of ready to go charts which can be styled and configured while D3 offers building blocks which are combined to create virtually any data visualisation.

What is a D3 JS developer?

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS.


2 Answers

Looks like this isn't supported by nvd3 at the moment. See the offending line.

In addition, your format specification isn't quite right. As mentioned in the documentation, "d" ignores non-integer values. So you probably want ",.0f" instead, which means:

  • ,: use commas to separate thousands.
  • .0: precision of zero (the exact meaning of this depends on which type is in use).
  • f: The type; in this case, Number.toFixed. This means a fixed number of digits (the precision) appear after the decimal point, and the number is rounded if necessary.
like image 168
Jason Davies Avatar answered Sep 19 '22 16:09

Jason Davies


this one can format label text from float to integer.

for pie chart:

chart.pie.valueFormat(d3.format(',.0d'));

for line chart:

chart.yAxisTickFormat(d3.format(',.0d'));
like image 38
kevin Avatar answered Sep 22 '22 16:09

kevin