How to show label for second y-axis in charts.js? - chart.js

I have two y-axis in my charts.js scatter chart, and all my data is displayed correctly.
However, whilst the label for the first y-axis is showing, the label for the second y-axis is not.
Here's my code:
window.laChart = new Chart(ctx, {
type: 'scatter',
data: {
datasets: allDatasets
},
options: {
scales: {
xAxes: [{
scaleLabel: {
display: true,
labelString: X_AXIS_LABEL
},
ticks: {
min: 0,
max: 18,
stepSize: 1,
// Format the x-axis values
callback: function(value, index, values) {
return value;// + ".000";
}
},
gridLines: {
display: false
}
}],
yAxes: [
{
ticks: {
beginAtZero: true,
min: 0,
max: 100
},
position: "left",
id: "y-axis-1",
type: "linear"
},
{
ticks: {
beginAtZero: true,
min: 0,
suggestedMax: 10
},
position: "right",
id: "y-axis-2",
type: "linear"
},
{
gridLines: {
display: false
},
ticks: {
maxTicksLimit: 3, // This will give us a top tick, middle tick and bottom tick.
callback: function(value, index) {
if (index === 1) { // This is the middle tick.
return Y1_AXIS_LABEL;
}
else {
return '';
}
}
}
}]
}
}
And here's a screenshot with the second y-axis label missing:
How do I make the label for the second y-axis appear?
SOLUTION
LeeLenalee's answer worked, but it left me with rotated labelStrings. I adapted his answer to solve this issue with this final code:
yAxes: [
{
ticks: {
beginAtZero: true,
min: 0,
max: 100
},
scaleLabel: {
display: false,
labelString: Y1_AXIS_LABEL
},
position: "left",
id: "y-axis-1",
type: "linear"
},
{
ticks: {
beginAtZero: true,
min: 0
},
scaleLabel: {
display: false,
labelString: Y2_AXIS_LABEL
},
position: "right",
id: "y-axis-2",
type: "linear"
},
// Change the orientation of the first y-axis label.
{
gridLines: {
display: false
},
ticks: {
maxTicksLimit: 3, // This will give us a top tick, middle tick and bottom tick.
callback: function(value, index) {
if (index === 1) { // This is the middle tick.
return Y1_AXIS_LABEL;
}
else {
return '';
}
}
},
position: "left"
},
// Change the orientation of the second y-axis label.
{
gridLines: {
display: false
},
ticks: {
maxTicksLimit: 3, // This will give us a top tick, middle tick and bottom tick.
callback: function(value, index) {
if (index === 1) { // This is the middle tick.
return Y2_AXIS_LABEL;
}
else {
return '';
}
}
},
position: "right"
}
]
Final screenshot:

What you did is implement a new scale as replacement for the scale label, this is not the way you should do it because the scale will only be on the left. You have to use the scalelabel as you did with the X axis for both yAxes like this:
yAxes: [
{
ticks: {
beginAtZero: true,
min: 0,
max: 100
},
scaleLabel: {
display: true,
labelString: Y1_AXIS_LABEL
},
position: "left",
id: "y-axis-1",
type: "linear"
},
{
ticks: {
beginAtZero: true,
min: 0,
suggestedMax: 10
},
scaleLabel: {
display: true,
labelString: Y2_AXIS_LABEL
},
position: "right",
id: "y-axis-2",
type: "linear"
}]

Related

Chart.js move x axis labels to the right

I'm using the latest chart.js to draw a simple histogram.
The histogram is fine but I actually need the x labels to be between the borders of every bar like this histogram_right_labels, are there any methods to approach this?
I've tried offsetGridLines: true and offsetGridLines: false, but what they actually did is moving the grid lines and not the labels, unlike what I read about their actual behaviors, I think it might be a conflict in my option settings but I don't know which, any help would be appreciated.
options: {
maintainAspectRatio: false,
layout: {
padding: {
left: 10,
right: 25,
top: 25,
bottom: 0
}
},
scales: {
xAxes: [{
gridLines: {
//display: false,
drawBorder: false,
padding: 20,
offsetGridLines: true
},
ticks: {
beginAtZero: true,
}
}],
yAxes: [{
ticks: {
padding: 10,
},
gridLines: {
color: "rgb(234, 236, 244)",
zeroLineColor: "rgb(234, 236, 244)",
drawBorder: false,
borderDash: [2],
zeroLineBorderDash: [2]
}
}],
},
legend: {
labels: {
boxWidth: 20
},
display: false
},
tooltips: {
backgroundColor: "rgb(255,255,255)",
bodyFontColor: "#858796",
titleMarginBottom: 10,
titleFontColor: '#6e707e',
titleFontSize: 14,
borderColor: '#dddfeb',
borderWidth: 1,
xPadding: 15,
yPadding: 15,
displayColors: false,
intersect: false,
mode: 'index',
caretPadding: 10,
},
}
You can add a second x-axis of type: 'linear' as follows.
{
type: 'linear',
ticks: {
min: 0,
max: labels.length,
stepSize: 1,
callback: (v, i) => i == 0 ? '' : labels[i - 1]
}
}
Note that I use a ticks callback method in order to provide the desired tick label at given index.
Then you should also hide the grid lines and ticks of the default x-axis.
{
gridLines: {
display: false
},
ticks: {
display: false
}
}
Please take a look at below runnable sample code and see how it works.
const labels = ['4.80', '9.60', '14.40', '19.20', '24.00', '28.80'];
new Chart(document.getElementById('myChart'), {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: "My Dataset",
data: [4, 8, 5, 7, 2, 4],
backgroundColor: 'orange',
categoryPercentage: 1,
barPercentage: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
gridLines: {
display: false
},
ticks: {
display: false
}
},
{
type: 'linear',
ticks: {
min: 0,
max: labels.length,
stepSize: 1,
callback: (v, i) => i == 0 ? '' : labels[i - 1]
}
}
]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<canvas id="myChart" height="90"></canvas>
you can use this to put the tooltips to the right. Possibilities are top, left, right and bottom
options: {
title: {
display: true,
position: 'right'
}
}

Chart.Js - Hightligh an area in the background

I need to highlight a fixed area of my chart with a different color between the values 0.5 and 1.5.
Currently my chart is as below:
The desired result is something like this:
As you can see in the above image, the intention is to have a fixed bar with a different color, that is always touching the maximum Y value (this chart auto adjusts the maximum Y value considering the plotted values). The idea is to show the red bars that are in or near this region.
Here is the currently config that I have for this chart:
configPotential = {
type: 'bar',
data: {
labels: [],
datasets: [{
data: [],
borderWidth: 0,
borderColor:'#E16972',
fill: true,
backgroundColor: "#E16972",
order: 1
}]
},
options: {
animation: {
duration: false, //remove animation
},
responsive: true,
tooltips: {
enabled: false
},
title: {
display: false
},
legend: {
display: false
},
elements: {
point:{
radius: 0
}
},
scales: {
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'PSD (Hz/s²)'
},
ticks: {
beginAtZero: true,
suggestedMax: 0.2
}
}],
xAxes: [{
display: false,
},
{
gridLines : {
display : false
},
scaleLabel: {
display: false
},
type: 'linear',
ticks: {
beginAtZero: true,
autoSkip: false,
min: 0,
max: 0.5,
stepSize: 0.1,
}
}]
}
}
}
Resolved using the plugin Chart.Js Annotation - enter link description here
config = {
type: 'bar',
data: {
labels: [],
datasets: [{
data: [],
borderWidth: 0,
borderColor:'#E16972',
fill: true,
backgroundColor: "#E16972",
order: 1
}]
},
options: {
animation: {
duration: false, //remove animation
},
responsive: true,
tooltips: {
enabled: false
},
title: {
display: false
},
legend: {
display: false
},
elements: {
point:{
radius: 0
}
},
scales: {
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'PSD (Hz/s²)'
},
ticks: {
beginAtZero: true,
suggestedMax: 0.2
}
}],
xAxes: [{
display: false,
},
{
gridLines : {
display : false
},
scaleLabel: {
display: false
},
type: 'linear',
ticks: {
beginAtZero: true,
autoSkip: false,
min: 0,
max: 0.5,
stepSize: 0.1,
}
}]
},
annotation: {
drawTime: 'beforeDatasetsDraw',
annotations: [{
type: 'box',
xScaleID: 'x-axis-1',
yScaleID: 'y-axis-1',
xMin: 0.5,
xMax: 0.15,
yMin: 0,
backgroundColor: 'rgba(195,207,221, 0.5)',
borderColor: 'rgba(195,207,221, 0.5)',
}]
}
}
}

