Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return correct label for value type axis #5920

Merged
merged 9 commits into from Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/scales/scale.category.js
@@ -1,5 +1,6 @@
'use strict';

var helpers = require('../helpers/index');
var Scale = require('../core/core.scale');

var defaultConfig = {
Expand Down Expand Up @@ -49,10 +50,19 @@ module.exports = Scale.extend({

getLabelForIndex: function(index, datasetIndex) {
var me = this;
var data = me.chart.data;
var chart = me.chart;
var data = chart.data;
var isHorizontal = me.isHorizontal();
var controller = chart.getDatasetMeta(datasetIndex).controller;

if (data.yLabels && !isHorizontal) {
// For controllers supporting getValueScaleId, we can be sure if this is a value scale.
// For object data ({x,y} etc.) we can rely on getRightValue to return correct thing.
// For others, check if data is a label and use that instead (so this is an value index, but values are labels)
kurkle marked this conversation as resolved.
Show resolved Hide resolved
var isValueScale = controller.getValueScaleId
kurkle marked this conversation as resolved.
Show resolved Hide resolved
? controller.getValueScaleId() === me.id
: helpers.isObject(data[0]) || (!isHorizontal && me.getLabels().indexOf(data.datasets[datasetIndex].data[index]) !== -1);
kurkle marked this conversation as resolved.
Show resolved Hide resolved

if (isValueScale) {
return me.getRightValue(data.datasets[datasetIndex].data[index]);
}
return me.ticks[index - me.minIndex];
Expand Down
57 changes: 30 additions & 27 deletions test/specs/scale.category.tests.js
Expand Up @@ -156,35 +156,38 @@ describe('Category scale tests', function() {
expect(scale.ticks).toEqual(labels);
});

it ('should get the correct label for the index', function() {
var scaleID = 'myScale';

var mockData = {
datasets: [{
yAxisID: scaleID,
data: [10, 5, 0, 25, 78]
}],
labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
};

var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
var Constructor = Chart.scaleService.getScaleConstructor('category');
var scale = new Constructor({
ctx: {},
options: config,
chart: {
data: mockData
it('should get the correct label for the index', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
xAxisID: 'xScale0',
yAxisID: 'yScale0',
data: [10, 5, 0, 25, 78]
}],
labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
},
id: scaleID
options: {
scales: {
xAxes: [{
id: 'xScale0',
type: 'category',
position: 'bottom'
}],
yAxes: [{
id: 'yScale0',
type: 'linear'
}]
}
}
});

scale.determineDataLimits();
scale.buildTicks();
var scale = chart.scales.xScale0;

expect(scale.getLabelForIndex(1)).toBe('tick2');
expect(scale.getLabelForIndex(1, 0)).toBe('tick2');
});

it ('Should get the correct pixel for a value when horizontal', function() {
it('Should get the correct pixel for a value when horizontal', function() {
kurkle marked this conversation as resolved.
Show resolved Hide resolved
var chart = window.acquireChart({
type: 'line',
data: {
Expand Down Expand Up @@ -227,7 +230,7 @@ describe('Category scale tests', function() {
expect(xScale.getValueForPixel(417)).toBe(4);
});

it ('Should get the correct pixel for a value when there are repeated labels', function() {
it('Should get the correct pixel for a value when there are repeated labels', function() {
var chart = window.acquireChart({
type: 'line',
data: {
Expand Down Expand Up @@ -258,7 +261,7 @@ describe('Category scale tests', function() {
expect(xScale.getPixelForValue('tick_1', 1, 0)).toBeCloseToPixel(143);
});

it ('Should get the correct pixel for a value when horizontal and zoomed', function() {
it('Should get the correct pixel for a value when horizontal and zoomed', function() {
var chart = window.acquireChart({
type: 'line',
data: {
Expand Down Expand Up @@ -299,7 +302,7 @@ describe('Category scale tests', function() {
expect(xScale.getPixelForValue(0, 3, 0)).toBeCloseToPixel(429);
});

it ('should get the correct pixel for a value when vertical', function() {
it('should get the correct pixel for a value when vertical', function() {
var chart = window.acquireChart({
type: 'line',
data: {
Expand Down Expand Up @@ -344,7 +347,7 @@ describe('Category scale tests', function() {
expect(yScale.getValueForPixel(437)).toBe(4);
});

it ('should get the correct pixel for a value when vertical and zoomed', function() {
it('should get the correct pixel for a value when vertical and zoomed', function() {
var chart = window.acquireChart({
type: 'line',
data: {
Expand Down