简体   繁体   中英

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:

例

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);

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM