Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- issue chartjs#4572: logarithmic type if all numbers are zero browser crashes "Out of memory"
 - issue chartjs#4703: [BUG] Browser unresponsive on bubble chart with logarithmic axes
  • Loading branch information
jcopperfield authored and etimberg committed Dec 12, 2017
1 parent bb5598a commit 95fe20a
Show file tree
Hide file tree
Showing 2 changed files with 435 additions and 121 deletions.
59 changes: 41 additions & 18 deletions src/scales/scale.logarithmic.js
Expand Up @@ -18,11 +18,9 @@ module.exports = function(Chart) {
determineDataLimits: function() {
var me = this;
var opts = me.options;
var tickOpts = opts.ticks;
var chart = me.chart;
var data = chart.data;
var datasets = data.datasets;
var valueOrDefault = helpers.valueOrDefault;
var isHorizontal = me.isHorizontal();
function IDMatches(meta) {
return isHorizontal ? meta.xAxisID === me.id : meta.yAxisID === me.id;
Expand Down Expand Up @@ -68,27 +66,23 @@ module.exports = function(Chart) {
helpers.each(dataset.data, function(rawValue, index) {
var values = valuesPerStack[key];
var value = +me.getRightValue(rawValue);
if (isNaN(value) || meta.data[index].hidden) {
// invalid, hidden and negative values are ignored
if (isNaN(value) || meta.data[index].hidden || value < 0) {
return;
}

values[index] = values[index] || 0;

if (opts.relativePoints) {
values[index] = 100;
} else {
// Don't need to split positive and negative since the log scale can't handle a 0 crossing
values[index] += value;
}
values[index] += value;
});
}
});

helpers.each(valuesPerStack, function(valuesForType) {
var minVal = helpers.min(valuesForType);
var maxVal = helpers.max(valuesForType);
me.min = me.min === null ? minVal : Math.min(me.min, minVal);
me.max = me.max === null ? maxVal : Math.max(me.max, maxVal);
if (valuesForType.length > 0) {
var minVal = helpers.min(valuesForType);
var maxVal = helpers.max(valuesForType);
me.min = me.min === null ? minVal : Math.min(me.min, minVal);
me.max = me.max === null ? maxVal : Math.max(me.max, maxVal);
}
});

} else {
Expand All @@ -97,7 +91,8 @@ module.exports = function(Chart) {
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
helpers.each(dataset.data, function(rawValue, index) {
var value = +me.getRightValue(rawValue);
if (isNaN(value) || meta.data[index].hidden) {
// invalid, hidden and negative values are ignored
if (isNaN(value) || meta.data[index].hidden || value < 0) {
return;
}

Expand All @@ -121,6 +116,17 @@ module.exports = function(Chart) {
});
}

// Common base implementation to handle ticks.min, ticks.max
this.handleTickRangeOptions();
},
handleTickRangeOptions: function() {
var me = this;
var opts = me.options;
var tickOpts = opts.ticks;
var valueOrDefault = helpers.valueOrDefault;
var DEFAULT_MIN = 1;
var DEFAULT_MAX = 10;

me.min = valueOrDefault(tickOpts.min, me.min);
me.max = valueOrDefault(tickOpts.max, me.max);

Expand All @@ -129,8 +135,25 @@ module.exports = function(Chart) {
me.min = Math.pow(10, Math.floor(helpers.log10(me.min)) - 1);
me.max = Math.pow(10, Math.floor(helpers.log10(me.max)) + 1);
} else {
me.min = 1;
me.max = 10;
me.min = DEFAULT_MIN;
me.max = DEFAULT_MAX;
}
}
if (me.min === null) {
me.min = Math.pow(10, Math.floor(helpers.log10(me.max)) - 1);
}
if (me.max === null) {
me.max = me.min !== 0
? Math.pow(10, Math.floor(helpers.log10(me.min)) + 1)
: DEFAULT_MAX;
}
if (me.minNotZero === null) {
if (me.min > 0) {
me.minNotZero = me.min;
} else if (me.max < 1) {
me.minNotZero = Math.pow(10, Math.floor(helpers.log10(me.max)));
} else {
me.minNotZero = DEFAULT_MIN;
}
}
},
Expand Down

0 comments on commit 95fe20a

Please sign in to comment.