83 - Add Chart.js and register the trend chart component
This commit is contained in:
parent
5f4ef40a76
commit
7e6e6ce4e1
3 changed files with 72 additions and 2 deletions
|
|
@ -15,6 +15,7 @@
|
||||||
"vite": "^6.2.4"
|
"vite": "^6.2.4"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.8.0"
|
"axios": "^1.8.0",
|
||||||
|
"chart.js": "^4.5.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
import { Livewire } from '../../vendor/livewire/livewire/dist/livewire.esm';
|
import { Alpine, Livewire } from '../../vendor/livewire/livewire/dist/livewire.esm';
|
||||||
import './bootstrap';
|
import './bootstrap';
|
||||||
|
import trendChart from './chart';
|
||||||
|
|
||||||
|
Alpine.data('trendChart', trendChart);
|
||||||
|
|
||||||
Livewire.start();
|
Livewire.start();
|
||||||
|
|
|
||||||
66
resources/js/chart.js
Normal file
66
resources/js/chart.js
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
import {
|
||||||
|
CategoryScale,
|
||||||
|
Chart,
|
||||||
|
Filler,
|
||||||
|
Legend,
|
||||||
|
LinearScale,
|
||||||
|
LineController,
|
||||||
|
LineElement,
|
||||||
|
PointElement,
|
||||||
|
Tooltip,
|
||||||
|
} from 'chart.js';
|
||||||
|
|
||||||
|
Chart.register(
|
||||||
|
CategoryScale,
|
||||||
|
Filler,
|
||||||
|
Legend,
|
||||||
|
LinearScale,
|
||||||
|
LineController,
|
||||||
|
LineElement,
|
||||||
|
PointElement,
|
||||||
|
Tooltip,
|
||||||
|
);
|
||||||
|
|
||||||
|
export default function trendChart({ labels = [], series = [] } = {}) {
|
||||||
|
return {
|
||||||
|
chart: null,
|
||||||
|
|
||||||
|
init() {
|
||||||
|
this.chart = new Chart(this.$refs.canvas, {
|
||||||
|
type: 'line',
|
||||||
|
data: this.data(labels, series),
|
||||||
|
options: {
|
||||||
|
responsive: true,
|
||||||
|
maintainAspectRatio: false,
|
||||||
|
interaction: { mode: 'index', intersect: false },
|
||||||
|
scales: {
|
||||||
|
y: { beginAtZero: true, ticks: { precision: 0 } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
data(labels, series) {
|
||||||
|
return {
|
||||||
|
labels,
|
||||||
|
datasets: series.map((one, index) => ({
|
||||||
|
label: one.name,
|
||||||
|
data: one.values,
|
||||||
|
borderColor: this.palette(index),
|
||||||
|
backgroundColor: this.palette(index),
|
||||||
|
spanGaps: false,
|
||||||
|
tension: 0.3,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
palette(index) {
|
||||||
|
return ['#3b82f6', '#10b981'][index % 2];
|
||||||
|
},
|
||||||
|
|
||||||
|
destroy() {
|
||||||
|
this.chart?.destroy();
|
||||||
|
this.chart = null;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue