score:2
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 with the desired styles underneath each bar.
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 = value == 60 ? 'red' : 'blue';
ctx.fillText(value, x, yAxis.bottom + 17);
});
ctx.restore();
}
}],
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First Dataset",
data: [60, 59, 80, 81, 60, 55, 40],
fill: false,
backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)", "rgba(75, 192, 192, 0.2)", "rgba(54, 162, 235, 0.2)", "rgba(153, 102, 255, 0.2)", "rgba(201, 203, 207, 0.2)"],
borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)", "rgb(153, 102, 255)", "rgb(201, 203, 207)"],
borderWidth: 1
}]
},
options: {
layout: {
padding: {
bottom: 20
}
},
scales: {
xAxes: [{
ticks: {
display: false
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="myChart" height="90"></canvas>
Source: stackoverflow.com
Related Query
- Chartjs change the specific label color in x axis in callback function
- In ChartJS how do I change the color of a label in the legend?
- ChartJS fails to render one of the lines in cartesian chart following update after change to max Y axis label value
- How to change the color of Chart.js points depending on the label
- How to change the color of legend in chartjs and be able to add one more legend?
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
- How to change the label color in chart.js?
- ChartJS align axis label to the top
- Is there any way to change the font color and size of labels in Chartjs 3.0.0
- Conditional in ChartJS axes tick callback function isn't returning the expected labels
- Change label color Y and X axis chart.js
- How to change the color of y axis labels in different color in Chart.js
- Change label color over a specific value in Chart.js
- How to Change the Y axis of an Primefaces 7.0 ChartJS Linechart with Java?
- How to change bar color acording to label value in chartjs
- 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
- how to change the label color and set y-axis values 1k intervals and hide y-axis?
- How can i change the every legend label font color using chart.js version 2.8.0 (spacial line chart)?
- Change Chartjs label color on click without losing hover
- How can I change the color of certain lines in chartjs / vue-chartjs?
- Chartjs change grid line color
- Change X and Y axis font color
- Change color of X and Y axis values in Chart.js
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- Chart Js change text label orientation on Ox axis
- chartjs datalabels change font and color of text displaying inside pie chart
- show label in tooltip but not in x axis for chartjs line chart
- Change point size and color on hover in chartjs
- Change point color on click using ChartJS
- How do I change the 'months' language displayed on the date axis in Chart JS?
More Query from same tag
- Pass objects from javascript to Chart.JS through EJS
- Hovering over chart.js values in Meteor onRendered function causes chart axis shift
- ChartJS disable gridlines outside chart area
- Custom tooltip in Chart.js
- ChartJS Scatter plot with JSON list variable not working
- Last value not showing in bar graph of charts.js-library
- Chart.js Treemap Adding custom text to each rectangle
- Annotation events not update the chart
- Chart.js - style legend: smaller circles
- chart js : how to display title or lable?
- How to hide more than one legend on Chartjs?
- how can I show only 2 numbers on the xAxe in Chartjs having more than two numbers on the chart? (line chart)
- Don't show label tooltip in Chart.js if hover is less than 1 second
- Chart.js bar chart is overflowing its containing element
- chart annotations not showing in Angular 2
- How to specify hover color angular-chart.js
- How can I achieve something like this image with chart.js(react-chartjs-2)?
- Min value of chart (Chart.js)
- how to highlight a specific area on chartjs line chart
- Draw horizontal line on chart in chart.js on v2
- Chartjs bar min, max and avarage value
- Chart Js Doughnut chart giving error
- Change height of chartjs dynamically
- charts.js won't show legend and on chart descriptions
- How to send data from struts2 to a javascript function to draw a chart
- Using Chart.Js to plot a scatter plot from an Array
- Chart.js and right side free space
- Jinja throughs "SyntaxError: expected property name, got '%' " error
- Tooltip is not displayed when using annotation in ChartJs
- how to show data value on bar chart body rather than using tooltip?