Chart.js hidden property - javascript

I'm new to programming and I'm trying to make a custom legend for my chart, how can I use the "hidden" property for when I click on it, it will be stroke,
when I do "hidden: true" they all get strikethrough, not only the one I clicked
Ps: I'm using 2.9.4 version of chart.js
Here is my code
legend: {
display: true,
labels: {
generateLabels: (chart) =>
chart.data.datasets.map((dataset, i) => ({
datasetIndex: i,
text: dataset.label,
fillStyle: dataset.backgroundColor,
strokeStyle: dataset.borderColor
})),
fontSize: 10,
boxWidth: 10,
},
}

You can use the isDatasetVisible method on the chart object with the current dataset index to check if it is hidden. This returns a boolean so you can put it in there right away after inversing it.
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
legend: {
labels: {
generateLabels: (chart) =>
chart.data.datasets.map((dataset, i) => ({
datasetIndex: i,
text: dataset.label,
fillStyle: dataset.backgroundColor,
strokeStyle: dataset.borderColor,
hidden: !chart.isDatasetVisible(i),
})),
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
</body>

Related

circular grid radar and scale max/min not working - chartjs

I am trying to set min/max starting values to scale and circular grid to a radar in chartjs, but none seens to work.
const chart1 = new Chart($('#chart-area1'), {
type: 'radar',
data: {
labels: ['test1', 'test2', 'test3'],
datasets: [{
label: '1',
data: [20, 25, 55],
borderColor: setup_borderColor[0],
backgroundColor: setup_backgroundColor[0],
borderWidth: 1
},{
label: '2',
data: [25, 35, 40],
borderColor: setup_borderColor[1],
backgroundColor: setup_backgroundColor[1],
borderWidth: 1
}]
},
options: {
scale: {
min: 0,
max: 100,
},
scales: {
r: {
grid: {
circular: true
}
}
}
}
});
The radar chartjs should start at 0 with a max value at 100 and be a circular grid, but both options dont work, my result:
Result:
Chartjs version: Chart.js v2.9.3
You are mixing V2 and V3 syntax in all the wrong ways, in V2 you need to use the scale option not scales.r and the min/max have to be configured in the ticks part not in the root and grid must be gridLines:
var options = {
type: 'radar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'orange'
}
]
},
options: {
scale: {
ticks: {
max: 40,
min: 0
},
gridLines: {
circular: true
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
</body>
For more info you can read the 2.9.4 documentation

Is there any way to hide default yAxes line and only show the values on yAxes in react-chartjs-2?

I have a little experience with chartjs but I am unable to find a way to hide the default line. I'm adding this image here which I need to fix.
I need to hide the line like this one
https://i.stack.imgur.com/UXMpi.png.
I need to make it like this one
https://i.stack.imgur.com/Phsos.png
If anyone knows how to do this, please let me know. Thanks.
set display to false in the grid settings of the x axis and drawBorder to false in both axes like so:
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'orange'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'pink'
}
]
},
options: {
scales: {
x: {
grid: {
display: false,
drawBorder: false
}
},
y: {
grid: {
drawBorder: false
}
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
</body>

remove undefined label in the upper part of a chart

I am using Chart.js library in my project and I have one issue that I could't solve.
This label that describes what the data shown is about, is one info that I don't actually need in my project. How can I remove it completely?
In another chart I need to place it in the right side of the chart, but I can't manage it. How can I solve these two problems that I have?
At the moment my code looks like this:
type: 'bar',
data: {
labels: data.map(x => moment(x.date).format("MMM Do")),
datasets: [{
data: data.map(x => x.premium),
backgroundColor: '#ffec87',
borderColor: "#ffec87",
borderWidth: 2,
borderStyle: 'dotted'
}]
},
Any ideas?
Thanks in advance!
Since its part of the legend you will need to configure it in the options.
Don't show at all example:
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'orange'
}
]
},
options: {
plugins: {
legend: {
display: false
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
</body>
Show it on the right side example:
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'orange'
}
]
},
options: {
plugins: {
legend: {
position: 'right'
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
</body>
Dont show undefined labels:
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
},
{
data: [7, 11, 5, 8, 3, 7],
borderColor: 'orange'
}
]
},
options: {
plugins: {
legend: {
labels: {
filter: (legendItem, data) => (typeof legendItem.text !== 'undefined')
}
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
</body>
For more information you can read the documentation about it
I agree with LeeLenalee answer but I think you need this for aligning:
plugins: {
legend: {
align: 'end',
}
},

Legend for only specific datasets - chart.js

I would like to only have a legend label for only two of my three datasets in my chart.js stacked bar chart.
Is there a way to do this? At the moment I am only able to hide the entire legend by putting the following in my chart options:
legend: {
display: false
}
I.e. would it be possible to specify which datasets have a legend and which don't in the options? (or in the datasets if that's possible).
You can use the filter on the labels of the legend to filter out the datasets you dont want to show in the legend, see example:
var options = {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
backgroundColor: 'red'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1,
backgroundColor: 'blue'
},
{
label: 'dont show this',
data: [17, 1, 15, 18, 13, 17],
borderWidth: 1,
backgroundColor: 'orange'
}
]
},
options: {
plugins: {
legend: {
labels: {
filter: (legendItem, chartData) => (legendItem.text !== 'dont show this')
}
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.js"></script>
</body>
Documentation: https://www.chartjs.org/docs/master/configuration/legend.html#legend-label-configuration

Render labels only when the data is available for a particular label

I'm creating an stack bar chart using chart.js. I need to hide the labels which don't have data on the current chart. As an example I want to only show "Prediction Success" label as data related to other labels are not there for the current date range. How can we do that?
You can use the legend filter like this:
var options = {
type: 'bar',
data: {
labels: ["Red", "Yellow", "Blue", "Yellow", "Green", "Purple", "Orange", "Green"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 0, 5, 6],
borderWidth: 1,
backgroundColor: 'red'
},
{
label: '# of NonVotes',
data: [],
borderWidth: 1,
backgroundColor: 'blue'
}
]
},
options: {
plugins: {
legend: {
labels: {
filter: (legendItem, chartData) => (legendItem, chartData, chartData.datasets[legendItem.datasetIndex].data.length > 0)
}
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.0/chart.js" integrity="sha512-LlFvdZpYhQdASf4aZfSpmyHD6+waYVfJRwfJrBgki7/Uh+TXMLFYcKMRim65+o3lFsfk20vrK9sJDute7BUAUw==" crossorigin="anonymous"></script>
</body>

Categories

Resources