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

51 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-06-12 22:00:48 +02:00
(function() {
"use strict";
var root = this,
Chart = root.Chart,
helpers = Chart.helpers;
// Default config for a category scale
var defaultConfig = {
position: "bottom",
};
2015-09-23 03:31:26 +02:00
var DatasetScale = Chart.Scale.extend({
buildTicks: function(index) {
this.ticks = this.data.labels;
},
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) {
if (this.isHorizontal()) {
var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
var valueWidth = innerWidth / Math.max((this.data.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
2015-10-21 02:33:51 +02:00
var widthOffset = (valueWidth * index) + this.paddingLeft;
if (this.options.gridLines.offsetGridLines && includeOffset) {
2015-10-21 02:33:51 +02:00
widthOffset += (valueWidth / 2);
}
2015-10-21 02:33:51 +02:00
return this.left + Math.round(widthOffset);
} else {
2015-09-26 22:52:14 +02:00
var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
var valueHeight = innerHeight / Math.max((this.data.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
2015-10-21 02:33:51 +02:00
var heightOffset = (valueHeight * index) + this.paddingTop;
2015-09-26 22:52:14 +02:00
if (this.options.gridLines.offsetGridLines && includeOffset) {
2015-10-21 02:33:51 +02:00
heightOffset += (valueHeight / 2);
2015-09-26 22:52:14 +02:00
}
2015-10-21 02:33:51 +02:00
return this.top + Math.round(heightOffset);
}
},
2015-09-23 03:31:26 +02:00
});
2015-09-21 01:18:59 +02:00
Chart.scaleService.registerScaleType("category", DatasetScale, defaultConfig);
2015-06-12 22:00:48 +02:00
}).call(this);