Skip to content

Commit

Permalink
refactor: chart date unit override (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored Oct 18, 2023
1 parent 844bac9 commit 96d1a33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions resources/assets/js/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Chart.register(...registerables);
* @param {Boolean} showCrosshair
* @param {CallableFunction|null} tooltipHandler
* @param {Boolean} hasDateTimeLabels
* @param {String|null} dateUnitOverride
* @return {Object}
*/
const CustomChart = (
Expand All @@ -39,7 +40,8 @@ const CustomChart = (
xPadding = 10,
showCrosshair = false,
tooltipHandler = null,
hasDateTimeLabels = false
hasDateTimeLabels = false,
dateUnitOverride = null
) => {
const themeMode = () => {
if (theme.mode === "auto") {
Expand Down Expand Up @@ -318,7 +320,9 @@ const CustomChart = (
if (hasDateTimeLabels) {
options.scales.xAxes.type = "time";
options.scales.xAxes.ticks.maxRotation = 0;
options.scales.xAxes.ticks.autoSkipPadding = 10;
options.scales.xAxes.ticks.autoSkipPadding = 20;
options.scales.yAxes.ticks.autoSkipPadding = 15;

options.scales.xAxes.time = {
displayFormats: {
hour: "HH:00",
Expand All @@ -327,6 +331,10 @@ const CustomChart = (
year: "yyyy",
},
};

if (dateUnitOverride) {
options.scales.xAxes.time.unit = dateUnitOverride;
}
}

this.chart = new Chart(this.getCanvasContext(), { data, options });
Expand Down
6 changes: 6 additions & 0 deletions resources/views/chart.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'showCrosshair' => false,
'tooltipHandler' => null,
'hasDateTimeLabels' => null,
'dateUnitOverride' => null,
])

<div
Expand All @@ -31,6 +32,11 @@
{{ $showCrosshair ? 'true' : 'false' }},
{{ $tooltipHandler ? $tooltipHandler : 'null' }},
{{ $hasDateTimeLabels ? 'true' : 'false' }},
@if ($dateUnitOverride)
'{{ $dateUnitOverride }}',
@else
null,
@endif
)"
wire:key="{{ $id.time() }}"
{{ $attributes->only('class') }}
Expand Down

0 comments on commit 96d1a33

Please sign in to comment.