score:5
Just extend the chart to do this. If your labels are static, it might be simpler to just change the tooltipPosition
method instead.
Preview
Script
Chart.types.Pie.extend({
name: "PieAlt",
initialize: function(data){
Chart.types.Pie.prototype.initialize.apply(this, arguments);
var requiredSpace = 0;
for (var i = 0; i < data.length; i++)
requiredSpace = Math.max(ctx.measureText(Chart.helpers.template(this.options.tooltipTemplate, data[i])).width, requiredSpace);
this.outerRadius -= (requiredSpace + 20);
},
draw: function(data){
Chart.types.Pie.prototype.draw.apply(this, arguments);
var self = this;
ctx.save();
ctx.font = Chart.helpers.fontString(self.options.scaleFontSize, self.options.scaleFontStyle, self.options.scaleFontFamily);
ctx.textBaseline = "middle";
self.segments.forEach(function (segment) {
var outerEdge = Chart.Arc.prototype.tooltipPosition.apply({
x: this.chart.width / 2,
y: this.chart.height / 2,
startAngle: segment.startAngle,
endAngle: segment.endAngle,
outerRadius: segment.outerRadius * 2 + 20,
innerRadius: 0
})
var normalizedAngle = (segment.startAngle + segment.endAngle) / 2;
while (normalizedAngle > 2 * Math.PI) {
normalizedAngle -= (2 * Math.PI)
}
if (normalizedAngle < (Math.PI * 0.4) || (normalizedAngle > Math.PI * 1.5))
ctx.textAlign = "start";
else if (normalizedAngle > (Math.PI * 0.4) && (normalizedAngle < Math.PI * 0.6)) {
outerEdge.y += 5;
ctx.textAlign = "center";
}
else if (normalizedAngle > (Math.PI * 1.4) && (normalizedAngle < Math.PI * 1.6)) {
outerEdge.y - 5;
ctx.textAlign = "center";
}
else
ctx.textAlign = "end";
ctx.fillText(Chart.helpers.template(self.options.tooltipTemplate, segment), outerEdge.x, outerEdge.y);
});
ctx.restore();
}
});
and then
new Chart(ctx).PieAlt(data, {
showTooltips: false
});
Fiddle - http://jsfiddle.net/h8rggkhp/
score:0
If you want pointed lines with outlabels like this
then use chartjs-plugin-piechart-outlabels
npm link
score:1
It looks like you control the x and y position of the tooltips:
var myPieChart = new Chart(ctx).Pie(data, {
customTooltips: function(tooltip) {
// tooltip will be false if tooltip is not visible or should be hidden
if (!tooltip) {
return;
}
// Otherwise, tooltip will be an object with all tooltip properties like:
// tooltip.caretHeight
// tooltip.caretPadding
// tooltip.chart
// tooltip.cornerRadius
// tooltip.fillColor
// tooltip.font...
// tooltip.text
// tooltip.x
// tooltip.y
// etc...
};
});
take a look at http://www.chartjs.org/docs/#doughnut-pie-chart-chart-options
score:2
I recently ran into the same issue, and chartsjs-plugin-labels solved the issue for me.
Example:
import {Chart} from 'chartjs';
import 'chartjs-plugin-labels';
let options = {
plugins: {
labels: {
position: 'outside',
render: (args) => {
return `${args.label}: ${args.value}%`;
}
}
}
}
let data = {
datasets: [
{
data: [39, 4, 57],
labels: ['Blue', 'Gray', 'Green'],
},
],
}
new Chart(ctx, {
type: 'pie',
data: data,
options: options
}
Source: stackoverflow.com
Related Query
- How to display data labels outside in pie chart with lines in ionic
- How to move labels' position on Chart.js pie
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- How to get labels on ChartJS Pie chart segments
- How to display labels outside the pie chart border?
- Char.js - How to show labels by default in pie chart
- How do we put labels on pie chart arcs - chart.js/vue-chart.js
- How to show labels above pie chart in chart.js
- Chart.js Show labels on Pie chart
- chart.js: Show labels outside pie chart
- How set color family to pie chart in chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
- How to display Line Chart dataset point labels with Chart.js?
- How to align Chart.JS line chart labels to the center
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- How to hide section in a Chart.js Pie Chart
- How to display the labels in doughnut chart using ng2 charts?
- How to create single value Doughnut or Pie chart using Chart.js?
- Chart.js Radar Chart How to Remove Outer Labels
- ChartJS 2.0 - Huddle labels on pie chart
- How do you set pie chart colors in angular-chart.js
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to add images to chart labels with vue-chartjs?
- How can I rotate a pie chart in charts.js?
- How to show percentage (%) using chartjs-plugin-labels ( Pie chart ) in angular 2/8
- ChartJS version 3 how to add percentage to pie chart tooltip
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- How to disable Chart.js Radar Chart point labels
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- Pie chart isn't loading, but the labels are
More Query from same tag
- Chart.js - doughnut show active segment tooltip (on click of external button)
- using data structures in chartjs
- How to edit my custom tooltips in my line chart using chart.js?
- Chartjs draw line chart where line go back and forth (by chronological order)
- Chart.js v3 after using of zoom plugin instance persist in memory
- How can I add new data points to an existing chart with chart.js?
- Angular chart js how to use formatter with Doughnut chart
- Chart.js not drawing on mobile (safari/chrome) fine on desktop
- Chart js data to start at zero
- Unable to update state in ReactJS
- Chart.js two categorical scales
- how to set a custom tick format in chartjs options from laravel controller?
- Chartjs Radar - Change color of end point labels
- chart.js: Chart in chart
- chartjs mixed type display want to set display none for some bar
- Chartjs - Doughnut chart with multi layer and running value
- Modifying the X-Axis Labels of a Scatterplot in Chart.js 2
- Vue-chartjs: Re-render a chart even if data hasn't changed
- Chartjs stacked bar separate tooltip for all stacked
- Put chart.js chart inside JQuery tooltip?
- How do I invert an y-axis using chart.js (v2.0-dev)
- Show data dynamically in line chart - ChartJS
- TopoJson and ChartGeo/ChartJS: Unexpected geomerty
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- plot line graph in Chartjs 2 when number of data in yaxis is not eqaul to number of data in xaxis
- No chart appears - Chart.js
- Multiple Chart JS percentage labels
- How to make a line dashed or dotted in chartJS?
- Re-creating this graph in ChartJS
- How can I implement Polar area charts with Chart.js and Canvas