score:32
You'll have to add code in 2 places. As an example, take the doughnut. First add label info to the defaults (look at the original Chart.js code and compare with this):
chart.Doughnut.defaults = {
segmentShowStroke : true,
segmentStrokeColor : "#fff",
segmentStrokeWidth : 2,
percentageInnerCutout : 50,
animation : true,
animationSteps : 100,
animationEasing : "easeOutBounce",
animateRotate : true,
animateScale : false,
onAnimationComplete : null,
labelFontFamily : "Arial",
labelFontStyle : "normal",
labelFontSize : 24,
labelFontColor : "#666"
};
Then go down to where the Doughnut is drawn and add the four ctx
lines.
animationLoop(config,null,drawPieSegments,ctx);
function drawPieSegments (animationDecimal){
ctx.font = config.labelFontStyle + " " + config.labelFontSize+"px " + config.labelFontFamily;
ctx.fillStyle = 'black';
ctx.textBaseline = 'middle';
ctx.fillText(data[0].value + "%", width/2 - 20, width/2, 200);
The ctx.fillText
call will put the text onto the canvas, so you can use that to write text with x,y coordinates. You ought to be able to use this way to do basic labels. Here is the jsfiddle to tinker with:
http://jsfiddle.net/nCFGL/ (look at lines 281 and 772 in the JavaScript section of the jsfiddle for aforementioned code)
If you need something fancier, someone forked a version of Charts.js and added tooltips. Here is the discussion https://github.com/nnnick/Chart.js/pull/35, and you'll be able to find the link to the forked version inside that discussion.
score:3
I have figured out a way so that we can display the values for each region out side the graph.
Also I removed the rotation of the values and I referred to here
Add the following lines of code inside the Doughnut function. ( I have pasted the modified lines from the Chart.js file).
var Doughnut = function(data,config,ctx){
var segmentTotal = 0;
//In case we have a canvas that is not a square. Minus 10 pixels as padding round the edge.
var doughnutRadius = Min([height/2,width/2]) - 15;
var cutoutRadius = doughnutRadius * (config.percentageInnerCutout/100);
//Modified for setting the label values out side the arc
var outRadius= doughnutRadius + cutoutRadius/3;
var outRadiustop= doughnutRadius + cutoutRadius/5;
......
......
......
function drawPieSegments (animationDecimal){
:
:
if (config.scaleShowValues) {
ctx.save();
ctx.translate(width / 2, height / 2);
ctx.font = config.scaleFontStyle + ' ' + config.scaleFontSize + 'px ' + config.scaleFontFamily;
ctx.textBaselne = 'middle';
var a = (cumulativeAngle + cumulativeAngle + segmentAngle) / 2,
w = ctx.measureText(data[i].value).width,
b = Math.PI / 2 < a && a < Math.PI * 3 / 2;
var c = 0 < a && a < Math.PI;
if(b){
ctx.textAlign = 'right';
}
else{
ctx.textAlign = 'left';
}
if(c){
ctx.translate(Math.cos(a) * outRadius +1 , Math.sin(a) * outRadius+1);
}
else{
ctx.translate(Math.cos(a) * outRadiustop, Math.sin(a) * outRadiustop);
}
ctx.fillStyle = config.scaleFontColor;
//If the segment angle less than 0.2, then the lables will overlap, so hiding it.
if(segmentAngle > 0.2){
ctx.fillText(data[i].value,0,0);
}
ctx.restore();
}
......
......
Now the values will be displayed out side each sections and it will not be rotated.
score:3
There is an forked version, ChartNew, that provides this functionality out of the box.
If you need to use ChartJS then you can use this revised version of @Jack's solution:
Chart.types.Doughnut.extend({
name: "DoughnutAlt",
draw: function() {
Chart.types.Doughnut.prototype.draw.apply(this, arguments);
this.chart.ctx.fillStyle = 'black';
this.chart.ctx.textBaseline = 'middle';
this.chart.ctx.fillText(this.segments[0].value + "%", this.chart.width / 2 - 20, this.chart.width / 2, 200);
}
});
score:7
This one literally took hours and hours and I found a working solution.
https://github.com/nnnick/Chart.js/pull/116
This was my final code. I was trying to display percentages as labels on doughnut
Chart.types.Doughnut.extend({
name: "DoughnutAlt",
draw: function() {
Chart.types.Doughnut.prototype.draw.apply(this, arguments);
this.chart.ctx.fillStyle = 'black';
this.chart.ctx.textBaseline = 'middle';
this.chart.ctx.textAlign = 'start';
this.chart.ctx.font="18px Verdana";
var total = 0;
for (var i = 0; i < this.segments.length; i++) {
total += this.segments[i].value;
}
this.chart.ctx.fillText(total , this.chart.width / 2 - 20, this.chart.height / 2, 100);
for(var i = 0; i < this.segments.length; i++){
var percentage = ((this.segments[i].value / total) * 100).toFixed(1);
if( percentage > 3 ){
var centreAngle = this.segments[i].startAngle + ((this.segments[i].endAngle - this.segments[i].startAngle) / 2),
rangeFromCentre = (this.segments[i].outerRadius - this.segments[i].innerRadius) / 2 + this.segments[i].innerRadius;
var x = this.segments[i].x + (Math.cos(centreAngle) * rangeFromCentre);
var y = this.segments[i].y + (Math.sin(centreAngle) * rangeFromCentre);
this.chart.ctx.textAlign = 'center';
this.chart.ctx.textBaseline = 'middle';
this.chart.ctx.fillStyle = '#fff';
this.chart.ctx.font = 'normal 10px Helvetica';
this.chart.ctx.fillText(percentage , x, y);
}
}
}
});
Source: stackoverflow.com
Related Query
- How to add labels into Chart.js canvas plugin?
- How to add images as labels to Canvas Charts using chart.js
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- How to add images to chart labels with vue-chartjs?
- How to add null value rows into pandas dataframe for missing years in a multi-line chart plot
- How to add labels on top of the chart bar with Chart.js 2
- How to add data labels in each bar in stacked bar chart in chart.js?
- How to add text inside the doughnut chart using Chart.js?
- How to clear a chart from a canvas so that hover events cannot be triggered?
- How to add an on click event to my Line chart using 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 add an offset to a dataset in Chart js
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
- How to add OnClick Event on labels in Chart.js v2.0?
- How to add datas to chart js from javascript array itself?
- How to add panning to chart in chartjs?
- How to display data labels outside in pie chart with lines in ionic
- How to align Chart.JS line chart labels to the center
- How to display the labels in doughnut chart using ng2 charts?
- How to add a second set of labels to a Chart.js doughnut chart?
- how to add a title to my ng2-charts bar chart
- Chart.js Radar Chart How to Remove Outer Labels
- How put dataset labels into multiTooltipTemplate?
- How to add comma in this chart js
- How to add gradient background to Line Chart [vue-chart.js]
- How can I add some text in the middle of a half doughnut chart in Chart.JS?
- How to add ChartJS code in Html2Pdf to view image
- How to draw a needle on a custom donut chart and add datapoints to the needle?
More Query from same tag
- ChartJS zoom/pan onPan event not firing in React useEffect hook
- Mixed bubble chart with line chart in vue-chartjs4
- Chartjs v3 overlap stacked bar chart with groups
- How to print a chart rendered by code
- Change font family of labels in piechart and linechart in chartjs
- Why does x axis not increment 'monthly' chart.js. Also, XAxis not taking title
- chart.js stacked graph that overlaps
- Setting ChartJS plugin on vue-chartjs not working
- React chartjs-2 - Increase spacing between legend and chart
- React using fetch returns undefined until save
- Chart.js: Can't get a coordinate for value for x axis unless x axis has the exact same value
- How do I increase the arrow length on the pie chart in Charts.js?
- Chart.js different doughnut thickness in a mixed chart
- How to draw baseline for bar chart in chart.js
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- Second tab doesn't show the chart . Can anybody see why?
- Chart.js multiTooltip labels
- Order the Time Units on Y-axis + Chart Js
- How to add vertical line in bar chart using chartJs and ng2-charts?
- Set minimum and maximum axis values on chart
- Click event of stacked line chart not working
- How to shrink the height and increase the line width of a custom Chartjs horizontal line?
- Chartjs, plot data based with unequal time intervals
- Align lines and bars in a mixed chart
- Chart.js displaying time data
- Display Charts based on year select in django
- Is it possible to scale Chart.js background Image
- Chart.js legend took up too much spaces
- Chart.js align line graph to the left
- ChartJS Legend Not Lining Up