const unit = stepSize || 1;
const maxNumSpaces = generationOptions.maxTicks - 1;
const {min: rmin, max: rmax} = dataRange;
+ const minDefined = !isNullOrUndef(min);
+ const maxDefined = !isNullOrUndef(max);
let spacing = niceNum((rmax - rmin) / maxNumSpaces / unit) * unit;
let factor, niceMin, niceMax, numSpaces;
// Beyond MIN_SPACING floating point numbers being to lose precision
// such that we can't do the math necessary to generate ticks
- if (spacing < MIN_SPACING && isNullOrUndef(min) && isNullOrUndef(max)) {
+ if (spacing < MIN_SPACING && !minDefined && !maxDefined) {
return [{value: rmin}, {value: rmax}];
}
niceMax = Math.ceil(rmax / spacing) * spacing;
// If min, max and stepSize is set and they make an evenly spaced scale use it.
- if (stepSize && !isNullOrUndef(min) && !isNullOrUndef(max)) {
+ if (stepSize && minDefined && maxDefined) {
// If very close to our whole number, use it.
if (almostWhole((max - min) / stepSize, spacing / 1000)) {
niceMin = min;
niceMin = Math.round(niceMin * factor) / factor;
niceMax = Math.round(niceMax * factor) / factor;
- ticks.push({value: isNullOrUndef(min) ? niceMin : min});
- for (let j = 1; j < numSpaces; ++j) {
+
+ let j = 0;
+ if (minDefined) {
+ ticks.push({value: min});
+ // If the niceMin is smaller than min, skip it
+ if (niceMin < min) {
+ j++;
+ }
+ // If the next nice tick is close to min, skip that too
+ if (almostWhole(Math.round((niceMin + j * spacing) * factor) / factor / min, spacing / 1000)) {
+ j++;
+ }
+ }
+
+ for (; j < numSpaces; ++j) {
ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor});
}
- ticks.push({value: isNullOrUndef(max) ? niceMax : max});
+
+ if (maxDefined) {
+ // If the previous tick is close to max, replace it with max, else add max
+ if (almostWhole(ticks[ticks.length - 1].value / max, spacing / 1000)) {
+ ticks[ticks.length - 1].value = max;
+ } else {
+ ticks.push({value: max});
+ }
+ } else {
+ ticks.push({value: niceMax});
+ }
return ticks;
}
expect(chart.scales.r.min).toBe(-1010);
expect(chart.scales.r.max).toBe(1010);
- expect(getLabels(chart.scales.r)).toEqual(['-1,010', '-1,000', '-500', '0', '500', '1,000', '1,010']);
+ expect(getLabels(chart.scales.r)).toEqual(['-1,010', '-500', '0', '500', '1,010']);
});
it('should forcibly include 0 in the range if the beginAtZero option is used', function() {