score:13
I'm aware that this is an old post but since it has been viewed so many times, I'll describe a solution that works with the current Chart.js version 2.9.3.
The Plugin Core API offers a range of hooks that may be used for performing custom code. You can use the afterDraw
hook to draw images (icons) directly on the canvas using CanvasRenderingContext2D
.
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
var xAxis = chart.scales['x-axis-0'];
var yAxis = chart.scales['y-axis-0'];
xAxis.ticks.forEach((value, index) => {
var x = xAxis.getPixelForTick(index);
var image = new Image();
image.src = images[index],
ctx.drawImage(image, x - 12, yAxis.bottom + 10);
});
}
}],
The position of the labels will have to be defined through the xAxes.ticks.padding
as follows:
xAxes: [{
ticks: {
padding: 30
}
}],
Please have a look at the following runnable code snippet.
const labels = ['Red Vans', 'Blue Vans', 'Green Vans', 'Gray Vans'];
const images = ['https://i.stack.imgur.com/2RAv2.png', 'https://i.stack.imgur.com/Tq5DA.png', 'https://i.stack.imgur.com/3KRtW.png', 'https://i.stack.imgur.com/iLyVi.png'];
const values = [48, 56, 33, 44];
new Chart(document.getElementById("myChart"), {
type: "bar",
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
var xAxis = chart.scales['x-axis-0'];
var yAxis = chart.scales['y-axis-0'];
xAxis.ticks.forEach((value, index) => {
var x = xAxis.getPixelForTick(index);
var image = new Image();
image.src = images[index],
ctx.drawImage(image, x - 12, yAxis.bottom + 10);
});
}
}],
data: {
labels: labels,
datasets: [{
label: 'My Dataset',
data: values,
backgroundColor: ['red', 'blue', 'green', 'lightgray']
}]
},
options: {
responsive: true,
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
ticks: {
padding: 30
}
}],
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="90"></canvas>
score:0
Chart.js v3+ solution to pie, doughnut and polar charts
With version 3 of Chart.js and the updated version of chart.js-plugin-labels, this is now incredbly simple.
in options.plugins.labels
, add render: image
and the nested array images
with objects containing the properties src
, width
and height
.
const data = {
labels: ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5', 'Label 6', 'Label 7', 'Label 8'],
datasets: [{
label: 'Image labels',
// Making each element take up full width, equally divided
data: [100, 100, 100, 100, 100, 100, 100, 100],
backgroundColor: [
'rgba(255, 26, 104, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(0, 0, 0, 0.2)',
'rgba(20, 43, 152, 0.2)'
]
}]
};
const config = {
type: 'doughnut',
data,
options: {
plugins: {
// Accessing labels and making them images
labels: {
render: 'image',
images: [{
src: 'https://cdn0.iconfinder.com/data/icons/google-material-design-3-0/48/ic_book_48px-256.png',
height: 25,
width: 25
},
{
src: 'https://cdn3.iconfinder.com/data/icons/glypho-free/64/pen-checkbox-256.png',
height: 25,
width: 25
},
{
src: 'https://cdn1.iconfinder.com/data/icons/jumpicon-basic-ui-glyph-1/32/-_Home-House--256.png',
height: 25,
width: 25
},
{
src: 'https://cdn1.iconfinder.com/data/icons/social-media-vol-3/24/_google_chrome-256.png',
height: 25,
width: 25
},
{
src: 'https://cdn0.iconfinder.com/data/icons/google-material-design-3-0/48/ic_book_48px-256.png',
height: 25,
width: 25
},
{
src: 'https://cdn3.iconfinder.com/data/icons/glypho-free/64/pen-checkbox-256.png',
height: 25,
width: 25
},
{
src: 'https://cdn1.iconfinder.com/data/icons/jumpicon-basic-ui-glyph-1/32/-_Home-House--256.png',
height: 25,
width: 25
},
{
src: 'https://cdn1.iconfinder.com/data/icons/social-media-vol-3/24/_google_chrome-256.png',
height: 25,
width: 25
},
]
}
}
}
};
// render init block
const myChart = new Chart(
document.getElementById('myChart').getContext('2d'),
config
);
.chartCard {
width: 100vw;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
}
.chartBox {
width: 600px;
padding: 20px;
}
<div class="chartCard">
<div class="chartBox">
<canvas id="myChart"></canvas>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://unpkg.com/chart.js-plugin-labels-dv@3.0.5/dist/chartjs-plugin-labels.min.js"></script>
Source: stackoverflow.com
Related Query
- How to add images as labels to Canvas Charts using chart.js
- How to add images to chart labels with vue-chartjs?
- How to add text inside the doughnut chart using Chart.js?
- How to add labels into Chart.js canvas plugin?
- 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?
- How to add text in centre of the doughnut chart using Chart.js?
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- How to display the labels in doughnut chart using ng2 charts?
- Using chart js version 3, how to add custom data to external tooltip?
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- how to add percentage value to legend field in pie chart using chart.js
- How to add text inside the doughnut chart using Chart.js version 3.2.1
- how to add a background that display "no data" on Canvas using ChartJS?
- How to destroy a canvas using Id and add new canvas to the same id
- How to get the data attribute of the canvas chart created using chartjs
- How to add labels on top of the chart bar with Chart.js 2
- How to add text inside the doughnut chart using Chart.js AngularJS 2.0?
- How to add image inside the doughnut chart using chart.js?
- How to plot a line chart which has both the start and points outside the canvas area using Chart.js
- How to show bar chart labels clearly using ChartJS?
- How to add prefix to legend in ng charts using angular
- How to run Chart.js samples using source code
- How to make the size of pie chart fixed without using canvas (chart.js)
- How to add data labels in each bar in stacked bar chart in chart.js?
- How to add label square to Bar Chart using Chart.js
- How to add vertical line in bar chart using chartJs and ng2-charts?
- how to not repeat code while creating multiple charts in chart js
- How to clear a chart from a canvas so that hover events cannot be triggered?
- Converting Chart.js canvas chart to image using .toDataUrl() results in blank image
More Query from same tag
- Having problems getting a line chart in Chartsjs to go *up* to 1, with bkg fill *under* the chart line
- Unable to access data in collection for chart.js
- Vue2 trying to set chart title
- How to remove x axis scale labels Chart.Js
- Chartjs with Vue, Bar Chart Border Radius not working
- Chart.js v3: Tooltip callback doesn't identify clicked dataset of stacked bar chart
- How to change draw order by line property with chart.js
- react chart js skip zero value month
- Error in created hook (Promise/async): "TypeError: Cannot read property 'position' of undefined"
- How to control the over riding of Y-axis values in Chart.js when more entries inserted using PHP?
- How to create dynamically chart for Ionic App with Firebase data?
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- How to hide zero data value from chartjs?
- Chart JS grouped sub labels
- Remove custom Chart Js tool tip colour square
- ChartJS 3 vertical annotations
- Chartjs for Line chart, values on y-axis
- Chart.js Adapting different X axis with same scale
- How do I change the grid line style on the Y axis in Chart.js?
- Display dataset values on bar ChartJs 2.1.6
- Chart js data to start at zero
- Show the latest labels in a bar chart with React.js using react-chartjs
- ChartJS works with Angular 5 while developing, but ng build fails
- Space between start and end of axis
- Chart.js tooltips callback function with pie charts
- Chart.js Pie Chart
- Chart js footer
- How to map json array to two different array for chart
- Updating Chart.js Datasets - Array not re-initializing (Jquery / PHP)
- Add rectangle fill to line chart