score:5
Accepted answer
Chart.js doesn't support this directly, but Evert Timberg was very helpful in providing an example Chart.js plugin does exactly this.
From Chart.js Data Labeling Example
// Define a plugin to provide data labels
Chart.plugins.register({
afterDatasetsDraw: function(chartInstance, easing) {
// To only draw at the end of animation, check for easing === 1
var ctx = chartInstance.chart.ctx;
chartInstance.data.datasets.forEach(function (dataset, i) {
var meta = chartInstance.getDatasetMeta(i);
if (!meta.hidden) {
meta.data.forEach(function(element, index) {
// Draw the text in black, with the specified font
ctx.fillStyle = 'rgb(0, 0, 0)';
var fontSize = 16;
var fontStyle = 'normal';
var fontFamily = 'Helvetica Neue';
ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
// Just naively convert to string for now
// <---- ADJUST TO DESIRED TEXT --->
var dataString = dataset.data[index].toString();
// Make sure alignment settings are correct
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var padding = 5;
var position = element.tooltipPosition();
ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
});
}
});
}
});
For example, if i just passed in "#22" as the text to render, we get this.
Source: stackoverflow.com
Related Query
- Chart.js - Add text/label to bubble chart elements without using tooltips?
- How to add text inside the doughnut chart using Chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
- Adding custom text to Bar Chart label values using Chart.js
- How to add text inside the doughnut chart using Chart.js version 3.2.1
- How to change default label of each bubble in bubble chart using chartjs-plugin-datalabels
- How to add text inside the doughnut chart using Chart.js AngularJS 2.0?
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Chart.js add label inside bubble chart
- How to add label square to Bar Chart using Chart.js
- Chart.js - How to Add Text in the label of the Chart with JavaScript?
- Chart Js change text label orientation on Ox axis
- How to add an on click event to my Line chart using Chart.js
- How to save Chart JS charts as image without black background using blobs and filesaver?
- Chartjs - show elements in all datasets on hover using bar chart
- Remove the label and show only value in tooltips of a bar chart
- X-axis multiple colored label for bar chart using chart.js
- Add chart using chart.js in codeigniter
- How do you add custom text to tooltips in charts.js
- Add a custom label to the top or bottom of a stacked bar chart
- How can I add some text in the middle of a half doughnut chart in Chart.JS?
- Add a text as tooltip for each point on a line chart
- Is it possible to change pointStyle for elements of a ChartJS bubble chart per dataset?
- How to add label in chart.js for polar chart area
- Add Text to Doughnut Chart - ChartJS
- Using chart js version 3, how to add custom data to external tooltip?
- Add data to line chart using chart.js
- Add a label to a point of a bubble graph (Chart.js)
- Add HTML to label of bar chart - chart js
- how to add percentage value to legend field in pie chart using chart.js
More Query from same tag
- Angular Charts - not able to read data
- Load Tooltip faster during onHover of Legend Doughnut chart
- ERROR TypeError: Cannot read properties of undefined (reading 'skip') Typescript/Api
- Draw a horizontal and vertical line on mouse hover in chart js
- Can't change color line with chart.js
- Chart.js - is there a way to extend an existing scale?
- Chart.js Update start date with button selector
- ChartJS: Custom tooltip always displaying
- bind first property value of an array of object into chart.js
- 'Chart.js' time chart not displaying properly
- Chart.js hover over slice get NaN percentage
- Chart.js: Get point index from chart.getPointsAtEvent(e)
- Chart.js time object labels not updating correctly
- ChartJS 2.0 differences in code help needed
- Legend not displaying on Line or Bar chart - React-Chartjs-2
- Animate datasets separately
- Chartjs: Align first tick in first index of axis
- ChartJS radar-chart hover text
- ChartJS - Y Axis line not drawing
- ChartJS on small screen
- ReactJS - Loading data using Axios + chartjs
- Dynamically update values of a chartjs2.0 chart
- Chartjs + jsPDF = Blurry image
- Switch chart.js data with button click
- How to set max ticks count in chart.js 3.*.*
- chart.js version 2.4.0 does not display bars for me
- How to display chart using Chartjs with JSON data in Laravel
- Access yLabel for specific bar in chartJs without click
- How could chartjs font family display everywhere
- (Angular Ionic) How can I stream my custom data with chartjs-plugin-streaming?