//String - Point label font colour
fontColor: "#666",
+
+ //Function - Used to convert point labels
+ callback: function(label) {
+ return label;
+ }
},
};
this.zeroLineIndex = this.ticks.indexOf(0);
},
+ convertTicksToLabels: function() {
+ Chart.Scale.prototype.convertTicksToLabels.call(this);
+
+ // Point labels
+ this.pointLabels = this.chart.data.labels.map(this.options.pointLabels.callback, this);
+ },
getLabelForIndex: function(index, datasetIndex) {
return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]);
},
for (i = 0; i < this.getValueCount(); i++) {
// 5px to space the text slightly out - similar to what we do in the draw function.
pointPosition = this.getPointPosition(i, largestPossibleRadius);
- textWidth = this.ctx.measureText(this.options.ticks.callback(this.chart.data.labels[i])).width + 5;
+ textWidth = this.ctx.measureText(this.options.ticks.callback(this.pointLabels[i])).width + 5;
if (i === 0 || i === this.getValueCount() / 2) {
// If we're at index zero, or exactly the middle, we're at exactly the top/bottom
// of the radar chart, so text will be aligned centrally, so we'll half it and compare
ctx.font = helpers.fontString(this.options.pointLabels.fontSize, this.options.pointLabels.fontStyle, this.options.pointLabels.fontFamily);
ctx.fillStyle = this.options.pointLabels.fontColor;
- var labelsCount = this.chart.data.labels.length,
- halfLabelsCount = this.chart.data.labels.length / 2,
+ var labelsCount = this.pointLabels.length,
+ halfLabelsCount = this.pointLabels.length / 2,
quarterLabelsCount = halfLabelsCount / 2,
upperHalf = (i < quarterLabelsCount || i > labelsCount - quarterLabelsCount),
exactQuarter = (i === quarterLabelsCount || i === labelsCount - quarterLabelsCount);
ctx.textBaseline = 'top';
}
- ctx.fillText(this.chart.data.labels[i], pointLabelPosition.x, pointLabelPosition.y);
+ ctx.fillText(this.pointLabels[i], pointLabelPosition.x, pointLabelPosition.y);
}
}
}
fontFamily: "'Arial'",
fontSize: 10,
fontStyle: "normal",
+ callback: defaultConfig.pointLabels.callback, // make this nicer, then check explicitly below
},
position: "chartArea",
scaleLabel: {
// Is this actually a function
expect(defaultConfig.ticks.callback).toEqual(jasmine.any(Function));
+ expect(defaultConfig.pointLabels.callback).toEqual(jasmine.any(Function));
});
it('Should correctly determine the max & min data values', function() {
yAxisID: scaleID,
data: [10, 5, 0, 25, 78]
}],
- labels: []
+ labels: ['label1', 'label2', 'label3', 'label4', 'label5']
};
var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('radialLinear'));
// Just the index
expect(scale.ticks).toEqual(['0', '1', '2', '3', '4']);
+ expect(scale.pointLabels).toEqual(['label1', 'label2', 'label3', 'label4', 'label5']);
+ });
+
+ it('Should build point labels using the user supplied callback', function() {
+ var scaleID = 'myScale';
+
+ var mockData = {
+ datasets: [{
+ yAxisID: scaleID,
+ data: [10, 5, 0, 25, 78]
+ }],
+ labels: ['label1', 'label2', 'label3', 'label4', 'label5']
+ };
+
+ var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('radialLinear'));
+ config.pointLabels.callback = function(value, index) {
+ return index.toString();
+ };
+
+ var mockContext = window.createMockContext();
+ var Constructor = Chart.scaleService.getScaleConstructor('radialLinear');
+ var scale = new Constructor({
+ ctx: mockContext,
+ options: config,
+ chart: {
+ data: mockData
+ },
+ id: scaleID,
+ });
+
+ scale.update(200, 300);
+
+ // Just the index
+ expect(scale.pointLabels).toEqual(['0', '1', '2', '3', '4']);
});
it('should correctly set the center point', function() {