d3.js nvd3.js — Get y-axis min/max values
Asked Answered
F

2

5

I'm using NVD3's lineChart model.

I need to force-set the min and max y-axis values relative to the actual ones, e.g.:

chart.lines.forceY([min/1.1,max*1.1]);

How can I get the current min/max y-axis values?

Fretwork answered 20/11, 2013 at 9:3 Comment(0)
L
5

You can get the current domain through chart.yAxis.scale().domain().

Lampyrid answered 20/11, 2013 at 9:4 Comment(6)
I don't need the domain, but the exact min and max values. In my case chart.yAxis.scale().domain() returns an array: [0, 1]Fretwork
For this, you can use d3.extent on your data.Lampyrid
Yes, I can always get the min and max values from the data but it will be a reduntant calculation. I'm asking if there is a way to query the values directly from the chart object as they are already calculated.Fretwork
Not unless they're set as the domain of the scale.Lampyrid
They are not set as the domain.Fretwork
Then you'll have to recalculate them.Lampyrid
H
2

If you have already set the y-axis domain like so:

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

y.domain([0, d3.max(yourData)]);

you can access the values through y. y[0] is the minimum, y[1] is the max.

Hemistich answered 30/5, 2015 at 8:53 Comment(2)
This is the correct answer. Try also: d3.extent(y.domain()) to return both the lower bound and upper bound values on the y-axis in a simple array.Propose
Addendum: Once the bounded values are known, you can simply use y(min) or y(max) to get the mapped bounds for your specified range.Propose

© 2022 - 2024 — McMap. All rights reserved.