Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/chartjs-plugin-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
}

var SUPPORTED_TYPES = {};
['pie', 'doughnut', 'polarArea', 'bar'].forEach(function (t) {
['pie', 'doughnut', 'polarArea', 'bar', 'horizontalBar'].forEach(function (t) {
SUPPORTED_TYPES[t] = true;
});

var spacedItems = 1;

function Label() {
this.renderToDataset = this.renderToDataset.bind(this);
}
Expand All @@ -65,7 +67,7 @@
textMargin: 2,
overlap: true
}, options);
if (chart.config.type === 'bar') {
if (chart.config.type === 'bar' || chart.config.type === 'horizontalBar') {
this.options.position = 'default';
this.options.arc = false;
this.options.overlap = true;
Expand Down Expand Up @@ -105,15 +107,17 @@
}
ctx.beginPath();
ctx.fillStyle = this.getFontColor(dataset, element, index);
this.renderLabel(label, renderInfo);

const percentage = this.getPercentage(dataset, element, index);
this.renderLabel(label, renderInfo, percentage, index);
ctx.restore();
};

Label.prototype.renderLabel = function (label, renderInfo) {
return this.options.arc ? this.renderArcLabel(label, renderInfo) : this.renderBaseLabel(label, renderInfo);
Label.prototype.renderLabel = function (label, renderInfo, percentage, index) {
return this.options.arc ? this.renderArcLabel(label, renderInfo) : this.renderBaseLabel(label, renderInfo, percentage, index);
};

Label.prototype.renderBaseLabel = function (label, position) {
Label.prototype.renderBaseLabel = function (label, position, percentage, index) {
var ctx = this.ctx;
if (typeof label === 'object') {
ctx.drawImage(label, position.x - label.width / 2, position.y - label.height / 2, label.width, label.height);
Expand All @@ -132,7 +136,26 @@
var lines = label.split('\n');
for (var i = 0; i < lines.length; i++) {
var y = position.y - this.options.fontSize / 2 * lines.length + this.options.fontSize * i;
ctx.fillText(lines[i], position.x, y);
if(this.chart.config.type === 'horizontalBar') {
var specPos = (position.x + (ctx.measureText(lines[i]).width / 2)) + this.options.textMargin;
if (localStorage.getItem('vm-base') && parseInt(localStorage.getItem('vm-base'), 10) + (ctx.measureText(lines[i]).width / 2) + this.options.textMargin > specPos) {
specPos = parseInt(localStorage.getItem('vm-base'), 10) + (ctx.measureText(lines[i]).width / 2) + this.options.textMargin;
}
ctx.fillText(lines[i], specPos, y);
} else if (this.chart.config.type === 'bar' && this.options.stacked){
if (lines[i] > 0) {
ctx.fillText(lines[i], position.x, (y + this.options.fontSize + 4));
} else {
ctx.fillText(Math.abs(lines[i]), position.x, y);
}
} else {
var newY = y;

if(percentage < 5) {
newY = y - 400;
}
ctx.fillText(lines[i], position.x, newY);
}
}
ctx.restore();
}
Expand Down Expand Up @@ -276,7 +299,7 @@
percentage = parseFloat(percentage.toFixed(this.options.precision));
}
if (this.chart.config.type === 'bar') {
this.barTotalPercentage[index] = this.totalPercentage
this.barTotalPercentage[index] = this.totalPercentage;
}
}
this.percentage = percentage;
Expand Down Expand Up @@ -337,7 +360,7 @@
endAngle: endAngle,
totalAngle: totalAngle,
view: view
}
};
};

Label.prototype.getBarRenderInfo = function (element, label) {
Expand Down