2016-09-14 20:05:19 +02:00
|
|
|
'use strict';
|
2016-02-12 04:30:53 +01:00
|
|
|
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// Default config for a category scale
|
|
|
|
var defaultConfig = {
|
2016-09-14 20:05:19 +02:00
|
|
|
position: 'bottom'
|
2016-02-14 23:06:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var DatasetScale = Chart.Scale.extend({
|
2016-07-09 17:22:25 +02:00
|
|
|
/**
|
Spelling fixes (#3522)
Including: actually, aspect, bottom, changes, characters, datasets, divisible, downward, guidelines, instance, instances, interrupted, item, javascript, label, lifecycle, lines, plugins, manually, milliseconds, nearest, occurring, position, predefined, recalculate, tooltip, those
2016-10-27 21:18:31 +02:00
|
|
|
* Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those
|
2016-07-09 17:22:25 +02:00
|
|
|
* else fall back to data.labels
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
getLabels: function() {
|
|
|
|
var data = this.chart.data;
|
2017-07-19 12:41:17 +02:00
|
|
|
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
|
2016-07-09 17:22:25 +02:00
|
|
|
},
|
2017-04-26 05:29:40 +02:00
|
|
|
|
2016-04-17 16:33:38 +02:00
|
|
|
determineDataLimits: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-08-25 18:52:12 +02:00
|
|
|
var labels = me.getLabels();
|
2016-06-04 20:14:16 +02:00
|
|
|
me.minIndex = 0;
|
2016-07-09 17:22:25 +02:00
|
|
|
me.maxIndex = 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
|
2017-06-24 11:46:06 +02:00
|
|
|
findIndex = labels.indexOf(me.options.ticks.min);
|
2016-06-04 20:14:16 +02:00
|
|
|
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
|
2017-06-24 11:46:06 +02:00
|
|
|
findIndex = labels.indexOf(me.options.ticks.max);
|
2016-06-04 20:14:16 +02:00
|
|
|
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex;
|
2016-04-03 05:05:48 +02:00
|
|
|
}
|
|
|
|
|
2016-07-09 17:22:25 +02:00
|
|
|
me.min = labels[me.minIndex];
|
|
|
|
me.max = labels[me.maxIndex];
|
2016-04-17 16:33:38 +02:00
|
|
|
},
|
|
|
|
|
2016-06-18 11:00:11 +02:00
|
|
|
buildTicks: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-07-09 17:22:25 +02:00
|
|
|
var labels = me.getLabels();
|
2016-04-03 05:05:48 +02:00
|
|
|
// If we are viewing some subset of labels, slice the original array
|
2016-07-09 17:22:25 +02:00
|
|
|
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
|
|
|
|
2016-09-10 03:03:59 +02:00
|
|
|
getLabelForIndex: function(index, datasetIndex) {
|
|
|
|
var me = this;
|
|
|
|
var data = me.chart.data;
|
|
|
|
var isHorizontal = me.isHorizontal();
|
|
|
|
|
2016-11-29 00:28:39 +01:00
|
|
|
if (data.yLabels && !isHorizontal) {
|
2016-09-10 03:03:59 +02:00
|
|
|
return me.getRightValue(data.datasets[datasetIndex].data[index]);
|
|
|
|
}
|
2016-11-29 00:28:39 +01:00
|
|
|
return me.ticks[index - me.minIndex];
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-03-28 00:02:56 +02:00
|
|
|
// If value is a data object, then index is the index in the data array,
|
|
|
|
// not the index of the scale. We need to change that.
|
|
|
|
var valueCategory;
|
|
|
|
if (value !== undefined && value !== null) {
|
|
|
|
valueCategory = me.isHorizontal() ? value.x : value.y;
|
|
|
|
}
|
|
|
|
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
|
2016-07-09 17:22:25 +02:00
|
|
|
var labels = me.getLabels();
|
2017-03-28 00:02:56 +02:00
|
|
|
value = valueCategory || value;
|
2016-07-09 17:22:25 +02:00
|
|
|
var idx = labels.indexOf(value);
|
|
|
|
index = idx !== -1 ? idx : index;
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.isHorizontal()) {
|
2016-10-30 18:14:59 +01:00
|
|
|
var valueWidth = me.width / offsetAmt;
|
|
|
|
var widthOffset = (valueWidth * (index - me.minIndex));
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-09-03 10:53:32 +02:00
|
|
|
if (me.options.gridLines.offsetGridLines && includeOffset || me.maxIndex === me.minIndex && includeOffset) {
|
2016-02-14 23:06:00 +01:00
|
|
|
widthOffset += (valueWidth / 2);
|
2016-09-03 10:53:32 +02:00
|
|
|
}
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
return me.left + Math.round(widthOffset);
|
2016-09-15 02:23:30 +02:00
|
|
|
}
|
2016-10-30 18:14:59 +01:00
|
|
|
var valueHeight = me.height / offsetAmt;
|
|
|
|
var heightOffset = (valueHeight * (index - me.minIndex));
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-09-15 02:23:30 +02:00
|
|
|
if (me.options.gridLines.offsetGridLines && includeOffset) {
|
|
|
|
heightOffset += (valueHeight / 2);
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
2016-09-15 02:23:30 +02:00
|
|
|
|
|
|
|
return me.top + Math.round(heightOffset);
|
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();
|
2016-10-30 18:14:59 +01:00
|
|
|
var valueDimension = (horz ? me.width : me.height) / offsetAmt;
|
2016-04-30 14:39:18 +02:00
|
|
|
|
2016-06-29 13:00:09 +02:00
|
|
|
pixel -= horz ? me.left : me.top;
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.options.gridLines.offsetGridLines) {
|
2016-04-30 14:39:18 +02:00
|
|
|
pixel -= (valueDimension / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pixel <= 0) {
|
|
|
|
value = 0;
|
|
|
|
} else {
|
|
|
|
value = Math.round(pixel / valueDimension);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2016-07-09 17:22:25 +02:00
|
|
|
},
|
|
|
|
getBasePixel: function() {
|
|
|
|
return this.bottom;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-14 20:05:19 +02:00
|
|
|
Chart.scaleService.registerScaleType('category', DatasetScale, defaultConfig);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-09-14 20:05:19 +02:00
|
|
|
};
|