score:29
There is now on option "mirror" to make the label appear inside the bar.
Example of "options" config for a horizontalBar chart :
options: {
scales: {
yAxes: [{ticks: {mirror: true}}]
}
}
Doc : http://www.chartjs.org/docs/latest/axes/cartesian/#common-configuration
score:0
It seems like there is no direct option for this right now. A possible implementation is iterating over bars/columns by using onAnimationComplete hook. You can view this question for detailed explanations how to display data values on Chart.js
I would like you remind that this has a drawback. Since it uses onAnimationComplete, whenever an animation happens on chart, values will be disappear for a second and appear again.
score:0
Since on "bar" chart doesn't work the "mirror" option...
I've found a workaround solution: Fiddle link
I used a chart.js 2.8.0 version modified in this link
<html>
<body>
<canvas id="myChart" width="800" height="600">
</canvas>
</body>
</html>
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: ["ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT"],
datasets: [{
label: "Quota Eni mensile",
backgroundColor: [
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)"
],
borderColor: [
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)",
"rgba(255, 255, 153, 1)"
],
borderWidth: 1,
data: [10, 11, 18, 10, 13, 28, 12, 16]
}
]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
ticks:{
maxRotation: 90,
minRotation: 90,
display: "top"
},
}],
yAxes: [{
display: false
}]
},
tooltips: {
enabled: true
},
maintainAspectRatio: true,
responsive: true
}
});
Chart.plugins.register({
/* Adjust axis labelling font size according to chart size */
id: "responsiveXlabels",
beforeDraw: function(c) {
var chartHeight = c.chart.height;
var size = (chartHeight * 2.5) / 100;
var xAxis = c.scales["x-axis-0"];
xAxis.options.ticks.minor.fontSize = size;
xAxis.height = 10;
xAxis.minSize.height = 10;
c.options.scales.xAxes[0].ticks.padding = -size * 4.5;
xAxis.margins.bottom = size * 12.5;
},
afterDraw: function(c) {
c.options.scales.xAxes[0].ticks.padding = -158;
}
});
the responsiveXlabel plugin is used to transform text on resize. There's only one thing remained to solve: align text at the bottom of the axis.
Hope this helps who wants to implement mirroring on X Axis and satisfy responsive request.
score:3
You can achieve this effect in chartjs 2.0 + by rotating the labels 90 degrees and applying negative padding, so that the label moves up inside the 'bar'. Something like this:
new Chart(document.getElementById(id), {
type: 'bar',
data: data,
options: {
scales: {
xAxes: [
{
ticks: {
autoSkip: false,
maxRotation: 90,
minRotation: 90,
padding: -110
}
}
]
}
}
});
See the tick configuration documentation for more information.
score:9
With reference to the original solution to displaying data value by using HTML5 Canvas fillText()
method in the animation
's onComplete
, the code below will get you what you need:
The key to custom-displaying any chart related data is inspecting the chart's dataset as in this.data.datasets
below. I did this by inspecting where the different values are in this dataset, as well as the position to place them for the fillText method.
Inspecting the chart's dataset: Image
Script (Chart.js 2.0 & up):
var barOptions = {
events: false,
showTooltips: false,
legend: {
display: false
},
scales:{
yAxes: [{
display: false
}]
},
animation: {
onComplete: function () {
var ctx = this.chart.ctx;
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontFamily, 'normal', Chart.defaults.global.defaultFontFamily);
ctx.textAlign = 'left';
ctx.textBaseline = 'bottom';
this.data.datasets.forEach(function (dataset) {
for (var i = 0; i < dataset.data.length; i++) {
var model = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._model,
left = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._xScale.left;
ctx.fillStyle = '#444'; // label color
var label = model.label;
ctx.fillText(label, left + 15, model.y + 8);
}
});
}
}
};
jsFiddle: http://jsfiddle.net/jfvy12h9/
Source: stackoverflow.com
Related Query
- Chart.js - Writing Labels Inside of Horizontal Bars?
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- Chart JS stacked labels instead of making them horizontal
- Chart, X and Y-Axis labels are blurred in horizontal bar chart using chart.js
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- How write the labels of the data inside a Doughnut Chart made with Chart.js?
- Stacked Horizontal Bars not in align Chart JS
- How to make labels on both side from horizontal bar chart js
- Reduce spacing between bars in horizontal bar chart (chart.js)
- Chart JS horizontal bar chart width of bars and the chart
- Chart.js Labelling - Need Two Labels on Y-axis of Horizontal Bar Chart
- Chart.js bar chart bars and labels do not align
- How to set percentage value by default on each bars in horizontal bar chart js
- How to add text inside the doughnut chart using Chart.js?
- Limit labels number on Chart.js line chart
- Chart.js Show labels on Pie chart
- Draw horizontal line on chart in chart.js on v2
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How to prevent first/last bars from being cut off in a chart with time scale
- chart.js: Show labels outside pie chart
- Horizontal stacked bar chart with chart.js
- Chart.js Bar Chart - how to chart bars from 0
- Horizontal bar chart in chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
- Chart JS data labels getting cut
- chartjs datalabels change font and color of text displaying inside pie chart
- How to set labels align left in Horizontal Bar using chart.js?
- How to display Line Chart dataset point labels with Chart.js?
- How to fix bars in Chart.js with long labels
More Query from same tag
- Extract data from an array of Object
- How to use generated HTML legends to enable or disable datasets in Chart.js
- How to align Chart.JS line chart labels to the center
- Chart.js repeating colors?
- How can I cause a legend to appear to the right of the pie (Chart.JS)?
- chart.js addData breaks function v1.0.2
- Chart.js: Truncate Legend's Labels
- How can I set a class to a HTML element which is created by custom tooltip function?
- Move tooltip further from data point for Chart.js?
- How to get canvas/ctx object in Chart.js for an Angular project
- Update Chart.js plugin
- Charts.js is automatically adding comma as thousands separator when it shouldn't
- react-chartjs-2 fill property not working?
- Chart.js Legend Customization
- Cannot visualise chartJS data in ASP .NET Core
- Multi-checkboxes And Chart Data
- ChartJS Line Charts - remove color underneath lines
- Chart.js: Pie chart legend "onclick" gets overwritten by "options.onclick" if both are present
- Make Chart.js Radar labels clickable
- How to hide the labels of graphs that are not toggled in ChartJS
- Chart.js & BPopUp - Make a Chart in a PopUP
- display multiple charts angular.js chart.js
- Given point in tooltiptext
- How to get item what been clicked on Chart (primeNg chart + angular application)
- Chart.js Radar Chart How to Remove Outer Labels
- How to display label inside pie chart in Chartkick?
- ChartJS zoom/pan onPan event not firing in React useEffect hook
- JQuery calling global from local
- How to use async data for use with chart.js in ionic
- Is there any way to get y of specific x which not belongs to the dataset in Chart.js graph?