2022-06-23 11:28:31 +02:00
|
|
|
import { Controller } from '@hotwired/stimulus';
|
2022-07-04 19:16:30 +02:00
|
|
|
import Chartkick from 'chartkick';
|
2024-06-17 10:37:11 +02:00
|
|
|
import Highcharts from 'highcharts';
|
|
|
|
import invariant from 'tiny-invariant';
|
|
|
|
|
|
|
|
Chartkick.use(Highcharts);
|
|
|
|
|
|
|
|
export default class ChartkickController extends Controller {
|
|
|
|
static targets = ['chart'];
|
|
|
|
|
|
|
|
declare readonly chartTargets: HTMLElement[];
|
|
|
|
|
|
|
|
toggleChart(event: Event) {
|
|
|
|
const target = event.currentTarget as HTMLInputElement;
|
|
|
|
const chartClass = target.dataset.toggleChart;
|
|
|
|
|
|
|
|
invariant(chartClass, 'Missing data-toggle-chart attribute');
|
2022-04-15 13:05:46 +02:00
|
|
|
|
2024-06-17 10:37:11 +02:00
|
|
|
const nextChart = document.querySelector(chartClass);
|
|
|
|
const currentChart = this.chartTargets.find(
|
|
|
|
(chart) => !chart.classList.contains('hidden')
|
2022-06-23 11:28:31 +02:00
|
|
|
);
|
2022-04-15 13:05:46 +02:00
|
|
|
|
2024-06-17 10:37:11 +02:00
|
|
|
if (currentChart) {
|
|
|
|
currentChart.classList.add('hidden');
|
|
|
|
}
|
2022-07-04 19:16:30 +02:00
|
|
|
|
2024-06-17 10:37:11 +02:00
|
|
|
if (nextChart) {
|
|
|
|
nextChart.classList.remove('hidden');
|
|
|
|
const nextChartId = nextChart.children[0]?.id;
|
|
|
|
this.reflow(nextChartId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reflow(chartId: string) {
|
|
|
|
if (chartId) {
|
|
|
|
const chart = Chartkick.charts[chartId];
|
|
|
|
if (chart) {
|
|
|
|
chart.getChartObject()?.reflow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 13:05:46 +02:00
|
|
|
}
|