154 lines
4.3 KiB
Vue
Raw Normal View History

2021-09-04 19:40:27 +02:00
<template>
<div class="chart">
2021-09-20 18:23:05 +02:00
<BarChart v-bind="barChartProps" class="bar-chart" />
2021-09-04 19:40:27 +02:00
</div>
</template>
<script lang="ts">
2021-09-21 18:01:36 +02:00
import {
Chart,
ChartData,
ChartOptions,
LayoutItem,
registerables,
} from 'chart.js'
2021-09-20 18:23:05 +02:00
import ChartDataLabels from 'chartjs-plugin-datalabels'
2021-09-04 19:40:27 +02:00
import { ComputedRef, PropType, computed, defineComponent } from 'vue'
import { BarChart, useBarChart } from 'vue-chart-3'
import { useI18n } from 'vue-i18n'
2021-09-04 19:40:27 +02:00
2021-09-04 21:36:59 +02:00
import { IStatisticsChartDataset, TDatasetKeys } from '@/types/statistics'
import { formatTooltipValue } from '@/utils/tooltip'
2021-09-04 19:40:27 +02:00
Chart.register(...registerables)
2021-09-20 18:23:05 +02:00
Chart.register(ChartDataLabels)
2021-09-04 19:40:27 +02:00
export default defineComponent({
name: 'Chart',
components: {
BarChart,
},
props: {
datasets: {
type: Object as PropType<IStatisticsChartDataset[]>,
required: true,
},
labels: {
type: Object as PropType<unknown[]>,
required: true,
},
2021-09-04 21:36:59 +02:00
displayedData: {
type: String as PropType<TDatasetKeys>,
required: true,
},
2021-09-04 19:40:27 +02:00
},
setup(props) {
const { t } = useI18n()
2021-09-20 18:23:05 +02:00
// eslint-disable-next-line
function getNumber(value: any): number {
return isNaN(value) ? 0 : +value
}
// eslint-disable-next-line
function getSum(total: any, value: any): number {
return getNumber(total) + getNumber(value)
}
2021-09-04 19:40:27 +02:00
let chartData: ComputedRef<ChartData<'bar'>> = computed(() => ({
labels: props.labels,
// workaround to avoid dataset modification
datasets: JSON.parse(JSON.stringify(props.datasets)),
2021-09-04 19:40:27 +02:00
}))
const options = computed<ChartOptions<'bar'>>(() => ({
responsive: true,
maintainAspectRatio: true,
2021-09-21 18:01:36 +02:00
animation: false,
2021-09-04 19:40:27 +02:00
layout: {
2021-09-20 18:23:05 +02:00
padding: {
top: 22,
},
2021-09-04 19:40:27 +02:00
},
scales: {
x: {
stacked: true,
grid: {
drawOnChartArea: false,
2021-09-04 19:40:27 +02:00
},
},
y: {
stacked: true,
grid: {
drawOnChartArea: false,
},
ticks: {
2021-09-21 18:01:36 +02:00
maxTicksLimit: 6,
callback: function (value) {
return formatTooltipValue(props.displayedData, +value, false)
},
2021-09-04 19:40:27 +02:00
},
2021-09-21 18:01:36 +02:00
afterFit: function (scale: LayoutItem) {
scale.width = 60
},
2021-09-04 19:40:27 +02:00
},
},
plugins: {
2021-09-20 18:23:05 +02:00
datalabels: {
anchor: 'end',
align: 'end',
formatter: function (value, context) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const total: number = context.chart.data.datasets
.map((d) => d.data[context.dataIndex])
.reduce((total, value) => getSum(total, value), 0)
return context.datasetIndex === 5 && total > 0
? formatTooltipValue(props.displayedData, total, false)
: null
},
},
2021-09-04 19:40:27 +02:00
legend: {
display: false,
},
2021-09-04 21:36:59 +02:00
tooltip: {
interaction: {
intersect: true,
mode: 'index',
},
filter: function (tooltipItem) {
return tooltipItem.formattedValue !== '0'
},
callbacks: {
label: function (context) {
let label = t(`sports.${context.dataset.label}.LABEL`) || ''
2021-09-04 21:36:59 +02:00
if (label) {
label += ': '
}
if (context.parsed.y !== null) {
label += formatTooltipValue(
props.displayedData,
context.parsed.y
)
}
return label
},
footer: function (tooltipItems) {
let sum = 0
tooltipItems.map((tooltipItem) => {
sum += tooltipItem.parsed.y
})
return (
`${t('statistics.TOTAL')}: ` +
formatTooltipValue(props.displayedData, sum)
)
2021-09-04 21:36:59 +02:00
},
},
},
2021-09-04 19:40:27 +02:00
},
}))
const { barChartProps } = useBarChart({
chartData,
options,
})
return { barChartProps }
},
})
</script>