diff --git a/resources/assets/js/chart.js b/resources/assets/js/chart.js index 5895883a..c7a326bd 100644 --- a/resources/assets/js/chart.js +++ b/resources/assets/js/chart.js @@ -22,6 +22,8 @@ Chart.register(...registerables); * @param {Number} yPadding * @param {Number} xPadding * @param {Boolean} showCrosshair + * @param {CallableFunction} tooltipHandler + * @param {CallableFunction} xTicksCallback * @return {Object} */ const CustomChart = ( @@ -35,7 +37,9 @@ const CustomChart = ( currency, yPadding = 15, xPadding = 10, - showCrosshair = false + showCrosshair = false, + tooltipHandler = null, + xTicksCallback = null ) => { const themeMode = () => { if (theme.mode === "auto") { @@ -246,7 +250,28 @@ const CustomChart = ( datasets: this.loadData(), }; + let tooltipOptions = { + enabled: tooltips, + external: this.tooltip, + displayColors: false, + callbacks: { + title: (items) => {}, + label: (context) => this.getCurrencyValue(context.raw), + labelTextColor: (context) => + getFontConfig("tooltip", themeMode()).fontColor, + }, + backgroundColor: getFontConfig("tooltip", themeMode()) + .backgroundColor, + }; + if (tooltipHandler) { + tooltipOptions = { + enabled: false, + external: tooltipHandler, + }; + } + const options = { + currency, spanGaps: true, normalized: true, responsive: true, @@ -259,20 +284,7 @@ const CustomChart = ( }, plugins: { legend: { display: false }, - tooltip: { - enabled: tooltips, - external: this.tooltip, - displayColors: false, - callbacks: { - title: (items) => {}, - label: (context) => - this.getCurrencyValue(context.raw), - labelTextColor: (context) => - getFontConfig("tooltip", themeMode()).fontColor, - }, - backgroundColor: getFontConfig("tooltip", themeMode()) - .backgroundColor, - }, + tooltip: tooltipOptions, }, hover: { mode: "nearest", @@ -303,6 +315,10 @@ const CustomChart = ( }, }; + if (xTicksCallback) { + options.scales.xAxes.ticks.callback = xTicksCallback; + } + this.chart = new Chart(this.getCanvasContext(), { data, options }); }, }; diff --git a/resources/views/chart.blade.php b/resources/views/chart.blade.php index 66f03b09..dd76027d 100644 --- a/resources/views/chart.blade.php +++ b/resources/views/chart.blade.php @@ -12,6 +12,8 @@ 'yPadding' => 15, 'xPadding' => 10, 'showCrosshair' => false, + 'tooltipHandler' => null, + 'ticksCallback' => null, ])