Chart.js/src/scales/scale.category.js

98 lines
3.3 KiB
JavaScript
Raw Normal View History

"use strict";
module.exports = function(Chart) {
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() {
this.minIndex = 0;
this.maxIndex = this.chart.data.labels.length;
2016-04-03 05:05:48 +02:00
var findIndex;
if (this.options.ticks.min !== undefined) {
// user specified min value
2016-04-03 15:15:11 +02:00
findIndex = helpers.indexOf(this.chart.data.labels, this.options.ticks.min);
2016-04-17 16:33:38 +02:00
this.minIndex = findIndex !== -1 ? findIndex : this.minIndex;
2016-04-03 05:05:48 +02:00
}
if (this.options.ticks.max !== undefined) {
// user specified max value
findIndex = helpers.indexOf(this.chart.data.labels, this.options.ticks.max);
2016-04-17 16:33:38 +02:00
this.maxIndex = findIndex !== -1 ? findIndex : this.maxIndex;
2016-04-03 05:05:48 +02:00
}
2016-04-17 16:33:38 +02:00
this.min = this.chart.data.labels[this.minIndex];
this.max = this.chart.data.labels[this.maxIndex];
},
buildTicks: function(index) {
2016-04-03 05:05:48 +02:00
// If we are viewing some subset of labels, slice the original array
2016-04-17 16:33:38 +02:00
this.ticks = (this.minIndex === 0 && this.maxIndex === this.chart.data.labels.length) ? this.chart.data.labels : this.chart.data.labels.slice(this.minIndex, this.maxIndex + 1);
},
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-04-03 05:05:48 +02:00
// 1 is added because we need the length but we have the indexes
var offsetAmt = Math.max((this.ticks.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
if (this.isHorizontal()) {
var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
2016-04-03 05:05:48 +02:00
var valueWidth = innerWidth / offsetAmt;
2016-04-17 16:33:38 +02:00
var widthOffset = (valueWidth * (index - this.minIndex)) + this.paddingLeft;
if (this.options.gridLines.offsetGridLines && includeOffset) {
widthOffset += (valueWidth / 2);
}
return this.left + Math.round(widthOffset);
} else {
var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
2016-04-03 05:05:48 +02:00
var valueHeight = innerHeight / offsetAmt;
2016-04-17 16:33:38 +02:00
var heightOffset = (valueHeight * (index - this.minIndex)) + this.paddingTop;
if (this.options.gridLines.offsetGridLines && includeOffset) {
heightOffset += (valueHeight / 2);
}
return this.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);
},
getValueForPixel: function(pixel)
{
var value
; var offsetAmt = Math.max((this.ticks.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
var horz = this.isHorizontal();
var innerDimension = horz ? this.width - (this.paddingLeft + this.paddingRight) : this.height - (this.paddingTop + this.paddingBottom);
var valueDimension = innerDimension / offsetAmt;
if (this.options.gridLines.offsetGridLines) {
pixel -= (valueDimension / 2);
}
pixel -= horz ? this.paddingLeft : this.paddingTop;
if (pixel <= 0) {
value = 0;
} else {
value = Math.round(pixel / valueDimension);
}
return value;
}
});
Chart.scaleService.registerScaleType("category", DatasetScale, defaultConfig);
};