add analytics in assets

This commit is contained in:
Simon Vieille 2022-02-20 21:08:32 +01:00
parent 690e117b0b
commit b32509401c
2 changed files with 54 additions and 0 deletions

View file

@ -23,3 +23,4 @@ require('./modules/sortable.js')()
require('./modules/batch.js')()
require('./modules/file-manager.js')()
require('./modules/file-picker.js')()
require('./modules/analytics.js')()

View file

@ -0,0 +1,53 @@
const $ = require('jquery')
const Chart = require('chart.js/auto').default
const drawChart = () => {
const ctx = document.getElementById('analytic-chart')
const options = {
type: 'bar',
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true
}
}
},
data: {
labels: JSON.parse(ctx.getAttribute('data-labels')),
datasets: [{
label: ctx.getAttribute('data-label'),
data: JSON.parse(ctx.getAttribute('data-values')),
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgb(54, 162, 235)',
borderWidth: 1
}]
}
}
const chart = new Chart(ctx, options)
const resize = () => {
const width = ctx.parentNode.parentNode.offsetWidth
const height = 250
chart.resize(width, height)
}
resize()
window.addEventListener('resize', resize)
}
module.exports = () => {
const body = $('body')
body.on('shown.bs.modal', '.modal', (e) => {
window.setTimeout(() => {
if (document.getElementById('analytic-chart')) {
drawChart()
}
}, 500)
})
}