user3348557 user3348557 - 3 months ago 53
Javascript Question

Formatting values on the y axis in a dc.js / d3 chart

I am trying to shorten the values on my Y axis, currently they are appearing as so:

Example

While I can play around with the padding, I would like the Y axis to be displayed in this manner:

100,000 = 100k

200,000 = 200k

etc etc

Attached is my code.

var paidSubChart = dc.lineChart("chart1")
.width(450)
.height(300)
.elasticX(true)
.x(d3.scale.linear().domain([-30,N]))
.elasticY(true)
.brushOn(false)
.yAxisLabel("Paid Subs")
.xAxisLabel("Days Since Launch")
.dimension(dayCountDimension)
.group(paidSubGroup);

var expansionActChart = dc.lineChart("chart2")
.width(450)
.height(300)
.elasticX(true)
.x(d3.scale.linear().domain([0,N]))
.elasticY(true)
.brushOn(false)
.yAxisLabel("Paid Subs")
.xAxisLabel("Days Since Launch")
.dimension(dayCountDimension)
.group(expActGroup);

Answer

The axes in dc.js are provided by d3.js.

I think you're looking for

chart.yAxis().tickFormat(d3.format('.3s'))

which calls d3.axis.tickFormat and uses d3.format's "SI prefix".

Note: it's best to keep axis manipulation in a separate statement because stuff breaks if you try to chain other dc.js chart commands after a yAxis command.

Comments