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

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

Related

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>

How can I make small grid lines above ChartJS labels?

I am using Chart.js to draw a simple line chart.
How can I make small grid lines on top of the xAxis labels?
The documentation for Line Chart is here: https://www.chartjs.org/docs/2.7.3/getting-started/?h=line, but I can't find anything about it.
I removed the XAxis and YAxis grid lines.
This is an example of the small grid lines wanted:
Thanks in advance.
Instead of removing the gridlines you can set the drawOnChartArea to false:
var 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: {
scales: {
x: {
grid: {
drawOnChartArea: false
}
},
y: {
grid: {
drawOnChartArea: false
}
}
}
},
}
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.6.2/chart.js"></script>
</body>

How to reach to a chartjs chart's value?

I have a chartjs line chart that has multiple lines like that;
I have been trying to add a date-time selector but I am not able to update my chart because I don't get how to reach the datas of my chart. Here is my chart decleration;
var ctx = document.getElementById('canvas');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: time1, time2,
datasets: [
{
label: 'Temperature - 1',
data: temp1,
backgroundColor: 'transparent',
borderColor: 'red',
borderWidth: 3
},
{
label: 'Temperature - 2',
data: temp2,
backgroundColor: 'transparent',
borderColor: 'purple',
borderWidth: 3
},
{
label: 'Humidity - 1',
data: hum1,
backgroundColor: 'transparent',
borderColor: 'green',
borderWidth: 3
},
{
label: 'Humidity - 2',
data: hum2,
backgroundColor: 'transparent',
borderColor: 'yellow',
borderWidth: 3
}]
},
options: {
elements: {
line: {
tension: 0
}
},
scales: {
y: {
title: "Temperature/Humidity",
beginAtZero: true,
min: 10,
max: 60
}
}
}
});
time1, time2, temp1, temp2, hum1, hum2 are all array. When I want to update the chart using chart.update() function that I read at its own documentation, as I said I don't get it how to reach to data arrays like temp1, temp2.
Something like myChart.data.datasets.data would not work, making them like json;
{first_data:{
label: 'Temperature - 1',
data: temp1,
backgroundColor: 'transparent',
borderColor: 'red',
borderWidth: 3
}},
Doesn't work also, how can I reach to temp1 for example?
SOLUTION:
Because of my carelessness I had asked a nonsense question. Because of myChart.data.datasets is an object, I am able to reach to temp1 like myChart.data.datasets[0].data, sorry everyone
You will need to target the specific dataset you want to get the data from so to get the data of the Temperature - 1 dataset you will need to target it like this: myChart.data.datasets[0].data
Example:
var 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: {}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
const myChart = new Chart(ctx, options);
console.log(myChart.data.datasets[0].label)
console.log(myChart.data.datasets[0].data)
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script>
</body>

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

Chart.js 3.x not able to display data on chart

I have the below chart using chart.js 3.x.
https://jsfiddle.net/7oxmesnj/1/
I have below chart configuration:
var options = {
type: 'scatter',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 0,
showLine: true,
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 0,
showLine: true,
}
]
},
options: {
scales: {
y:{
type: 'linear',
min: 0,
max: 500,
ticks: {
stepSize: 100,
reverse: false
}
},
x:{
type: 'linear',
min: 0,
max: 500,
ticks: {
stepSize: 100,
reverse: false
}
}
}
}
}
I am not able to plot the data on the scatter chart.
I have followed the migration guild, but still no data on chart.
https://www.chartjs.org/docs/master/getting-started/v3-migration
You should change datasets to have pairs of numbers (x, y). See documentation.
var options = {
type: 'scatter',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [{x:12,y:7}, {x:19,y:11}, {x:3,y:5}, {x:5,y:8}, {x:2,y:3}, {x:3,y:7}],
borderWidth: 1,
showLine: true,
pointBackgroundColor: 'red',
borderColor: 'blue',
backgroundColor: 'blue',
}]
},
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
The main problem in your code is the definition of the x-axis. The x-axis represents the labels, which are strings. Therefore, you cannot define numeric min, max and ticks.stepSize options.
In order to obtain a scatter chart with given data, you could change its type to 'line' and define showLine: false on each dataset.
Please take a look at your amended code below. I changed y
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
fill: false,
showLine: false
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
fill: false,
showLine: false
}
]
},
options: {
scales: {
y: {
min: 0,
max: 50,
ticks: {
stepSize: 10
}
}
}
}
};
new Chart('chartJSContainer', options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="chartJSContainer"></canvas>

Categories

Resources