Nvd3: How prevent to display chart between -1 and 1 if have all y values 0?
Asked Answered
M

2

10

Have a little modified version of linePlusBarChart model(), when pass data that has all y values setted to zero Y axis show me a range between 1 and -1. Is possible to set a range between 0 and 1?

Have tried with chart.yAxis.scale().domain([0]); and chart.forceY([0]) but nothing.

Menorah answered 4/3, 2014 at 12:52 Comment(4)
You need to pass a two-element array to forceY -- #11767379Empoison
have tried but nothing, problem still presentMenorah
could you put your code on JSFiddleGoodspeed
I should have said, my issue is with the stackedAreaChart, where apparently yDomain doesn't work as expected.Baltic
A
3

forceY forces the domain to include the values you pass in, it doesn't shrink the domain created from the data. To set a specific domain, you set chart.yDomain([0,1]). However, that would set the domain to [0,1] regardless of what your data is. As I understand it, you only want to change the behaviour when all your y-values are 0.

For that, try chart.forceY([1]). Now, when NVD3 tries to figure out the domain, it will see both the zero values from the data and the 1 from the force statement. So it will have a valid domain and it won't have to make up a domain by adding and subtracting 1 from the data value.

Antifebrile answered 4/3, 2014 at 17:59 Comment(0)
U
1

If you follow the example provided by the nvd3 site example linePlusBarChart

You can insert one line in the javascript

chart.lines.forceY([0,1]);

Here is the test code

Usurp answered 13/3, 2014 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.