Skip to content

Commit

Permalink
refactor: allow chart labels as datetime objects (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored Oct 6, 2023
1 parent 42c88b5 commit 99cfdec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
20 changes: 15 additions & 5 deletions resources/assets/js/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Chart.register(...registerables);
* @param {Number} yPadding
* @param {Number} xPadding
* @param {Boolean} showCrosshair
* @param {CallableFunction} tooltipHandler
* @param {CallableFunction} xTicksCallback
* @param {CallableFunction|null} tooltipHandler
* @param {Boolean} hasDateTimeLabels
* @return {Object}
*/
const CustomChart = (
Expand All @@ -39,7 +39,7 @@ const CustomChart = (
xPadding = 10,
showCrosshair = false,
tooltipHandler = null,
xTicksCallback = null
hasDateTimeLabels = false
) => {
const themeMode = () => {
if (theme.mode === "auto") {
Expand Down Expand Up @@ -315,8 +315,18 @@ const CustomChart = (
},
};

if (xTicksCallback) {
options.scales.xAxes.ticks.callback = xTicksCallback;
if (hasDateTimeLabels) {
options.scales.xAxes.type = "time";
options.scales.xAxes.ticks.maxRotation = 0;
options.scales.xAxes.ticks.autoSkipPadding = 10;
options.scales.xAxes.time = {
displayFormats: {
hour: "HH:00",
day: "dd.MM",
month: "MMM yyyy",
year: "yyyy",
},
};
}

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

<div
Expand All @@ -30,7 +30,7 @@
{{ $xPadding }},
{{ $showCrosshair ? 'true' : 'false' }},
{{ $tooltipHandler ? $tooltipHandler : 'null' }},
{{ $ticksCallback ? $ticksCallback : 'null' }}
{{ $hasDateTimeLabels ? 'true' : 'false' }},
)"
wire:key="{{ $id.time() }}"
{{ $attributes->only('class') }}
Expand Down

0 comments on commit 99cfdec

Please sign in to comment.