mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-19 14:46:51 +01:00
Implement getValueForPixel for category scale
This commit is contained in:
@@ -69,6 +69,27 @@ module.exports = function(Chart) {
|
||||
},
|
||||
getPixelForTick: function(index, includeOffset) {
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -146,17 +146,23 @@ describe('Category scale tests', function() {
|
||||
|
||||
expect(scale.getPixelForValue(0, 0, 0, false)).toBe(33);
|
||||
expect(scale.getPixelForValue(0, 0, 0, true)).toBe(85);
|
||||
expect(scale.getValueForPixel(33)).toBe(0);
|
||||
expect(scale.getValueForPixel(85)).toBe(0);
|
||||
|
||||
expect(scale.getPixelForValue(0, 4, 0, false)).toBe(452);
|
||||
expect(scale.getPixelForValue(0, 4, 0, true)).toBe(505);
|
||||
expect(scale.getValueForPixel(452)).toBe(4);
|
||||
expect(scale.getValueForPixel(505)).toBe(4);
|
||||
|
||||
config.gridLines.offsetGridLines = false;
|
||||
|
||||
expect(scale.getPixelForValue(0, 0, 0, false)).toBe(33);
|
||||
expect(scale.getPixelForValue(0, 0, 0, true)).toBe(33);
|
||||
expect(scale.getValueForPixel(33)).toBe(0);
|
||||
|
||||
expect(scale.getPixelForValue(0, 4, 0, false)).toBe(557);
|
||||
expect(scale.getPixelForValue(0, 4, 0, true)).toBe(557);
|
||||
expect(scale.getValueForPixel(557)).toBe(4);
|
||||
});
|
||||
|
||||
it ('Should get the correct pixel for a value when horizontal and zoomed', function() {
|
||||
@@ -268,17 +274,22 @@ describe('Category scale tests', function() {
|
||||
|
||||
expect(scale.getPixelForValue(0, 0, 0, false)).toBe(11);
|
||||
expect(scale.getPixelForValue(0, 0, 0, true)).toBe(30);
|
||||
expect(scale.getValueForPixel(11)).toBe(0);
|
||||
expect(scale.getValueForPixel(30)).toBe(0);
|
||||
|
||||
expect(scale.getPixelForValue(0, 4, 0, false)).toBe(161);
|
||||
expect(scale.getPixelForValue(0, 4, 0, true)).toBe(180);
|
||||
expect(scale.getValueForPixel(161)).toBe(4);
|
||||
|
||||
config.gridLines.offsetGridLines = false;
|
||||
|
||||
expect(scale.getPixelForValue(0, 0, 0, false)).toBe(11);
|
||||
expect(scale.getPixelForValue(0, 0, 0, true)).toBe(11);
|
||||
expect(scale.getValueForPixel(11)).toBe(0);
|
||||
|
||||
expect(scale.getPixelForValue(0, 4, 0, false)).toBe(199);
|
||||
expect(scale.getPixelForValue(0, 4, 0, true)).toBe(199);
|
||||
expect(scale.getValueForPixel(199)).toBe(4);
|
||||
});
|
||||
|
||||
it ('should get the correct pixel for a value when vertical and zoomed', function() {
|
||||
|
||||
Reference in New Issue
Block a user