fix time in chart
This commit is contained in:
56
index.html
56
index.html
@@ -9,6 +9,8 @@
|
|||||||
<script src="js/chart.umd.min.js"></script>
|
<script src="js/chart.umd.min.js"></script>
|
||||||
<script src="js/chartjs-plugin-zoom.min.js"></script>
|
<script src="js/chartjs-plugin-zoom.min.js"></script>
|
||||||
<script src="js/hammer.min.js"></script>
|
<script src="js/hammer.min.js"></script>
|
||||||
|
<script
|
||||||
|
src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
|
||||||
<style>
|
<style>
|
||||||
*,
|
*,
|
||||||
*::before,
|
*::before,
|
||||||
@@ -1061,7 +1063,10 @@
|
|||||||
bodyFont: { family: 'JetBrains Mono', size: 11 },
|
bodyFont: { family: 'JetBrains Mono', size: 11 },
|
||||||
callbacks: {
|
callbacks: {
|
||||||
title: items => {
|
title: items => {
|
||||||
const d = new Date(items[0].label);
|
const timestamp = items[0].parsed.x;
|
||||||
|
const d = new Date(timestamp);
|
||||||
|
|
||||||
|
// Returns local date and time string
|
||||||
return d.toLocaleString();
|
return d.toLocaleString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1072,49 +1077,30 @@
|
|||||||
mode: 'x',
|
mode: 'x',
|
||||||
onZoomComplete({ chart }) {
|
onZoomComplete({ chart }) {
|
||||||
const { min, max } = chart.scales.x;
|
const { min, max } = chart.scales.x;
|
||||||
const minT = chart.data.labels[Math.round(min)]?.getTime();
|
if (minT && maxT) applyTimeFilter(Math.floor(min), Math.floor(max));
|
||||||
const maxT = chart.data.labels[Math.min(Math.round(max), chart.data.labels.length - 1)]?.getTime();
|
|
||||||
if (minT && maxT) applyTimeFilter(minT, maxT);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
scales: {
|
scales: {
|
||||||
x: {
|
x: {
|
||||||
type: 'category',
|
type: 'time', // CRITICAL: Change from 'category' to 'time'
|
||||||
|
offset: true, // This adds space at the start/end and helps bar sizing
|
||||||
stacked: true,
|
stacked: true,
|
||||||
|
time: {
|
||||||
|
// Force a unit if your data is consistent (e.g., every minute)
|
||||||
|
// unit: 'minute',
|
||||||
|
displayFormats: {
|
||||||
|
second: 'HH:mm:ss',
|
||||||
|
minute: 'HH:mm',
|
||||||
|
hour: 'HH:mm',
|
||||||
|
day: 'dd/MM HH:mm'
|
||||||
|
}
|
||||||
|
},
|
||||||
ticks: {
|
ticks: {
|
||||||
// Grouping: Limit the number of labels shown to prevent overlapping
|
|
||||||
maxTicksLimit: 8,
|
maxTicksLimit: 8,
|
||||||
// Formatting
|
color: '#555966',
|
||||||
// callback: function (val, index) {
|
font: { family: 'JetBrains Mono', size: 10 }
|
||||||
// // Assuming your data labels are Date objects or Date strings
|
|
||||||
// const date = new Date(this.getLabelForValue(val));
|
|
||||||
|
|
||||||
// // Returns "HH:mm:ss" in local time
|
|
||||||
// return date.toLocaleTimeString('sv-SE');
|
|
||||||
// }
|
|
||||||
callback: function (val, index) {
|
|
||||||
const labels = this.chart.data.labels;
|
|
||||||
const date = new Date(labels[index]);
|
|
||||||
|
|
||||||
// Check if the first and last labels are on the same day
|
|
||||||
const firstDate = new Date(labels[0]).toDateString();
|
|
||||||
const lastDate = new Date(labels[labels.length - 1]).toDateString();
|
|
||||||
const isSameDay = firstDate === lastDate;
|
|
||||||
|
|
||||||
// Format based on the result
|
|
||||||
if (isSameDay) {
|
|
||||||
// Returns: HH:mm:ss
|
|
||||||
return date.toLocaleTimeString('sv-SE');
|
|
||||||
} else {
|
|
||||||
// Returns: DD/MM HH:mm:ss
|
|
||||||
const day = String(date.getDate()).padStart(2, '0');
|
|
||||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
||||||
const time = date.toLocaleTimeString('sv-SE');
|
|
||||||
return `${day}/${month} ${time}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
grid: { color: '#ffffff08', drawBorder: false },
|
grid: { color: '#ffffff08', drawBorder: false },
|
||||||
border: { color: '#ffffff10' }
|
border: { color: '#ffffff10' }
|
||||||
|
|||||||
Reference in New Issue
Block a user