score:3
You can make use of the Plugin Core API. It offers different hooks that may be used for executing custom code. In below code snippet, I use the afterDraw
hook to draw text of the same color as the corresponding bar.
chart.data.labels.forEach((l, i) => {
var value = chart.data.datasets[0].data[i];
var x = xAxis.getPixelForValue(l);
ctx.fillStyle = chart.data.datasets[0].backgroundColor[i];
ctx.fillText(l, x, yAxis.bottom + 17);
});
When drawing your own tick labels, you need to instruct Chart.js not to display the default labels. This can be done through the following definition inside the chart options.
scales: {
xAxes: [{
ticks: {
display: false
}
}],
You also need to define some padding for the bottom of the chart, otherwise you won't see your custom tick labels.
layout: {
padding: {
bottom: 20
}
},
Please take a look at the following sample code that illustrates how to change the labels on the x-axis depending on the values.
new Chart('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'];
ctx.save();
ctx.textAlign = 'center';
ctx.font = '12px Arial';
chart.data.labels.forEach((l, i) => {
var value = chart.data.datasets[0].data[i];
var x = xAxis.getPixelForValue(l);
ctx.fillStyle = chart.data.datasets[0].backgroundColor[i];
ctx.fillText(l, x, yAxis.bottom + 17);
});
ctx.restore();
}
}],
data: {
labels: ["-3", "-2", "-1", "0", "+1", "+2", "+3"],
datasets: [{
label: "My First Dataset",
data: [60, 59, 80, 81, 60, 55, 40],
fill: false,
backgroundColor: ['rgba(245,88,97,1)', 'rgba(245,88,97,1)', 'rgba(245,88,97,1)', 'rgba(145,151,163,1)', 'rgba(70,180,220,1)', 'rgba(70,180,220,1)', 'rgba(70,180,220,1)'],
borderWidth: 1
}]
},
options: {
layout: {
padding: {
bottom: 20
}
},
scales: {
xAxes: [{
ticks: {
display: false
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
canvas {
max-width: 300px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="myChart" height="200"></canvas>
Source: stackoverflow.com
Related Query
- X-axis multiple colored label for bar chart using chart.js
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- 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
- Line chart plotting multiple points for duplicate data on x and y axis using chart.js
- show label in tooltip but not in x axis for chartjs line chart
- Chart JS show multiple data points for the same label
- PrimeNg bar chart how to show a label for the y-axis
- Multiple stacked bar chart using ChartJs
- Adding custom text to Bar Chart label values using Chart.js
- Minimum value for x Axis doesn't work for horizontal bar chart | ChartJS
- Displaying line chart for multiple months using chart.js
- How to use set the color for each bar in a bar chart using chartjs?
- How to draw multiple color bar in a bar chart using chart.js
- display vertical axis label in line chart using chart.js
- Chart Js reduce text size for label on X axis
- Set fixed label size for grouped bar chart in angular Chartjs
- Linear x axis for bar chart chartjs
- How do I get a different label for each bar in a bar chart in ChartJS?
- How do you set x and y axis and Title for a line chart using charts.js?
- Using ChartJS to create a multiple grouped bar chart - see picture below
- ChartJS, Multiple line in bar chart label
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- How to set Solid label in bar chart using Chart JS
- How to custom index label on each bar chart using chartjs?
- Unable to hide x axis for horizontal bar chart vue js
- Display ellipsis for null or empty values in the bar chart using Chart.js
- Displaying line chart for multiple datasets using chart.js
- subcategories for each bar in multibar chart using chartjs
- Using custom dataformat in chart.js for Multi Axis Line Chart
More Query from same tag
- Changing Fonts In Javascript's CanvasJS
- Chart.js not drawing on mobile (safari/chrome) fine on desktop
- How can I process json file and display some data in form of charts in django
- how to differentiate chart with colors
- Properly scaling xAxis with time in chart.js
- How to get rid of large left and right margins in chart.js donut chart?
- ng2-charts - Angular 4 working example
- Nesting directives and using the data inside the local scope
- Add zoom event handler to charts for chartjs with chartjs-plugin-zoom
- Angular Dynamic Chart Generate
- Two independent time axes in Chart.js
- About the charts in react-native
- Gradient colour in backgroundColor JS
- Removing exceeding parts of axis
- How can I add a euro sign (€) to all tooltips in my chart js line chart
- Chart.js Passing an array starting with 1 instead of 0 problem
- How to use GET method to choose the right table to show ChartJS graph?
- Chart.js: chart does not update
- Chart.js: How to group legends of dataset? So one toggle for multiple datasets?
- Why am I getting "plot.new has not been called yet" calling legend() after chartJSRadar()
- Bootstrap modal showing when clicking individual points in Linechart but not in individual bars in Barchart
- Chart.js (line chart) tooltip duration/delay
- Stacked Group Bar from Chart.js : Can I give each stack group a unique color?
- React Native Chart not showing data
- Chart Js Line chart with fill on click with full information of its legend text
- ChartJS vertical bar chart - measure percentage of the data in each column based on a specified max
- How to move Chart.js legend?
- Why scatter chart does not show axes?
- How do I get a chart.js chart to display data in a Node/React web application?
- How to display only one label in a multi line char?