I have a record with data from the last 36 hours. These should be displayed in my Chart.js -Chart so that there is a tick every 30 minutes. If a data set falls on xx:45, it should not be rounded to the 30-minute interval.
I have the feeling that I have not yet understood the terms ticks and units correctly. I want to have vertical lines every 30 minutes, but the data sets should be freely distributed on the x-axis.
{
...
type: "time",
time: {
unit: "minute",
displayFormats: {
minute: "HH:mm"
},
stepSize: 30
},
gridLines: {
drawOnChartArea: true,
color: ["transparent", ...Array(Math.max(0, countDays(bedCapacities) - 2)).fill("#DFF2FA"), "#303740"],
zeroLineColor: "#DFF2FA",
drawBorder: true,
tickMarkLength: -20
},
ticks: {
fontColor: "#CCCCCC",
fontSize: 11,
fontStyle: "bold",
labelOffset: 20,
source: "auto"
}
This is my setting, using v3.6.0.
You can ignore y axis, just let you know mine works.
scales: {
x: {
type: 'time',
time: {
unit: 'minute',
stepSize: 30
},
min: tradingDuration().openTime,
max: tradingDuration().closeTime
// title: {
// display: true,
// text: 'Date'
// }
},
y: {
min: limitDown,
max: limitUp
}
},
Related
I have dataset width random time moments and I'd like to display round dates on x axe like
| | | | |
12 13 14 15 16
oct oct oct oct oct
Looks like time.round iss the proper option, but it not works, my code
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: chart_labels,
datasets: [{
label: 'Время доступа',
data: chart_values,
borderColor: ["#03A9F5"],
borderWidth: 1,
fill: true,
backgroundColor: "#03A9F522",
pointRadius: 1,
pointHoverRadius: 5,
}]
},
options: {
interaction: {
intersect: false,
mode: 'index',
},
tension: 0.4,
scales: {
x: {
type: 'time',
'time.round': 'day',
},
y: {
title: {
display: true,
stacked: true,
text: 'Секунды',
beginAtZero: true
}
}
},
}
});
And ticks go by 3 hours: 5pm 8pm 11pm...
Edited
time: {round: 'day'} is not a correct parameter, it moves dots to the beginning of a day, when I need to keep dots where they are but draw ticks on the begining of the days.
https://jsfiddle.net/7y2L9ueq/
I think the issue is that you are using round option in the time.
For what you need, you should use unit option, which is defining the time unit on the axis.
scales: {
x: {
type: 'time',
time: {
unit: 'day' // <-- to use
}
},
y: {
title: {
display: true,
stacked: true,
text: 'Секунды',
beginAtZero: true
}
}
}
I am plotting data on a graph with chartjs. It used to work but I don't know why I am continuously getting uncaught exception: 0 and 1587533402000 are too far apart with stepSize of 1 hour, although neither 0 nor 1587533402000 are part of the data I plot.
Here is how I plot the graph :
var chart_temperature = new Chart(ctx_temperature, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: timeXValues,
fill: false, // no inner color
datasets: [{
label: 'Temperature',
borderColor: 'rgb(255, 99, 132)',
data: temperatureData
}]
},
// Configuration options go here
options: {
responsive: true,
layout: {
padding: {
bottom: 50
}
},
elements: {
point: {
radius: 0 // don't show points
},
line: {
fill: false
}
},
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'hour',
displayFormats: {
hour: 'DD/MM/YYYY HH:mm'
}
},
ticks: {
beginAtZero: false // tried true, and also removed all this as it used to be
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'T°C'
}
}]
},
showLines: true, // the points will be connected
// Optimization
animation: {
duration: 0 // general animation time
},
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0 // animation duration after a resize
}
});
Why is chartjs using 0 whereas the chart is not starting at 0 ? Where should I look at ?
Any help appreciated :-)
Edit :
Commenting the following line (in scales.xAxes) makes the chart displayed :
// type: 'time',
But the X Axis becomes then useless since timestamps are displayed.
A genius idea finally spurted! Searching against are too far apart with stepSize in chartjs git repository showed that time scale min option was wrongly set to 0.
Adding these min max to time option of xAxes solved the issue.
And even as time min max options are deprecated, ticks.min and ticks.max should be used:
ticks: {
min: startTimestamp,
max: endTimestamp
}
For me, I needed to update my type time to "minute" instead of "second".
xAxes: [ {
type: 'time',
time: {
unit: 'minute'
},
In my case, that because the data from x is not 'int' but 'string'.
data from Backend:
data: [{
x: "1626414792000", //1626414792000
y: 2
}, {
x: 1626414873000, // 14:00:00
y: 3
}, {
x: 1626415500000, // 13:00:00
y: 5
}]
}],
Then, I parse x data before chart.js use it.
My full code:
var obj = {
type: 'line',
data: {
datasets: [{
label: 'First dataset',
data: [{
x: "1626414792000",
y: 2
}, {
x: 1626414873000,
y: 3
}, {
x: 1626415500000,
y: 5
}]
}],
},
options: {
title: {
text: 'Chart',
display: true,
},
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'minute',
/* parsing x data before chart.js use it */
parser: function(dt){
console.log(dt)
return parseInt(dt);
}
}
}]
}
}
}
In my case, the issue happened when zooming.
It was caused by wrong properties of the zoom plugin option used with a chart type 'time'. The following works options works fine:
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
suggestedMin: 0,
},
}],
xAxes: [{
type: 'time',
time: {
unit: 'day',
tooltipFormat: 'MMM DD YYYY',
},
}],
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'xy'
},
zoom: {
enabled: true,
mode: 'xy',
}
}
}
}
I was been working on a timescale line chart, and noticed that when the all the data points are close enough together, the xAxis labels automatically convert to just time. I have tried different xAxis time units with no success.
Here is a simply codepen example in which the xAxis labels are by hour. If you change a moment to be March instead of April, the xAxis labels are by date.
Is there a way to force the xAxis labels to always be date AND time?
xAxes: [{
type: 'time',
time: {
parser: timeFormat,
// unit: 'day',
// unit: 'hour',
tooltipFormat: 'll HH:mm'
},
https://codepen.io/ccwakes/pen/abvzewW
Thanks
You can use property time.displayFormats according to Display Formats from Chart.js documentation. See Moment.js for the allowable format strings.
time: {
unit: 'hour',
displayFormats: {
hour: 'MMM DD HH:mm'
},
tooltipFormat: 'MMM DD HH:mm'
}
Please have a look at your amended code below.
new Chart(document.getElementById('canvas'), {
type: 'line',
data: {
datasets: [{
label: 'Dataset 1',
backgroundColor: 'red',
borderColor: 'red',
fill: false,
data: [
{ x: '2020-04-10 13:00', y: 60 },
{ x: '2020-04-12 06:00', y: 100 },
{ x: '2020-04-12 13:00', y: 5 }
],
}, {
label: 'Dataset 2',
backgroundColor: 'blue',
borderColor: 'blue',
fill: false,
data: [
{ x: '2020-04-10 13:00', y: 45 },
{ x: '2020-04-11 13:00', y: 65 },
{ x: '2020-04-12 06:00', y: 80 },
{ x: '2020-04-12 13:00', y: 65 }
]
}]
},
options: {
title: {
text: 'Time Scale'
},
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'hour',
displayFormats: {
hour: 'MMM DD HH:mm'
},
tooltipFormat: 'MMM DD HH:mm'
},
scaleLabel: {
display: true,
labelString: 'Date'
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Value'
}
}]
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="canvas" height="90"></canvas>
I am trying to display a simple line chart of temperatures versus time. The dataset has temperatures and times in ten minute intervals. Times are in HH:mm format.
The graph is displaying correctly but only the left axis, right axis and first tick time value and grid line are displaying.
My data looks like this:
12:00 20.1
12:10 20.3
12:20 20.5
...
13:20 21
13:30 21.4
I get the left axis labelled as 12:00 then one label at 12:10 with grid line, and then nothing until 13:30 on the right axis.
If I leave out the unitStepSize I get ticks and gridlines every minute (crowded). So obviously I am missing something to do with this parameter.
var myChart = new Chart(ctx,
{
type: 'line',
data: data,
options :
{
responsive:false,
maintainAspectRatio: false,
scales:
{
xAxes: [
{
type: 'time',
scaleLabel:
{
display: true,
labelString: 'Time'
},
time:
{
unit: 'minute',
unitStepSize: '10',
format: "HH:mm",
displayFormats:
{
minute: 'HH:mm',
hour: 'HH:mm'
}
}
}],
yAxes: [
{
scaleLabel:
{
display: true,
labelString: 'Temp'
},
ticks: {
max: 25,
min: 15,
stepSize: 1
}
}]
}
}
});
The issue you are currently facing is causing because, you are passing the unitStepSize value as a string.
It should be a number, with no quotes ('') around it.
var ctx = document.querySelector('#canvas').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['12:00', '12:10', '12:20', '13:20', '13:30'],
datasets: [{
label: 'Temperatures',
data: [20, 21, 22, 21, 23],
backgroundColor: 'rgba(75,192,192, 0.4)',
borderColor: '#4bc0c0',
pointBackgroundColor: 'black',
tension: 0,
fill: false
}]
},
options: {
scales: {
xAxes: [{
type: 'time',
scaleLabel: {
display: true,
labelString: 'Time'
},
time: {
unit: 'minute',
unitStepSize: 10,
format: "HH:mm",
displayFormats: {
minute: 'HH:mm',
hour: 'HH:mm'
}
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Temp'
},
ticks: {
max: 25,
min: 15,
stepSize: 1
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>
Using v2.0beta of Chart.js
I'm having trouble displaying the xAxes and yAxes scale labels, as well as setting the yAxes min/max.
I need the yAxes ticks to range from -10 to 120 in increments of 10....this was super easy to do in v1 but I had to switch to v2.0 in order to invert the yAxes...help would be much appreciated :)
Tried setting it up in a jsfiddle but couldn't get it to work, sorry.
Here's my Line object:
var chart = new Chart(context, {
type: 'line',
data: data,
options: {
scales: {
xAxes: [
{
position: "top",
scaleLabel: {
display: true,
labelString: "Frequency (Hz)",
fontFamily: "Montserrat",
fontColor: "black",
},
ticks: {
fontFamily: "Montserrat",
}
}
],
yAxes: [
{
position: "left",
scaleLabel: {
display: true,
labelString: "dB",
fontFamily: "Montserrat",
fontColor: "black",
},
ticks: {
fontFamily: "Montserrat",
reverse : true,
min: -10,
max: 120,
},
}
],
},
}
});
A bit late answer but Chart.js v2.0beta doesn't seem to support those tick options well.
I updated your fiddle. I updated Chart.js v2.0beta to Chart.js v2.5, which is the latest Chart.js for now.
Your tick options should look like
ticks: {
min: -10,
max: 110,
stepSize: 10,
reverse: true,
},
I know you were asking for set boundaries, but with my chart I do not know what the scale is so I look at the data and get the Hi/Low and adjust the chart to match the data.
var hi = data.datasets[3].data[0];
var lo = data.datasets[1].data[data.datasets[1].data.length - 1];
ticks: {
max: hi,
min: lo,
stepSize: 10
}
}
Hope this helps someone.