Skip to content

Commit 99cfdec

Browse files
authored
refactor: allow chart labels as datetime objects (#599)
1 parent 42c88b5 commit 99cfdec

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

resources/assets/js/chart.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Chart.register(...registerables);
2222
* @param {Number} yPadding
2323
* @param {Number} xPadding
2424
* @param {Boolean} showCrosshair
25-
* @param {CallableFunction} tooltipHandler
26-
* @param {CallableFunction} xTicksCallback
25+
* @param {CallableFunction|null} tooltipHandler
26+
* @param {Boolean} hasDateTimeLabels
2727
* @return {Object}
2828
*/
2929
const CustomChart = (
@@ -39,7 +39,7 @@ const CustomChart = (
3939
xPadding = 10,
4040
showCrosshair = false,
4141
tooltipHandler = null,
42-
xTicksCallback = null
42+
hasDateTimeLabels = false
4343
) => {
4444
const themeMode = () => {
4545
if (theme.mode === "auto") {
@@ -315,8 +315,18 @@ const CustomChart = (
315315
},
316316
};
317317

318-
if (xTicksCallback) {
319-
options.scales.xAxes.ticks.callback = xTicksCallback;
318+
if (hasDateTimeLabels) {
319+
options.scales.xAxes.type = "time";
320+
options.scales.xAxes.ticks.maxRotation = 0;
321+
options.scales.xAxes.ticks.autoSkipPadding = 10;
322+
options.scales.xAxes.time = {
323+
displayFormats: {
324+
hour: "HH:00",
325+
day: "dd.MM",
326+
month: "MMM yyyy",
327+
year: "yyyy",
328+
},
329+
};
320330
}
321331

322332
this.chart = new Chart(this.getCanvasContext(), { data, options });

resources/views/chart.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
'xPadding' => 10,
1414
'showCrosshair' => false,
1515
'tooltipHandler' => null,
16-
'ticksCallback' => null,
16+
'hasDateTimeLabels' => null,
1717
])
1818

1919
<div
@@ -30,7 +30,7 @@
3030
{{ $xPadding }},
3131
{{ $showCrosshair ? 'true' : 'false' }},
3232
{{ $tooltipHandler ? $tooltipHandler : 'null' }},
33-
{{ $ticksCallback ? $ticksCallback : 'null' }}
33+
{{ $hasDateTimeLabels ? 'true' : 'false' }},
3434
)"
3535
wire:key="{{ $id.time() }}"
3636
{{ $attributes->only('class') }}

0 commit comments

Comments
 (0)