score:8
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) instead of tick labels directly on the canvas using CanvasRenderingContext2D
.
You'll also have to instruct Chart.js not to display the default tick labels. This can be done through the following definition inside the chart options.
scales: {
xAxes: [{
ticks: {
display: false
}
}],
Further you need to define some padding at the bottom of the chart, otherwise you'll see only part of the images.
layout: {
padding: {
bottom: 30
}
},
Please have a look at the runnable code snippet below.
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,
layout: {
padding: {
bottom: 30
}
},
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
ticks: {
display: false
}
}],
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="90"></canvas>
score:13
The easiest way to do this would be to get a font that includes these icons and then set the font family for the ticks
...
options: {
scales: {
xAxes: [{
ticks: {
fontFamily: 'FontAwesome'
}
}]
}
}
};
and then specify the appropriate character codes in your labels
labels: ["\uf24e", "\uf1b9", "\uf242", "\uf0fc", "\uf236"],
With the FontAwesome CSS, the above would give you
Source: stackoverflow.com
Related Query
- How to change the labels to the image (icon) in bar chart.js
- How to show the chartjs bar chart data values labels as text?
- How to add labels on top of the chart bar with Chart.js 2
- Angular-Charts bar chart does not update when I change the data, series, labels
- Is there any way to change the font size of labels in bar chart in Chart.js v3?
- How to change the position of the tooltip for the Bar type chart in Chart.Js
- How can I change the background colors of a bar chart after it has been created?
- How I can change the font-family of labels in the bar chart?
- How to change line chart data label to icon or image in chart.js
- How do I change the 'months' language displayed on the date axis in Chart JS?
- How to change the cursor to a pointer when I hover over a bar in a ChartJS bar chart?
- How to align Chart.JS line chart labels to the center
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to display the labels in doughnut chart using ng2 charts?
- PrimeNg bar chart how to show a label for the y-axis
- How to make bar chart animation where all bars grow at the same speed?
- How to change Chart.js horizontal bar chart Width?
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- How to add background image in bar chart column in Chart.js?
- PrimeNg bar chart how do I hide the bar labels?
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- How to change the fontFamily of the labels and remove the grid in chart.js
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- How to use set the color for each bar in a bar chart using chartjs?
- angular-chart.js doughnut chart : How do I change width of the arc of a doughnut?
- How to change fonts and axis labels for Chart.js image rendering with QuickChart?
- chart.js: How do I make the radar chart axis labels bigger?
More Query from same tag
- VueJS TypeScript using ChartJS - Parsing error: '}' expected
- Display data labels on a pie chart in angular-chart.js
- Chart js custom separate legend returns error when clicking legend
- chart.js - link to other page when click on specific section in chart
- How to correctly transform data for a stack chart chartJS (data transformation)?
- How to add image inside the doughnut chart using chart.js?
- Chart.js Treemap Adding custom text to each rectangle
- Values above points - chart.js
- Horizontal xAxis labels in Chart.js
- Y axis limit in Chart JS Bar chart?
- Chart js how to remove numbers on circles of polar chart
- how to change size of point in ChartJS
- How to create Bar chart that gets updated from new ajax requests with ChartJS?
- Chart.js changes selected legend filters when using update function
- protoype js 1.0.6 is conflicting with chart js
- How to make bars change color based on value in chartsjs
- implementing horizontal bar chart
- Saving ChartJS chart showing all tooltips
- How to group and count missing values using linq
- Failed to create chart
- Chartjs: How to offset ticks on radarchart?
- ChartJS line chart or bar graph from a Java program
- My Chart.js Options referencing React state are being ignored
- ChartJS does not render correctly
- Using Chart.js with Pyramid Web Framework
- Pushing data from angular typescript to chart.js library complication
- Parsing JSON data into specific format for chart.js
- "Responsive" Chart.js chart is taller than the containing window
- How can have variable data be from a url in javascript
- displaying bar chart in laravel