Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 931
byterussian Avatar asked Mar 04 '14 12:03

byterussian


2 Answers

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.

like image 170
AmeliaBR Avatar answered Nov 13 '22 02:11

AmeliaBR


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

like image 34
card_master Avatar answered Nov 13 '22 02:11

card_master