Acyra
Acyra

Reputation: 16034

Prevent negative values in NVD3 chart axis

Hi I need to limit my line graphs with NVD3/D3 to having a Y axis with no negative numbers. I'm trying to do that based on this answer as such:

chart.y1Axis.scale().domain([0, maxValue])
            .tickFormat(d3.format(',f'));

But adding ".scale().domain([0, maxValue])" to the chain kills the entire chart. I can't figure out how to do this basic thing and can't find an example that works. Any thoughts? Thanks!

Upvotes: 2

Views: 1828

Answers (1)

Adam Pearce
Adam Pearce

Reputation: 9293

y1Axis.scale() returns a scale object, which .domain([0, maxValue]) modifies.

.tickFormat(d3.format(',f')) needs an axis object, not a scale object. To fix the problem, put .scale() after you're done modifying the axis:

chart.y1Axis
    .tickFormat(d3.format(',f'))
    .tickValues([1, 2, 3, 5, 8, 13, 21])
    .etc(...)
  .scale()
    .domain([0, maxValue])

Upvotes: 2

Related Questions