Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logarithmic scale - support of : beginAtZero, zero values, multidataset selection (issue-#9629) #10714

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 58 additions & 10 deletions src/scales/scale.logarithmic.js
@@ -1,4 +1,4 @@
import {finiteOrDefault, isFinite} from '../helpers/helpers.core';
import {callback as call, finiteOrDefault, isFinite} from '../helpers/helpers.core';
import {formatNumber} from '../helpers/helpers.intl';
import {_setMinAndMaxByKey, log10} from '../helpers/helpers.math';
import Scale from '../core/core.scale';
Expand Down Expand Up @@ -39,8 +39,13 @@ function startExp(min, max) {
* @param dataRange the range of the data
* @returns {object[]} array of tick objects
*/
function generateTicks(generationOptions, {min, max}) {
min = finiteOrDefault(generationOptions.min, min);
function generateTicks(generationOptions, dataRange) {
let {min, max} = dataRange;
if (dataRange._zero === true) {
skalimer0 marked this conversation as resolved.
Show resolved Hide resolved
min = Math.pow(10, Math.floor(log10(dataRange._minNotZero)) - 1);
} else {
min = finiteOrDefault(generationOptions.min, min);
}
const ticks = [];
const minExp = log10Floor(min);
let exp = startExp(min, max);
Expand Down Expand Up @@ -102,23 +107,53 @@ export default class LogarithmicScale extends Scale {

parse(raw, index) {
const value = LinearScaleBase.prototype.parse.apply(this, [raw, index]);
if (value === 0) {
this._zero = true;
return undefined;
return isFinite(value) && value >= 0 ? value : null;
}

getMinMax(canStack) {
let {min, max, minDefined, maxDefined} = this.getUserBounds();
let range;
this._zero = false;
this._minNotZero = min;
if (minDefined && maxDefined) {
return {min, max};
}
return isFinite(value) && value > 0 ? value : null;
const metas = this.getMatchingVisibleMetas();
for (let i = 0, ilen = metas.length; i < ilen; ++i) {
range = metas[i].controller.getMinMax(this, canStack);
if (!minDefined) {
min = Math.min(min, range.min);
}
if (!maxDefined) {
max = Math.max(max, range.max);
}
for (let j = 0, jlen = metas[i]._dataset.data.length; j < jlen; ++j) {
if (metas[i]._dataset.data[j] > 0) {
this._minNotZero = Math.min(this._minNotZero, metas[i]._dataset.data[j]);
}
}
}

// Make sure min <= max when only min or max is defined by user and the data is outside that range
min = maxDefined && min > max ? max : min;
max = minDefined && min > max ? min : max;

return {
min: finiteOrDefault(min, finiteOrDefault(max, min)),
max: finiteOrDefault(max, finiteOrDefault(min, max))
};
}

determineDataLimits() {
const {min, max} = this.getMinMax(true);

this.min = isFinite(min) ? Math.max(0, min) : null;
this.max = isFinite(max) ? Math.max(0, max) : null;

if (this.options.beginAtZero) {
skalimer0 marked this conversation as resolved.
Show resolved Hide resolved
if (this.min === 0 || this.options.beginAtZero) {
this._zero = true;
}

skalimer0 marked this conversation as resolved.
Show resolved Hide resolved
// if data has `0` in it or `beginAtZero` is true, min (non zero) value is at bottom
// of scale, and it does not equal suggestedMin, lower the min bound by one exp.
if (this._zero && this.min !== this._suggestedMin && !isFinite(this._userMin)) {
Expand Down Expand Up @@ -185,6 +220,19 @@ export default class LogarithmicScale extends Scale {
return ticks;
}

generateTickLabels(ticks) {
const tickOpts = this.options.ticks;
let i, ilen, tick;
for (i = 0, ilen = ticks.length; i < ilen; i++) {
tick = ticks[i];
if (i === 0 && this._zero === true) {
skalimer0 marked this conversation as resolved.
Show resolved Hide resolved
tick.label = call(tickOpts.callback, [0, i, ticks], this);
} else {
tick.label = call(tickOpts.callback, [tick.value, i, ticks], this);
}
}
}

/**
* @param {number} value
* @return {string}
Expand Down