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">
|
|
|
|
import { Chart, ChartData, ChartOptions, 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'
|
2021-09-21 08:51:07 +02:00
|
|
|
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) {
|
2021-09-21 08:51:07 +02:00
|
|
|
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,
|
|
|
|
datasets: props.datasets,
|
|
|
|
}))
|
|
|
|
const options = computed<ChartOptions<'bar'>>(() => ({
|
|
|
|
responsive: true,
|
|
|
|
maintainAspectRatio: true,
|
|
|
|
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: {
|
2021-09-05 09:41:39 +02:00
|
|
|
drawOnChartArea: false,
|
2021-09-04 19:40:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
stacked: true,
|
|
|
|
grid: {
|
2021-09-05 09:41:39 +02:00
|
|
|
drawOnChartArea: false,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
callback: function (value) {
|
|
|
|
return formatTooltipValue(props.displayedData, +value, false)
|
|
|
|
},
|
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) {
|
2021-09-21 08:51:07 +02:00
|
|
|
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
|
|
|
|
})
|
2021-09-21 08:51:07 +02:00
|
|
|
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>
|