I have a bar chart from chart.js but I can't get the chart.js datalabels plugin to show the values of each bar within the chart.
Here is the chart.js code...the labels and data within chart.js is pulling from an external google sheet via a simple php array.
The chart.js library is referenced in my header, followed by the chart.js datalabels plugin library:
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.9.4/dist/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels#0.7.0"></script>
<canvas height="225vh" id="yield-comparison-bar-chart"></canvas>
<script type="text/javascript">
new Chart(document.getElementById("yield-comparison-bar-chart"), {
type: 'bar',
data: {
labels: [
'<?php echo $holdings_array[29][3]; ?>',
'<?php echo $holdings_array[29][4]; ?>',
'<?php echo $holdings_array[29][5]; ?>',
'<?php echo $holdings_array[29][6]; ?>',
'<?php echo $holdings_array[29][7]; ?>',
'<?php echo $holdings_array[29][8]; ?>',
'<?php echo $holdings_array[29][9]; ?>',
],
datasets: [
{
label: "Distribution Yield (%)",
backgroundColor: ["#003b5c", "#b0ced8","#b0ced8","#b0ced8","#b0ced8","#b0ced8","#b0ced8"],
data: [
<?php echo number_format(floatval($holdings_array[30][3]),2); ?>,
<?php echo number_format(floatval($holdings_array[30][4]),2); ?>,
<?php echo number_format(floatval($holdings_array[30][5]),2); ?>,
<?php echo number_format(floatval($holdings_array[30][6]),2); ?>,
<?php echo number_format(floatval($holdings_array[30][7]),2); ?>,
<?php echo number_format(floatval($holdings_array[30][8]),2); ?>,
<?php echo number_format(floatval($holdings_array[30][9]),2); ?>,]
}
]
},
options: {
legend: { display: false },
title: {
display: false,
text: ''
},
plugins: {
datalabels: {
display: true,
color: '#333',
align: 'center',
anchor: 'center'
}
},
scales: {
yAxes: [{
ticks: {
// Include a % sign in the ticks
callback: function(value, index, values) {
return value + '%';
},
gridLines: {
display: false,
drawBorder: false,
},
}
}],
xAxes: [{
gridLines: {
display: false,
drawBorder: false,
},
}]
}
}
});
</script>
I would like for the values of each bar to be shown on top them please
Related
I am new to using Chart.js objects and have run into a bug or syntax error using "yAxisID" as a property of my datasets. I am sending to QuickChart using the PowerShell command:
Invoke-RestMethod -Method Post -Uri "https://quickchart.io/chart/create" -Body $json -ContentType "application/json"
Note that the png outfile approach seems to work fine..
Invoke-RestMethod -Method Post -Uri "https://quickchart.io/chart" -Body $json -ContentType "application/json" -OutFile <outfile.png>"
Here is the $json
{
data: {
labels: [
'2022Dec2',
'2022Dec7',
'2022Dec12',
'2022Dec22',
'2022Dec27',
'2022Dec30',
],
datasets: [
{
data: [0.0, -3.38, -1.96, -6.44, -6.27, -6.02],
label: 'SPY',
borderDash: [1, 0],
fill: false,
borderColor: '#4E79A7',
yAxisID: 'y',
},
{
data: [0.0, -4.11, -2.38, -8.82, -9.9, -8.98],
label: 'QQQ',
borderDash: [1, 0],
fill: false,
borderColor: '#F28E2B',
yAxisID: 'y1',
},
],
},
options: {
title: {
text: 'Stock Symbol % Change',
display: true,
},
scales: {
yAxes: [
{
position: 'left',
id: 'y',
display: true,
},
{
position: 'right',
gridLines: {
drawOnChartArea: false,
},
id: 'y1',
display: true,
},
],
},
legend: {
position: 'bottom',
},
},
type: 'line',
}
I tried deleting the "yAxisID" lines of the $json and that seems to work fine (although on just a single y-axis). I am expecting the "yAxisID" lines to plot the first dataset on the left y-axis and the second dataset on the right y-axis.
I created a chart with chartJS and I would like to change the color of data label and date. What's wrong?
const labels = <?php echo json_encode($Date) ?>;
const data = {
labels: labels,
datasets: [{
label: 'Données Vent',
backgroundColor: 'gray',
borderColor: 'rgb(255, 99, 132)',
data: <?php echo json_encode($Vent) ?>,
data: <?php echo json_encode($Rafale) ?>,
}]
};
const config = {
type: 'line',
data: data,
options: {
color: 'white', //couleur du titre graphique
scales: {
y: {
color:"white",
beginAtZero: true
}
}
},
};
const myChart = new Chart(document.getElementById('myChart'), config);
changed label color like follows:
options: {
legend: {
labels: {
fontColor: "blue",
fontSize: 18
}
},
}
#Robin Hood #pistou
finally it's now different. i do like this:
hAxis: { direction: -1,
textStyle: {
color:'white',
}},
vAxis: {
textStyle: {
color:'white',
}},
Can you please tell me how to display values in bars as below in bar chart. I am using ChartJS 3.6 version.
I have tried below code, it didn't work
const chartOptions = {
responsive: false,
plugins: {
legend: {
display: false,
},
datalabels: {
display: true,
align: 'center',
anchor: 'center',
},
},
}
For the datalabels plugin to work you need to install it and register it:
Bundler:
npm install chartjs-plugin-datalabels
import Chart from 'chart.js/auto';
import ChartDataLabels from 'chartjs-plugin-datalabels';
Chart.register(ChartDataLabels);
Script tag:
<script src="https://cdn.jsdelivr.net/npm/chart.js#3/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels#2"></script>
Chart.register(ChartDataLabels);
Please take a look at below runnable code and see how it can be done with chartjs-plugin-datalabels.
Chart.register(ChartDataLabels);
new Chart("barChart", {
type: 'bar',
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
label: 'Data',
data: [5, 8, 24, 14],
backgroundColor: 'rgb(124, 124, 255)',
barPercentage: 0.5
}]
},
options: {
plugins: {
datalabels: {
color: 'white',
font: {
weight: 'bold'
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels#2"></script>
<canvas id="barChart"></canvas>
Remove datalabels: "center" and anchor: "center"
I'm trying to generate a bar graph but after generating when i hover on bar first graph the hover works and tooltip is also coming correctly but when i hover on second or third bar the bar color becomes black
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.8.0"></script>
<canvas id="barChart" style="margin-top:10px"></canvas>
var ctxBarChart = document.getElementById('barChart').getContext('2d');
var chart = new Chart(ctxBarChart, {
// The type of chart we want to create
type: 'bar',
// The data for our dataset
data: {
labels: ['bar 1', 'bar 2', 'bar 3'],
datasets: [{
label: 'bar graph label',
backgroundColor: ['#ff7f27', 'DodgerBlue', 'Green'],
borderColor: ['#ff7f27', 'DodgerBlue', 'Green'],
data: [24.6154, 28.4, 28.4]
}]
},
// Configuration options go here
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
return tooltipItem.yLabel.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + '%';
}
}
},
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
gridLines: {
display: false
},
ticks: {
// max: 100,
min: 0,
// stepSize:20
}
}]
}
}
});
but the result i'm getting is like this please see following images for bar 1 and bar 2
This seems to be caused by case-sensitivity of colour names.
Change your colours to all lowercase like so:
backgroundColor: ['#ff7f27', 'dodgerblue', 'green'],
borderColor: ['#ff7f27', 'dodgerblue', 'green'],
The CSS3 specification explicitly states the names are case-insensitive so this looks like a bug in Chart.js
I am trying to use data from the table to create a bar graph using high-charts in rails.I have a table named school and it contains two columns i.e,name and attendance.I want to show student name on x-axis and attendance on y-axis.My JavaScript code is:
$(function () {
// Create the chart
$('#student')high-charts({
chart: {
type: 'column'
},
title: {
text: 'student details'
},
x Axis: {
type: 'category'
},
y Axis: {
title: {
text: 'Total attendance'
}
},
legend: {
enabled: false
},
plot-options: {
series: {
border-width: 0,
data-labels: {
enabled: true,
format: '{point.y:.1f}%'
}
}
},
tool-tip: {
header-format: '<span style="font-size:11px">{series.name}</span> <Br>',
point-format: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<BR/>'
},
series: [ {
name: 'Stu',
id: 'Stu',
data: [
[<%= #students.each do |s|%>
'<%= s.name %>',
<%= s.attendance%>
<%end%>
]
]
}
in my HTML i have :
<div class="col-md-8">
<div class="x_panel">
<div id="student" style="min-width: 310px; height: 400px; margin: 0 auto">
</div>
</div>
</div>
I was unable to load the bar graph.Please help
try this
$(function () {
// Create the chart
new Highcharts.Chart({
chart: { renderTo: 'student' },
title: {
text: 'student details'
},
x Axis: {
type: 'category'
},
y Axis: {
title: {
text: 'Total attendance'
}
},
legend: {
enabled: false
},
plot-options: {
series: {
border-width: 0,
data-labels: {
enabled: true,
format: '{point.y:.1f}%'
}
}
},
tool-tip: {
header-format: '<span style="font-size:11px">{series.name}</span> <
Br>',
point-format: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<BR/>'
},
series: [ {
name: 'Stu',
id: 'Stu',
data: [
[<%= #students.each do |s|%>
'<%= s.name %>',
<%= s.attendance%>
<%end%>
]
]
}
Create a new highchart and render div in chart