In Bubble Chart how to pass Labels [Label will be value in X Axis and not just any text] for X Axis rather than bubble chart calculate it dynamically

I have a bubble Chart and I am passing the values for plotting the charts.
The bubble Chart Automatically calculates the Labels for the X-Axis but I want to set it or hardcode it rather than bubble Chart doing it.
this.chart = new Chart(htmlRef, {
type: 'bubble',
data: {
// labels: ["0", "60", "120", "180", "240", "300"],
datasets: [
{
type:'line',
label: 'CPU Usage',
//data: [10,20],
data: [{x:0,y:5,r:5},{x:10,y:20},{x:15,y:30},{x:25,y:40},{x:55,y:30},{x:80,y:40}],
borderColor: '#4333FF',
backgroundColor: '#3398FF',
fill: true,
borderWidth: 2
}
]
},
options: {
tooltips:{
callbacks:{
label:function(tooltip){
console.log(tooltip);
return ("CPU Utilization : "+tooltip.yLabel);
}
}
},
legend: {
display: true,
position: "right"
},
scales: {
xAxes: [
{
display: true,
gridLines: {
lineWidth: 2,
drawBorder: true
]
},
scaleLabel: {
display: true,
labelString: 'Time'
}
}],
yAxes: [
{
display: true,
stacked: true,
scaleLabel: {
display: true,
labelString: 'Utilization'
},
ticks: {
min: 0,
max: 100,
// forces step size to be 5 units
stepSize: 30
}
}
]
}
}
})
I expect to set Labels myself like it should be [0,30,60,90,120 ] or if i want to change it on click of button , it should change to [0,60,120,180,240,300].
At any point in time, I can update the Labels in X-Axis.

