2015-08-09 00:34:19 +02:00
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var root = this,
|
|
|
|
Chart = root.Chart,
|
|
|
|
helpers = Chart.helpers;
|
|
|
|
|
|
|
|
var defaultConfig = {
|
|
|
|
position: "left",
|
|
|
|
|
2015-09-21 01:06:09 +02:00
|
|
|
// scale label
|
|
|
|
scaleLabel: {
|
|
|
|
// actual label
|
|
|
|
labelString: '',
|
|
|
|
// display property
|
|
|
|
show: false,
|
|
|
|
},
|
|
|
|
|
2015-08-09 00:34:19 +02:00
|
|
|
// label settings
|
|
|
|
labels: {
|
2015-09-15 02:32:23 +02:00
|
|
|
template: "<%var remain = value / (Math.pow(10, Math.floor(Chart.helpers.log10(value))));if (remain === 1 || remain === 2 || remain === 5) {%><%=value.toExponential()%><%} else {%><%= null %><%}%>",
|
2015-08-09 00:34:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-24 20:07:40 +02:00
|
|
|
var LogarithmicScale = Chart.Scale.extend({
|
|
|
|
buildTicks: function() {
|
2015-08-09 00:34:19 +02:00
|
|
|
|
2015-09-24 20:07:40 +02:00
|
|
|
// Calculate Range (we may break this out into it's own lifecycle function)
|
|
|
|
|
|
|
|
this.min = null;
|
|
|
|
this.max = null;
|
|
|
|
|
|
|
|
var values = [];
|
|
|
|
|
|
|
|
if (this.options.stacked) {
|
|
|
|
helpers.each(this.data.datasets, function(dataset) {
|
|
|
|
if (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id) {
|
|
|
|
helpers.each(dataset.data, function(rawValue, index) {
|
|
|
|
|
|
|
|
var value = this.getRightValue(rawValue);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
this.min = helpers.min(values);
|
|
|
|
this.max = helpers.max(values);
|
2015-08-09 00:34:19 +02:00
|
|
|
|
|
|
|
} else {
|
2015-09-24 20:07:40 +02:00
|
|
|
helpers.each(this.data.datasets, function(dataset) {
|
|
|
|
if (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id) {
|
|
|
|
helpers.each(dataset.data, function(rawValue, index) {
|
|
|
|
var value = this.getRightValue(rawValue);
|
2015-08-09 00:34:19 +02:00
|
|
|
|
2015-09-24 20:07:40 +02:00
|
|
|
if (this.min === null) {
|
|
|
|
this.min = value;
|
|
|
|
} else if (value < this.min) {
|
|
|
|
this.min = value;
|
|
|
|
}
|
2015-08-09 00:34:19 +02:00
|
|
|
|
2015-09-24 20:07:40 +02:00
|
|
|
if (this.max === null) {
|
|
|
|
this.max = value;
|
|
|
|
} else if (value > this.max) {
|
|
|
|
this.max = value;
|
|
|
|
}
|
|
|
|
}, this);
|
2015-08-09 00:34:19 +02:00
|
|
|
}
|
2015-09-24 20:07:40 +02:00
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-08-09 00:34:19 +02:00
|
|
|
}
|
2015-09-24 20:07:40 +02: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
|
|
|
|
this.ticks = [];
|
2015-08-09 00:34:19 +02:00
|
|
|
|
2015-09-24 20:07:40 +02: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 minExponent = Math.floor(helpers.log10(this.min));
|
|
|
|
var maxExponent = Math.ceil(helpers.log10(this.max));
|
|
|
|
|
|
|
|
for (var exponent = minExponent; exponent < maxExponent; ++exponent) {
|
|
|
|
for (var i = 1; i < 10; ++i) {
|
|
|
|
this.ticks.push(i * Math.pow(10, exponent));
|
|
|
|
}
|
2015-08-09 00:34:19 +02:00
|
|
|
}
|
|
|
|
|
2015-09-24 20:07:40 +02:00
|
|
|
this.ticks.push(1.0 * Math.pow(10, maxExponent));
|
|
|
|
|
2015-08-09 00:34:19 +02: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
|
|
|
|
this.ticks.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we need to update our max and min given the tick values since we have expanded the
|
|
|
|
// range of the scale
|
|
|
|
this.max = helpers.max(this.ticks);
|
|
|
|
this.min = helpers.min(this.ticks);
|
|
|
|
|
|
|
|
if (this.options.reverse) {
|
|
|
|
this.ticks.reverse();
|
|
|
|
|
|
|
|
this.start = this.max;
|
|
|
|
this.end = this.min;
|
|
|
|
} else {
|
|
|
|
this.start = this.min;
|
|
|
|
this.end = this.max;
|
|
|
|
}
|
2015-09-24 20:07:40 +02:00
|
|
|
|
|
|
|
console.log(this.ticks);
|
2015-08-09 00:34:19 +02:00
|
|
|
},
|
|
|
|
buildLabels: function() {
|
|
|
|
// We assume that this has been run after ticks have been generated. We try to figure out
|
|
|
|
// a label for each tick.
|
|
|
|
this.labels = [];
|
|
|
|
|
|
|
|
helpers.each(this.ticks, function(tick, index, ticks) {
|
|
|
|
var label;
|
|
|
|
|
|
|
|
if (this.options.labels.userCallback) {
|
|
|
|
// If the user provided a callback for label generation, use that as first priority
|
|
|
|
label = this.options.labels.userCallback(tick, index, ticks);
|
|
|
|
} else if (this.options.labels.template) {
|
|
|
|
// else fall back to the template string
|
|
|
|
label = helpers.template(this.options.labels.template, {
|
|
|
|
value: tick
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-15 02:32:23 +02:00
|
|
|
this.labels.push(label); // empty string will not render so we're good
|
2015-08-09 00:34:19 +02:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
// Get the correct value. If the value type is object get the x or y based on whether we are horizontal or not
|
|
|
|
getRightValue: function(rawValue) {
|
|
|
|
return typeof rawValue === "object" ? (this.isHorizontal() ? rawValue.x : rawValue.y) : rawValue;
|
|
|
|
},
|
|
|
|
getPixelForValue: function(value) {
|
|
|
|
// This must be called after fit has been run so that
|
|
|
|
// this.left, this.top, this.right, and this.bottom have been defined
|
|
|
|
var pixel;
|
|
|
|
var range = helpers.log10(this.end) - helpers.log10(this.start);
|
|
|
|
|
|
|
|
if (this.isHorizontal()) {
|
|
|
|
if (value === 0) {
|
|
|
|
pixel = this.left + this.paddingLeft;
|
|
|
|
} else {
|
|
|
|
var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
|
|
|
|
pixel = this.left + (innerWidth / range * (helpers.log10(value) - helpers.log10(this.start)));
|
|
|
|
pixel += this.paddingLeft;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Bottom - top since pixels increase downard on a screen
|
|
|
|
if (value === 0) {
|
|
|
|
pixel = this.top + this.paddingTop;
|
2015-09-24 20:07:40 +02:00
|
|
|
} else {
|
2015-08-09 00:34:19 +02:00
|
|
|
var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
|
2015-09-15 01:45:13 +02:00
|
|
|
pixel = (this.bottom - this.paddingBottom) - (innerHeight / range * (helpers.log10(value) - helpers.log10(this.start)));
|
2015-08-09 00:34:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixel;
|
|
|
|
},
|
|
|
|
|
|
|
|
getPointPixelForValue: function(rawValue, index, datasetIndex) {
|
|
|
|
var value = this.getRightValue(rawValue);
|
|
|
|
|
|
|
|
if (this.options.stacked) {
|
|
|
|
var offsetPos = 0;
|
|
|
|
var offsetNeg = 0;
|
|
|
|
|
|
|
|
for (var i = this.data.datasets.length - 1; i > datasetIndex; --i) {
|
|
|
|
if (this.data.datasets[i].data[index] < 0) {
|
|
|
|
offsetNeg += this.data.datasets[i].data[index];
|
|
|
|
} else {
|
|
|
|
offsetPos += this.data.datasets[i].data[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return this.getPixelForValue(offsetNeg + value);
|
|
|
|
} else {
|
|
|
|
return this.getPixelForValue(offsetPos + value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return this.getPixelForValue(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Functions needed for bar charts
|
|
|
|
calculateBarBase: function(datasetIndex, index) {
|
|
|
|
var base = 0;
|
|
|
|
|
|
|
|
if (this.options.stacked) {
|
|
|
|
|
|
|
|
var value = this.data.datasets[datasetIndex].data[index];
|
|
|
|
|
2015-09-18 02:29:41 +02:00
|
|
|
for (var j = 0; j < datasetIndex; j++) {
|
|
|
|
if (this.data.datasets[j].yAxisID === this.id) {
|
|
|
|
base += this.data.datasets[j].data[index];
|
2015-08-09 00:34:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getPixelForValue(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
base = this.getPixelForValue(this.min);
|
|
|
|
|
2015-09-18 02:29:41 +02:00
|
|
|
if (this.min < 0 && this.max < 0) {
|
2015-08-09 00:34:19 +02:00
|
|
|
// All values are negative. Use the top as the base
|
|
|
|
base = this.getPixelForValue(this.max);
|
|
|
|
}
|
|
|
|
|
|
|
|
return base;
|
|
|
|
|
|
|
|
},
|
|
|
|
calculateBarY: function(datasetIndex, index) {
|
|
|
|
var value = this.data.datasets[datasetIndex].data[index];
|
|
|
|
|
|
|
|
if (this.options.stacked) {
|
|
|
|
|
|
|
|
var sumPos = 0,
|
|
|
|
sumNeg = 0;
|
|
|
|
|
|
|
|
for (var i = 0; i < datasetIndex; i++) {
|
|
|
|
if (this.data.datasets[i].data[index] < 0) {
|
|
|
|
sumNeg += this.data.datasets[i].data[index] || 0;
|
|
|
|
} else {
|
|
|
|
sumPos += this.data.datasets[i].data[index] || 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return this.getPixelForValue(sumNeg + value);
|
|
|
|
} else {
|
|
|
|
return this.getPixelForValue(sumPos + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getPixelForValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
var offset = 0;
|
|
|
|
|
|
|
|
for (var j = datasetIndex; j < this.data.datasets.length; j++) {
|
|
|
|
if (j === datasetIndex && value) {
|
|
|
|
offset += value;
|
|
|
|
} else {
|
|
|
|
offset = offset + value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getPixelForValue(value);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Chart.scaleService.registerScaleType("logarithmic", LogarithmicScale, defaultConfig);
|
|
|
|
|
|
|
|
}).call(this);
|