Skip to content

Commit

Permalink
Merge pull request #2105 from dmapper/fix-y-axis-tick-number
Browse files Browse the repository at this point in the history
[discreteBarChart] Fix y axis ticks number calculation.
  • Loading branch information
liquidpele committed Mar 1, 2018
2 parents 2ac1b65 + e8432bd commit 4bb381d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/models/discreteBarChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ nv.models.discreteBarChart = function() {
if (showYAxis) {
yAxis
.scale(y)
._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
._ticks( nv.utils.calcTicksY(availableHeight/36, data, discretebar.y()) )
.tickSize( -availableWidth, 0);

g.select('.nv-y.nv-axis').call(yAxis);
Expand Down
30 changes: 26 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,33 @@ nv.utils.calcTicksX = function(numTicks, data) {


/*
returns number of ticks to actually use on Y axis, based on chart data
*/
nv.utils.calcTicksY = function(numTicks, data) {
// currently uses the same logic but we can adjust here if needed later
returns number of ticks to actually use on Y axis, based on chart data
*/
nv.utils.calcTicksY = function(numTicks, data, getY) {
if (getY) {
// find max number of values from all data streams
var numValues = 1;
for (var i=0; i < data.length; i += 1) {
var values = data[i] && data[i].values ? data[i].values : [];
var maxValue;
for (var j=0; j < values.length; j += 1) {
maxValue = values[j] && getY(values[j]) ? getY(values[j]): 0;
numValues = maxValue > numValues ? maxValue : numValues;
}
}
nv.log("Requested number of ticks: ", numTicks);
nv.log("Calculated max values to be: ", numValues);
// make sure we don't have more ticks than values to avoid duplicates
numTicks = numTicks > numValues ? numValues - 1 : numTicks;
// make sure we have at least one tick
numTicks = numTicks < 1 ? 1 : numTicks;
// make sure it's an integer
numTicks = Math.floor(numTicks);
nv.log("Calculating tick count as: ", numTicks);
return numTicks;
} else {
return nv.utils.calcTicksX(numTicks, data);
}
};


Expand Down

0 comments on commit 4bb381d

Please sign in to comment.