How to Create a Custom Logarithmic Axis in Chart.js

I'm trying to refactor an old Flex app using Chart.js and I would like to replicate this linear/log axis. Notice the clean minimal spacing on the yAxis.
Here is my Chart.js code.
var mtbsoData = [];
var mtbsoLables = [];
for (let i = 0; i <= 10; i++) {
mtbsoData.push(i * i * i * i);
mtbsoLables.push(i.toString());
}
var ctxMtbso = document.getElementById("chartMtbso").getContext('2d');
var chartMtbso = new Chart(ctxMtbso, {
type: 'bar',
data: {
labels: mtbsoLables,
datasets: [{
label: 'label',
data: mtbsoData,
backgroundColor: 'rgba(0, 0, 255, .6)',
borderColor: 'rgba(0, 0, 255, 1)',
borderWidth: 1
}]
},
options: {
title: {
text: 'Title',
display: true,
fontSize: 24
},
scales: {
xAxes: [{
display: true,
ticks: {
beginAtZero: true
},
scaleLabel: {
display: true,
labelString: 'X Axis'
}
}],
yAxes: [{
type: 'logarithmic',
ticks: {
min: 0,
max: 1000,
callback: function (value, index, values) {
return value + ' years';
}
},
scaleLabel: {
display: true,
labelString: 'Y Axis'
}
}]
},
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
var label = data.datasets[tooltipItem.datasetIndex].label || '';
if (label) {
label += ': ';
}
label += tooltipItem.yLabel.toFixed(2);
return label;
}
}
},
legend: {
display: false
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="chartMtbso"></canvas>
Here's a screenshot. Notice the crowded ticks on the yAxis.
It seems from the documentation that the only ticks properties that you can apply are the "min" and "max" properties.
Can anybody suggest a solution?
I fixed it.
Modify the yAxes callback as shown:
yAxes: [{
type: 'logarithmic',
ticks: {
autoSkip: true,
min: 0,
callback: function (value, index, values) {
if( value==10 || value==100 || value==1000 || value==10000 || value==100000 || value==1000000){
return value + ' years';
}
}
},
scaleLabel: {
display: true,
labelString: 'Mean Time Between Stock-Out'
}
}]
Then you only return a tick if the value is a power of ten.

Chart.js: how to increase space between multiple Y axes on the same side?

I have a line chart with multiple Y axes on the right side, but they're too close. The options I've applied are:
{
scales: {
yAxes: [
{
id: "GC",
position: "right",
scaleLabel: {
display: true,
labelString: "Garbage Collection",
fontSize: 15
},
gridLines: {
display: false
},
ticks: {
beginAtZero: true
}
},
{
id: "CPU",
position: "right",
scaleLabel: {
display: true,
labelString: "CPU Usage",
fontSize: 15
},
gridLines: {
display: false
},
ticks: {
beginAtZero: true,
max: 100
}
}
]
}
}
The result:
How can I increase the space between multiple Y axes?