2016-02-12 04:30:53 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
var helpers = Chart.helpers;
|
|
|
|
|
|
|
|
var defaultConfig = {
|
|
|
|
position: "left",
|
|
|
|
|
|
|
|
// label settings
|
|
|
|
ticks: {
|
|
|
|
callback: function(value, index, arr) {
|
|
|
|
var remain = value / (Math.pow(10, Math.floor(Chart.helpers.log10(value))));
|
|
|
|
|
|
|
|
if (remain === 1 || remain === 2 || remain === 5 || index === 0 || index === arr.length - 1) {
|
|
|
|
return value.toExponential();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var LogarithmicScale = Chart.Scale.extend({
|
|
|
|
determineDataLimits: function() {
|
|
|
|
// Calculate Range
|
|
|
|
this.min = null;
|
|
|
|
this.max = null;
|
|
|
|
|
|
|
|
if (this.options.stacked) {
|
|
|
|
var valuesPerType = {};
|
|
|
|
|
2016-04-21 17:11:52 +02:00
|
|
|
helpers.each(this.chart.data.datasets, function(dataset, datasetIndex) {
|
|
|
|
var meta = this.chart.getDatasetMeta(datasetIndex);
|
2016-04-21 19:12:12 +02:00
|
|
|
if (this.chart.isDatasetVisible(datasetIndex) && (this.isHorizontal() ? meta.xAxisID === this.id : meta.yAxisID === this.id)) {
|
2016-04-21 23:43:47 +02:00
|
|
|
if (valuesPerType[meta.type] === undefined) {
|
|
|
|
valuesPerType[meta.type] = [];
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
helpers.each(dataset.data, function(rawValue, index) {
|
2016-04-21 23:43:47 +02:00
|
|
|
var values = valuesPerType[meta.type];
|
2016-02-14 23:06:00 +01:00
|
|
|
var value = +this.getRightValue(rawValue);
|
|
|
|
if (isNaN(value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
values[index] = values[index] || 0;
|
|
|
|
|
|
|
|
if (this.options.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;
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
helpers.each(valuesPerType, function(valuesForType) {
|
|
|
|
var minVal = helpers.min(valuesForType);
|
|
|
|
var maxVal = helpers.max(valuesForType);
|
|
|
|
this.min = this.min === null ? minVal : Math.min(this.min, minVal);
|
|
|
|
this.max = this.max === null ? maxVal : Math.max(this.max, maxVal);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
} else {
|
2016-04-21 17:11:52 +02:00
|
|
|
helpers.each(this.chart.data.datasets, function(dataset, datasetIndex) {
|
|
|
|
var meta = this.chart.getDatasetMeta(datasetIndex);
|
2016-04-21 19:12:12 +02:00
|
|
|
if (this.chart.isDatasetVisible(dataset, datasetIndex) && (this.isHorizontal() ? meta.xAxisID === this.id : meta.yAxisID === this.id)) {
|
2016-02-14 23:06:00 +01:00
|
|
|
helpers.each(dataset.data, function(rawValue, index) {
|
|
|
|
var value = +this.getRightValue(rawValue);
|
|
|
|
if (isNaN(value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.min === null) {
|
|
|
|
this.min = value;
|
|
|
|
} else if (value < this.min) {
|
|
|
|
this.min = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.max === null) {
|
|
|
|
this.max = value;
|
|
|
|
} else if (value > this.max) {
|
|
|
|
this.max = value;
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.min = this.options.ticks.min !== undefined ? this.options.ticks.min : this.min;
|
|
|
|
this.max = this.options.ticks.max !== undefined ? this.options.ticks.max : this.max;
|
|
|
|
|
|
|
|
if (this.min === this.max) {
|
|
|
|
if (this.min !== 0 && this.min !== null) {
|
|
|
|
this.min = Math.pow(10, Math.floor(helpers.log10(this.min)) - 1);
|
|
|
|
this.max = Math.pow(10, Math.floor(helpers.log10(this.max)) + 1);
|
|
|
|
} else {
|
|
|
|
this.min = 1;
|
|
|
|
this.max = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
buildTicks: function() {
|
|
|
|
// Reset the ticks array. Later on, we will draw a grid line at these positions
|
|
|
|
// The array simply contains the numerical value of the spots where ticks will be
|
2016-03-17 00:39:09 +01:00
|
|
|
this.ticks = [];
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Figure out what the max number of ticks we can support it is based on the size of
|
|
|
|
// the axis area. For now, we say that the minimum tick spacing in pixels must be 50
|
|
|
|
// We also limit the maximum number of ticks to 11 which gives a nice 10 squares on
|
|
|
|
// the graph
|
|
|
|
|
|
|
|
var tickVal = this.options.ticks.min !== undefined ? this.options.ticks.min : Math.pow(10, Math.floor(helpers.log10(this.min)));
|
|
|
|
|
|
|
|
while (tickVal < this.max) {
|
2016-03-17 00:39:09 +01:00
|
|
|
this.ticks.push(tickVal);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
var exp = Math.floor(helpers.log10(tickVal));
|
|
|
|
var significand = Math.floor(tickVal / Math.pow(10, exp)) + 1;
|
|
|
|
|
|
|
|
if (significand === 10) {
|
|
|
|
significand = 1;
|
|
|
|
++exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
tickVal = significand * Math.pow(10, exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastTick = this.options.ticks.max !== undefined ? this.options.ticks.max : tickVal;
|
2016-03-17 00:39:09 +01:00
|
|
|
this.ticks.push(lastTick);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
if (this.options.position === "left" || this.options.position === "right") {
|
|
|
|
// We are in a vertical orientation. The top value is the highest. So reverse the array
|
2016-03-17 00:39:09 +01:00
|
|
|
this.ticks.reverse();
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we need to update our max and min given the tick values since we have expanded the
|
|
|
|
// range of the scale
|
2016-03-17 00:39:09 +01:00
|
|
|
this.max = helpers.max(this.ticks);
|
|
|
|
this.min = helpers.min(this.ticks);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
if (this.options.ticks.reverse) {
|
2016-03-17 00:39:09 +01:00
|
|
|
this.ticks.reverse();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
this.start = this.max;
|
|
|
|
this.end = this.min;
|
|
|
|
} else {
|
|
|
|
this.start = this.min;
|
|
|
|
this.end = this.max;
|
|
|
|
}
|
2016-03-17 00:39:09 +01:00
|
|
|
},
|
|
|
|
convertTicksToLabels: function() {
|
|
|
|
this.tickValues = this.ticks.slice();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-03-17 00:39:09 +01:00
|
|
|
Chart.Scale.prototype.convertTicksToLabels.call(this);
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
|
|
|
// Get the correct tooltip label
|
|
|
|
getLabelForIndex: function(index, datasetIndex) {
|
|
|
|
return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]);
|
|
|
|
},
|
|
|
|
getPixelForTick: function(index, includeOffset) {
|
|
|
|
return this.getPixelForValue(this.tickValues[index], null, null, includeOffset);
|
|
|
|
},
|
|
|
|
getPixelForValue: function(value, index, datasetIndex, includeOffset) {
|
|
|
|
var pixel;
|
|
|
|
|
|
|
|
var newVal = +this.getRightValue(value);
|
|
|
|
var range = helpers.log10(this.end) - helpers.log10(this.start);
|
|
|
|
|
|
|
|
if (this.isHorizontal()) {
|
|
|
|
|
|
|
|
if (newVal === 0) {
|
|
|
|
pixel = this.left + this.paddingLeft;
|
|
|
|
} else {
|
|
|
|
var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
|
|
|
|
pixel = this.left + (innerWidth / range * (helpers.log10(newVal) - helpers.log10(this.start)));
|
|
|
|
pixel += this.paddingLeft;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Bottom - top since pixels increase downard on a screen
|
|
|
|
if (newVal === 0) {
|
|
|
|
pixel = this.top + this.paddingTop;
|
|
|
|
} else {
|
|
|
|
var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
|
|
|
|
pixel = (this.bottom - this.paddingBottom) - (innerHeight / range * (helpers.log10(newVal) - helpers.log10(this.start)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
Chart.scaleService.registerScaleType("logarithmic", LogarithmicScale, defaultConfig);
|
|
|
|
|
|
|
|
};
|