score:60
i pulled out the data from being defined inside of mychart that way i could pull out the max value from the dataset. then inside of the yaxes you can set the max ticks to be the max value + 10 from your data set. this ensures that the top bars in the graph will not go off the edge of the canvas and not display their value.
var ctx = document.getelementbyid("mychart");
debugger;
var data = {
labels: ["2 jan", "9 jan", "16 jan", "23 jan", "30 jan", "6 feb", "13 feb"],
datasets: [{
data: [150, 87, 56, 50, 88, 60, 45],
backgroundcolor: "#4082c4"
}]
}
var mychart = new chart(ctx, {
type: 'bar',
data: data,
options: {
"hover": {
"animationduration": 0
},
"animation": {
"duration": 1,
"oncomplete": function() {
var chartinstance = this.chart,
ctx = chartinstance.ctx;
ctx.font = chart.helpers.fontstring(chart.defaults.global.defaultfontsize, chart.defaults.global.defaultfontstyle, chart.defaults.global.defaultfontfamily);
ctx.textalign = 'center';
ctx.textbaseline = 'bottom';
this.data.datasets.foreach(function(dataset, i) {
var meta = chartinstance.controller.getdatasetmeta(i);
meta.data.foreach(function(bar, index) {
var data = dataset.data[index];
ctx.filltext(data, bar._model.x, bar._model.y - 5);
});
});
}
},
legend: {
"display": false
},
tooltips: {
"enabled": false
},
scales: {
yaxes: [{
display: false,
gridlines: {
display: false
},
ticks: {
max: math.max(...data.datasets[0].data) + 10,
display: false,
beginatzero: true
}
}],
xaxes: [{
gridlines: {
display: false
},
ticks: {
beginatzero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.5.0/chart.bundle.js"></script>
<canvas id="mychart" width="100" height="100"></canvas>
score:0
this works in my case but its show values in mid of the bar.
chart.chart.config.options.animation["oncomplete"] = function () {
var ctx = chart.chart.ctx;
ctx.font = '22px "helvetica neue", helvetica, arial, sans-serif';
ctx.textalign = 'center';
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,
scale_max = dataset._meta[object.keys(dataset._meta)[0]].data[i]._yscale.maxheight;
ctx.fillstyle = '#444';
var y_pos = model.y + 50;
if ((scale_max - model.y) / scale_max >= 0.5)
y_pos = model.y + 20;
ctx.filltext(dataset.data[i], model.x, y_pos);
}
});
}
score:1
you can consider plugin which show data value on top of each bar.
plugins: {
afterdatasetsdraw: function (context, easing) {
var ctx = context.chart.ctx;
context.data.datasets.foreach(function (dataset) {
for (var i = 0; i < dataset.data.length; i++) {
if (dataset.data[i] != 0) {
var model = dataset._meta[object.keys(dataset._meta)[0]].data[i]._model;
var texty = model.y + (dataset.type == "line" ? -3 : 15);
ctx.font = chart.helpers.fontstring(chart.defaults.global.defaultfontsize, 'normal', chart.defaults.global.defaultfontfamily);
ctx.textalign = 'start';
ctx.textbaseline = 'middle';
ctx.fillstyle = dataset.type == "line" ? "black" : "black";
ctx.save();
ctx.translate(model.x, texty-15);
ctx.rotate(4.7);
ctx.filltext(dataset.data[i], 0, 0);
ctx.restore();
}
}
});
}
}
live code: link
score:2
you can use add some padding-top inside your chart options
layout: {
padding: {
left: 0,
right: 0,
top: 30,
bottom: 0
}
},
score:3
this worked in my case.(plugins section below)
options: {
responsive: true,
scales: {
yaxes: [{
ticks: {
beginatzero: true,
}
}]
},
plugins: {
datalabels: {
anchor: 'end',
align: 'top',
formatter: math.round,
font: {
weight: 'bold'
}
}
}
}
happy coding.
score:19
here is a more robust solution that will display the datapoint value inside the bar for cases where the axis height is close to the bar height. in other words, this will display the value above the bar and/or below the bar if the text will extend beyond the canvas visible area.
chart.plugins.register({
afterdraw: function(chartinstance) {
if (chartinstance.config.options.showdatapoints) {
var helpers = chart.helpers;
var ctx = chartinstance.chart.ctx;
var fontcolor = helpers.getvalueordefault(chartinstance.config.options.showdatapoints.fontcolor, chartinstance.config.options.defaultfontcolor);
// render the value of the chart above the bar
ctx.font = chart.helpers.fontstring(chart.defaults.global.defaultfontsize, 'normal', chart.defaults.global.defaultfontfamily);
ctx.textalign = 'center';
ctx.textbaseline = 'bottom';
ctx.fillstyle = fontcolor;
chartinstance.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;
var scalemax = dataset._meta[object.keys(dataset._meta)[0]].data[i]._yscale.maxheight;
var ypos = (scalemax - model.y) / scalemax >= 0.93 ? model.y + 20 : model.y - 5;
ctx.filltext(dataset.data[i], model.x, ypos);
}
});
}
}
});
you can enable your chart to use the plugin by adding the below property in your chart options.
showdatapoints: true,
score:33
the fastest way i found to do this was to use this plugin: https://github.com/chartjs/chartjs-plugin-datalabels
labels can be added to your charts simply by importing the plugin to the js file e.g.:
import 'chartjs-plugin-datalabels'
and if you want to apply it values on top (globally), simply set these options in your code:
chart.defaults.global.plugins.datalabels.anchor = 'end';
chart.defaults.global.plugins.datalabels.align = 'end';
more options can be found here: https://chartjs-plugin-datalabels.netlify.com/options.html
Source: stackoverflow.com
Related Query
- Show values on top of bars in chart.js
- How to show data values in top of bar chart and line chart in chart.js 3
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- How to show values on top of bars in a PDF using chart.js and chartjs-node-canvas?
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Show all values in Chart js y axis
- Show data on top of bar chart in Chart.js v 2.7.1
- Show point values in Radar Chart using chart.js
- Show data values in Chart.js bars (version 3)
- Chart.JS - show values on top of points
- Force ChartJS to show Doughnut chart with null values
- Chart.js: Reverse bar chart with regular bars (Bottom to top instead of top to bottom)
- Show values in Chart.js Pie chart parts
- How to show a "total" sum label on the top of stacked bars
- I am using chart.js I sent my time values throw my api as timestamp know i need to show them in the chart as date format
- How to show the chartjs bar chart data values labels as text?
- Bar chart (chart.js) with only 2 points does not show one of the bars
- Chart js 2 bars with one customize label on top
- Show the values of each arc on doughtnut chart of chart.js
- How remove duplicates xAxis labels and show all values on chart
- Chart.js Change color of the values *inside* the bars of a Bar chart
- Chart.js stacked bar chart text on top of the stacked bars
- ChartJs multiaxis chart show different label bottom and top
- Radar chart - show values near vertices in chart.js
- Dynamically update values of a chartjs chart
- Chart.js Show labels on Pie chart
- How to prevent first/last bars from being cut off in a chart with time scale
- chart.js: Show labels outside pie chart
- Chart.js Bar Chart - how to chart bars from 0
- line chart with {x, y} point data displays only 2 values
More Query from same tag
- Chartjs with multiple y axes and different scales
- In chart.js how can I change the x axis on my line \chart from January-December to October-September? Basically, fiscal year instead of calendar year
- Chartjs Plugin Deferred throws "Uncaught TypeError: Cannot read property 'plugins' of undefined" error
- Chart.js - Font only renders after you reload the canvas
- ChartJS Tooltips with time axis and multiple datasets
- Chart.Js is keeping old data after AJAX call
- Chart.js time series
- Allow display of multiple data attributes in tooltip of Chart.js
- Chart JS not displaying graph
- Chart.js add border around line chart, and yAxis unit title ([s])
- ChartJS: Draw line between two data points on hover
- How to make a interactive legend with chartjs
- Change point color on click using ChartJS
- Hide/disable tooltips chart.js
- Draw asynchronous information on a graph with VueJS
- ChartJS -- How do I change scale color when I have to scales?
- chart js -data on chart in rotated form
- chart.js - link to other page when click on specific section in chart
- How do I fetch data from 2 tables and display into 1 chartjs
- ChartJS Line chart cut off at the top and bottom
- Chart.js removing first dataset and label
- Line chart doesn't work with type time chart.js
- Transparentize some bars of dataset in ChartJS
- Set default line style in chart.js
- Chart.js is not displayed when page loads first time
- Chart.js with 2 y-axis
- Undefined x values in chart.js
- Adding different point style to each column from parsed csv in chart.js
- Is there anyway add 2 or more chart with foreach loop
- Bootstrap Card Render Problem in Google Chrome