Skip to content

Commit 96d1a33

Browse files
authored
refactor: chart date unit override (#603)
1 parent 844bac9 commit 96d1a33

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

resources/assets/js/chart.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Chart.register(...registerables);
2424
* @param {Boolean} showCrosshair
2525
* @param {CallableFunction|null} tooltipHandler
2626
* @param {Boolean} hasDateTimeLabels
27+
* @param {String|null} dateUnitOverride
2728
* @return {Object}
2829
*/
2930
const CustomChart = (
@@ -39,7 +40,8 @@ const CustomChart = (
3940
xPadding = 10,
4041
showCrosshair = false,
4142
tooltipHandler = null,
42-
hasDateTimeLabels = false
43+
hasDateTimeLabels = false,
44+
dateUnitOverride = null
4345
) => {
4446
const themeMode = () => {
4547
if (theme.mode === "auto") {
@@ -318,7 +320,9 @@ const CustomChart = (
318320
if (hasDateTimeLabels) {
319321
options.scales.xAxes.type = "time";
320322
options.scales.xAxes.ticks.maxRotation = 0;
321-
options.scales.xAxes.ticks.autoSkipPadding = 10;
323+
options.scales.xAxes.ticks.autoSkipPadding = 20;
324+
options.scales.yAxes.ticks.autoSkipPadding = 15;
325+
322326
options.scales.xAxes.time = {
323327
displayFormats: {
324328
hour: "HH:00",
@@ -327,6 +331,10 @@ const CustomChart = (
327331
year: "yyyy",
328332
},
329333
};
334+
335+
if (dateUnitOverride) {
336+
options.scales.xAxes.time.unit = dateUnitOverride;
337+
}
330338
}
331339

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

resources/views/chart.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'showCrosshair' => false,
1515
'tooltipHandler' => null,
1616
'hasDateTimeLabels' => null,
17+
'dateUnitOverride' => null,
1718
])
1819

1920
<div
@@ -31,6 +32,11 @@
3132
{{ $showCrosshair ? 'true' : 'false' }},
3233
{{ $tooltipHandler ? $tooltipHandler : 'null' }},
3334
{{ $hasDateTimeLabels ? 'true' : 'false' }},
35+
@if ($dateUnitOverride)
36+
'{{ $dateUnitOverride }}',
37+
@else
38+
null,
39+
@endif
3440
)"
3541
wire:key="{{ $id.time() }}"
3642
{{ $attributes->only('class') }}

0 commit comments

Comments
 (0)