Skip to content

Commit

Permalink
When calculating the ticks, should use the date that is closest
Browse files Browse the repository at this point in the history
to the tick date, even if that date is smaller than the tick date.
Currently it only uses the closest date that is greater than
the tick date.
  • Loading branch information
sf-wind committed Nov 18, 2015
1 parent 84b8f2b commit 497de40
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/scale/financetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,16 @@ module.exports = function(d3_scale_linear, d3_time, d3_bisect, techan_util_rebin
return function(d) {
var value = visibleDomainLookup[+d];
if (value !== undefined) return visibleDomain[value];
return visibleDomain[d3_bisect(visibleDomain, d)];
var index = d3_bisect(visibleDomain, d);
if (index > 0) {
// d3_bisect gets the index of the closest value that is the greater than d,
// which may not be the value that is closest to d.
// If the closest value that is smaller than d is closer, choose that instead.
if ((+d - (+visibleDomain[index-1])) < (+visibleDomain[index] - +d)) {
index--;
}
}
return visibleDomain[index];
};
}

Expand Down
4 changes: 3 additions & 1 deletion test/spec/bundle/scale/financetimeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ techanModule('scale/financetime', function(specBuilder) {

it('Then ticks with specified interval and step count returns that number', function() {
expect(financetime.ticks(d3.time.day, 2)).toEqual([
new Date(2012,4,18),
new Date(2012,4,21),
new Date(2012,4,23),
new Date(2012,4,25),
Expand Down Expand Up @@ -425,6 +426,7 @@ techanModule('scale/financetime', function(specBuilder) {

it('Then ticks with specified interval and step count returns that number', function() {
expect(financetime.ticks(d3.time.day, 2)).toEqual([
new Date(2012,4,18),
new Date(2012,4,21),
new Date(2012,4,23),
new Date(2012,4,25),
Expand Down Expand Up @@ -627,4 +629,4 @@ techanModule('scale/financetime', function(specBuilder) {
});
});
});
});
});

0 comments on commit 497de40

Please sign in to comment.