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;
|
|
|
|
// Default config for a category scale
|
|
|
|
var defaultConfig = {
|
|
|
|
position: "bottom"
|
|
|
|
};
|
|
|
|
|
|
|
|
var DatasetScale = Chart.Scale.extend({
|
2016-04-17 16:33:38 +02:00
|
|
|
// Implement this so that
|
|
|
|
determineDataLimits: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
me.minIndex = 0;
|
|
|
|
me.maxIndex = me.chart.data.labels.length - 1;
|
2016-04-03 05:05:48 +02:00
|
|
|
var findIndex;
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.options.ticks.min !== undefined) {
|
2016-04-03 05:05:48 +02:00
|
|
|
// user specified min value
|
2016-06-04 20:14:16 +02:00
|
|
|
findIndex = helpers.indexOf(me.chart.data.labels, me.options.ticks.min);
|
|
|
|
me.minIndex = findIndex !== -1 ? findIndex : me.minIndex;
|
2016-04-03 05:05:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.options.ticks.max !== undefined) {
|
2016-04-03 05:05:48 +02:00
|
|
|
// user specified max value
|
2016-06-04 20:14:16 +02:00
|
|
|
findIndex = helpers.indexOf(me.chart.data.labels, me.options.ticks.max);
|
|
|
|
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex;
|
2016-04-03 05:05:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
me.min = me.chart.data.labels[me.minIndex];
|
|
|
|
me.max = me.chart.data.labels[me.maxIndex];
|
2016-04-17 16:33:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
buildTicks: function(index) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-04-03 05:05:48 +02:00
|
|
|
// If we are viewing some subset of labels, slice the original array
|
2016-06-04 20:14:16 +02:00
|
|
|
me.ticks = (me.minIndex === 0 && me.maxIndex === me.chart.data.labels.length - 1) ? me.chart.data.labels : me.chart.data.labels.slice(me.minIndex, me.maxIndex + 1);
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getLabelForIndex: function(index, datasetIndex) {
|
|
|
|
return this.ticks[index];
|
|
|
|
},
|
|
|
|
|
|
|
|
// Used to get data value locations. Value can either be an index or a numerical value
|
|
|
|
getPixelForValue: function(value, index, datasetIndex, includeOffset) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-04-03 05:05:48 +02:00
|
|
|
// 1 is added because we need the length but we have the indexes
|
2016-06-04 20:14:16 +02:00
|
|
|
var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - ((me.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
|
2016-04-03 05:05:48 +02:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.isHorizontal()) {
|
|
|
|
var innerWidth = me.width - (me.paddingLeft + me.paddingRight);
|
2016-04-03 05:05:48 +02:00
|
|
|
var valueWidth = innerWidth / offsetAmt;
|
2016-06-04 20:14:16 +02:00
|
|
|
var widthOffset = (valueWidth * (index - me.minIndex)) + me.paddingLeft;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.options.gridLines.offsetGridLines && includeOffset) {
|
2016-02-14 23:06:00 +01:00
|
|
|
widthOffset += (valueWidth / 2);
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
return me.left + Math.round(widthOffset);
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
var innerHeight = me.height - (me.paddingTop + me.paddingBottom);
|
2016-04-03 05:05:48 +02:00
|
|
|
var valueHeight = innerHeight / offsetAmt;
|
2016-06-04 20:14:16 +02:00
|
|
|
var heightOffset = (valueHeight * (index - me.minIndex)) + me.paddingTop;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.options.gridLines.offsetGridLines && includeOffset) {
|
2016-02-14 23:06:00 +01:00
|
|
|
heightOffset += (valueHeight / 2);
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
return me.top + Math.round(heightOffset);
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-04-03 05:05:48 +02:00
|
|
|
},
|
|
|
|
getPixelForTick: function(index, includeOffset) {
|
2016-04-17 16:33:38 +02:00
|
|
|
return this.getPixelForValue(this.ticks[index], index + this.minIndex, null, includeOffset);
|
2016-04-30 14:39:18 +02:00
|
|
|
},
|
2016-05-01 18:40:14 +02:00
|
|
|
getValueForPixel: function(pixel) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var value;
|
|
|
|
var offsetAmt = Math.max((me.ticks.length - ((me.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
|
|
|
|
var horz = me.isHorizontal();
|
|
|
|
var innerDimension = horz ? me.width - (me.paddingLeft + me.paddingRight) : me.height - (me.paddingTop + me.paddingBottom);
|
2016-04-30 14:39:18 +02:00
|
|
|
var valueDimension = innerDimension / offsetAmt;
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.options.gridLines.offsetGridLines) {
|
2016-04-30 14:39:18 +02:00
|
|
|
pixel -= (valueDimension / 2);
|
|
|
|
}
|
2016-06-04 20:14:16 +02:00
|
|
|
pixel -= horz ? me.paddingLeft : me.paddingTop;
|
2016-04-30 14:39:18 +02:00
|
|
|
|
|
|
|
if (pixel <= 0) {
|
|
|
|
value = 0;
|
|
|
|
} else {
|
|
|
|
value = Math.round(pixel / valueDimension);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Chart.scaleService.registerScaleType("category", DatasetScale, defaultConfig);
|
|
|
|
|
|
|
|
};
|