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) {
|
2016-05-08 15:24:45 +02:00
|
|
|
var remain = value / (Math.pow(10, Math.floor(helpers.log10(value))));
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
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() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var opts = me.options;
|
2016-05-08 15:24:45 +02:00
|
|
|
var tickOpts = opts.ticks;
|
2016-06-04 20:14:16 +02:00
|
|
|
var chart = me.chart;
|
2016-05-08 15:24:45 +02:00
|
|
|
var data = chart.data;
|
|
|
|
var datasets = data.datasets;
|
|
|
|
var getValueOrDefault = helpers.getValueOrDefault;
|
2016-06-04 20:14:16 +02:00
|
|
|
var isHorizontal = me.isHorizontal();
|
2016-05-08 15:24:45 +02:00
|
|
|
function IDMatches(meta) {
|
2016-06-04 20:14:16 +02:00
|
|
|
return isHorizontal ? meta.xAxisID === me.id : meta.yAxisID === me.id;
|
2016-05-08 15:24:45 +02:00
|
|
|
}
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// Calculate Range
|
2016-06-04 20:14:16 +02:00
|
|
|
me.min = null;
|
|
|
|
me.max = null;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-08 15:24:45 +02:00
|
|
|
if (opts.stacked) {
|
2016-02-14 23:06:00 +01:00
|
|
|
var valuesPerType = {};
|
|
|
|
|
2016-05-08 15:24:45 +02:00
|
|
|
helpers.each(datasets, function(dataset, datasetIndex) {
|
|
|
|
var meta = chart.getDatasetMeta(datasetIndex);
|
|
|
|
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
|
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-06-04 20:14:16 +02:00
|
|
|
var value = +me.getRightValue(rawValue);
|
2016-04-23 10:57:29 +02:00
|
|
|
if (isNaN(value) || meta.data[index].hidden) {
|
2016-02-14 23:06:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
values[index] = values[index] || 0;
|
|
|
|
|
2016-05-08 15:24:45 +02:00
|
|
|
if (opts.relativePoints) {
|
2016-02-14 23:06:00 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-05-08 15:24:45 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-05-08 15:24:45 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
helpers.each(valuesPerType, function(valuesForType) {
|
|
|
|
var minVal = helpers.min(valuesForType);
|
|
|
|
var maxVal = helpers.max(valuesForType);
|
2016-06-04 20:14:16 +02:00
|
|
|
me.min = me.min === null ? minVal : Math.min(me.min, minVal);
|
|
|
|
me.max = me.max === null ? maxVal : Math.max(me.max, maxVal);
|
2016-05-08 15:24:45 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
} else {
|
2016-05-08 15:24:45 +02:00
|
|
|
helpers.each(datasets, function(dataset, datasetIndex) {
|
|
|
|
var meta = chart.getDatasetMeta(datasetIndex);
|
|
|
|
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
|
2016-02-14 23:06:00 +01:00
|
|
|
helpers.each(dataset.data, function(rawValue, index) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var value = +me.getRightValue(rawValue);
|
2016-04-23 10:57:29 +02:00
|
|
|
if (isNaN(value) || meta.data[index].hidden) {
|
2016-02-14 23:06:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.min === null) {
|
|
|
|
me.min = value;
|
|
|
|
} else if (value < me.min) {
|
|
|
|
me.min = value;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.max === null) {
|
|
|
|
me.max = value;
|
|
|
|
} else if (value > me.max) {
|
|
|
|
me.max = value;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-05-08 15:24:45 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-05-08 15:24:45 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
me.min = getValueOrDefault(tickOpts.min, me.min);
|
|
|
|
me.max = getValueOrDefault(tickOpts.max, me.max);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.min === me.max) {
|
|
|
|
if (me.min !== 0 && me.min !== null) {
|
|
|
|
me.min = Math.pow(10, Math.floor(helpers.log10(me.min)) - 1);
|
|
|
|
me.max = Math.pow(10, Math.floor(helpers.log10(me.max)) + 1);
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
me.min = 1;
|
|
|
|
me.max = 10;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
buildTicks: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var opts = me.options;
|
2016-05-08 15:24:45 +02:00
|
|
|
var tickOpts = opts.ticks;
|
|
|
|
var getValueOrDefault = helpers.getValueOrDefault;
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// 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-06-04 20:14:16 +02:00
|
|
|
var ticks = me.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
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
var tickVal = getValueOrDefault(tickOpts.min, Math.pow(10, Math.floor(helpers.log10(me.min))));
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
while (tickVal < me.max) {
|
2016-05-08 15:24:45 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-08 15:24:45 +02:00
|
|
|
var lastTick = getValueOrDefault(tickOpts.max, tickVal);
|
|
|
|
ticks.push(lastTick);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (!me.isHorizontal()) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// We are in a vertical orientation. The top value is the highest. So reverse the array
|
2016-05-08 15:24:45 +02:00
|
|
|
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-06-04 20:14:16 +02:00
|
|
|
me.max = helpers.max(ticks);
|
|
|
|
me.min = helpers.min(ticks);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-08 15:24:45 +02:00
|
|
|
if (tickOpts.reverse) {
|
|
|
|
ticks.reverse();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
me.start = me.max;
|
|
|
|
me.end = me.min;
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
me.start = me.min;
|
|
|
|
me.end = me.max;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
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]);
|
|
|
|
},
|
2016-06-18 11:00:11 +02:00
|
|
|
getPixelForTick: function(index) {
|
|
|
|
return this.getPixelForValue(this.tickValues[index]);
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
2016-06-18 11:00:11 +02:00
|
|
|
getPixelForValue: function(value) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-05-08 15:24:45 +02:00
|
|
|
var innerDimension;
|
2016-02-14 23:06:00 +01:00
|
|
|
var pixel;
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
var start = me.start;
|
|
|
|
var newVal = +me.getRightValue(value);
|
|
|
|
var range = helpers.log10(me.end) - helpers.log10(start);
|
|
|
|
var paddingTop = me.paddingTop;
|
|
|
|
var paddingBottom = me.paddingBottom;
|
|
|
|
var paddingLeft = me.paddingLeft;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.isHorizontal()) {
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
if (newVal === 0) {
|
2016-06-04 20:14:16 +02:00
|
|
|
pixel = me.left + paddingLeft;
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
innerDimension = me.width - (paddingLeft + me.paddingRight);
|
|
|
|
pixel = me.left + (innerDimension / range * (helpers.log10(newVal) - helpers.log10(start)));
|
2016-05-08 15:24:45 +02:00
|
|
|
pixel += paddingLeft;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Bottom - top since pixels increase downard on a screen
|
|
|
|
if (newVal === 0) {
|
2016-06-04 20:14:16 +02:00
|
|
|
pixel = me.top + paddingTop;
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
innerDimension = me.height - (paddingTop + paddingBottom);
|
|
|
|
pixel = (me.bottom - paddingBottom) - (innerDimension / range * (helpers.log10(newVal) - helpers.log10(start)));
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixel;
|
2016-04-21 13:48:47 +02:00
|
|
|
},
|
|
|
|
getValueForPixel: function(pixel) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var range = helpers.log10(me.end) - helpers.log10(me.start);
|
2016-06-18 11:00:11 +02:00
|
|
|
var value, innerDimension;
|
2016-04-21 13:48:47 +02:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.isHorizontal()) {
|
|
|
|
innerDimension = me.width - (me.paddingLeft + me.paddingRight);
|
|
|
|
value = me.start * Math.pow(10, (pixel - me.left - me.paddingLeft) * range / innerDimension);
|
2016-04-21 13:48:47 +02:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
innerDimension = me.height - (me.paddingTop + me.paddingBottom);
|
|
|
|
value = Math.pow(10, (me.bottom - me.paddingBottom - pixel) * range / innerDimension) / me.start;
|
2016-04-21 13:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-04-23 19:27:10 +02:00
|
|
|
return value;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Chart.scaleService.registerScaleType("logarithmic", LogarithmicScale, defaultConfig);
|
|
|
|
|
|
|
|
};
|