mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-08 05:09:08 +02:00
49 lines
1.6 KiB
JavaScript
49 lines
1.6 KiB
JavaScript
"use strict";
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
var helpers = require('../core/core.helpers.js');
|
|
// Default config for a category scale
|
|
var defaultConfig = {
|
|
position: "bottom",
|
|
};
|
|
|
|
var DatasetScale = Chart.Scale.extend({
|
|
buildTicks: function(index) {
|
|
this.ticks = this.chart.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.chart.data.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
|
|
var widthOffset = (valueWidth * index) + 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);
|
|
var valueHeight = innerHeight / Math.max((this.chart.data.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
|
|
var heightOffset = (valueHeight * index) + this.paddingTop;
|
|
|
|
if (this.options.gridLines.offsetGridLines && includeOffset) {
|
|
heightOffset += (valueHeight / 2);
|
|
}
|
|
|
|
return this.top + Math.round(heightOffset);
|
|
}
|
|
},
|
|
});
|
|
|
|
Chart.scaleService.registerScaleType("category", DatasetScale, defaultConfig);
|
|
|
|
};
|