How to display linear day ticks on x time-axe in Chartjs - chart.js

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
}
}
}

Related

Why does x axis not increment 'monthly' chart.js. Also, XAxis not taking title

https://water.sas.usace.army.mil/chart.htm
https://water.sas.usace.army.mil/chart.png
See the Code Above, (View Source)
The code builds a time-series chart.
The scales section has unit set to month, however chart does not display with monthly increment on x axis.
Also Y axis will not display title.
The main problem is that config is not properly formatted, options.plugins is not closed by a }, hence the entire scales section is not considered by Chart.js.
Further, you should also define a time.parser to let the date adapter know how to parse the provided date strings.
Be careful to choose the correct date-fns formats for time.parser as well as for time.displayFormats.
Therefore, if you define config as follows, it should work.
const config = {
type: 'line',
data: data,
options: {
responsive: true,
plugins: {
legend: {
display: true,
position: 'bottom'
},
title: {
display: true,
text: "HARTWELL PROJECT",
font: {
size: 20
}
},
},
scales: {
x: {
type: 'time',
time: {
parser: 'MM/dd/yyyy',
unit: 'month',
displayFormats: {
month: 'MMM yyyy'
}
},
title: {
display: true,
text: 'Date'
}
},
y: {
title: {
position: 'left',
display: true,
text: 'Elevation (FT-MSL)'
}
}
},
chartAreaBorder: {
borderColor: 'black',
borderWidth: 10,
//borderDash: [5, 5],
borderDashOffset: 5
},
imgPlugin: {}
},
plugins: [chartAreaBorder, imgPlugin, GradientBgPlugin]
};

Working with multiple date axes in ChartJS

I'm trying to setup a chart with 2 datasets, 1 as a line which will show a median value for a month, and 1 dataset as a scatterplot which shows the value on a certain day. The daily date doesn't need to be shown on the x axis.
I'm using dayjs as my time formatting library elsewhere within my app so I was trying to use the chartjs-adapter-dayjs-3 library.
I'm happy to manage the construction of the x,y objects myself for the data.
I've set up a codepen showing what I think should work but I'm just getting a blank chart at the moment so I'm obviously doing something wrong.
import dayjs from "https://cdn.skypack.dev/dayjs#1.10.7";
import * as chartjsAdapterDayjs from "https://cdn.skypack.dev/chartjs-adapter-dayjs#1.0.0";
const ctx = document.getElementById("myChart");
const today = dayjs()
const myChart = new Chart(ctx, {
data: {
datasets: [
{
label: "Median Sales",
data: [
{x: today, y: 10},
{x: today.add(1, 'month'), y: 20},
{x: today.add(2, 'month') , y: 15}
],
type: "line",
xAxisID: 'x1'
},
{
label: 'Individual Sales Prices',
data: [{x: today, y: 8}],
type: 'scatter',
xAxisID: 'x2'
}
]
},
options: {
scales: {
x1: {
type: 'time',
time: {
unit: 'month'
}
},
x2: {
type: 'time',
time: {
unit: 'day'
}
}
}
}
});
I'm finding it a little unclear as to how time-based data, and axes ought work at the moment so any help would be appreciated.
Desired Layout
The issue you are getting is because of 2 things. The first thing is that you are not using the date adapter your say you are using. You are using another one and importing it with skypack which has the big side effect of also import Chart.js itself. The version of Chart.js that skypack imports is version 2.
The lib that you are linking does not seem to have a build available that works out of the blue in your browser even with their example using script tags. So if you want to use day.js you will need to write your own date adapter.
Example of just using js dates and using the date-fns date adapter:
let start = new Date(),
end = new Date();
start.setDate(start.getDate() - 7); // set to 'now' minus 7 days.
start.setHours(0, 0, 0, 0); // set to midnight.
new Chart(document.getElementById("lastSevenDaysOverview"), {
type: "line",
data: {
datasets: [{
label: 'Dataset 1',
data: [{
x: new Date('10-16-2021'),
y: 1
}, {
x: new Date('10-18-2021'),
y: 4
}, {
x: new Date('10-19-2021'),
y: 66
}],
borderColor: '#ff3366',
backgroundColor: '#ff3366',
},
{
label: 'Dataset 2',
data: [{
x: new Date('10-16-2021'),
y: 31
}, {
x: new Date('10-18-2021'),
y: 14
}, {
x: new Date('10-19-2021'),
y: 6
}],
borderColor: '#880000',
backgroundColor: '#880000'
}
]
},
options: {
interaction: {
mode: 'index',
intersect: true,
},
responsive: true,
scales: {
y: {
type: 'linear',
display: true,
position: 'left',
},
x: {
type: "time",
time: {
unit: "day"
}
}
},
}
});
<canvas id="lastSevenDaysOverview"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>

chartjs : uncaught exception: 0 and X are too far apart with stepSize of Y

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',
}
}
}
}

ChartJS: Full Date for xAxis Labels?

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>

Chart with Time axis only displaying first grid line and tick label (unitStepSize)

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>