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-26 08:59:17 +02:00
|
|
|
import { ChartData, ChartOptions, LayoutItem } from 'chart.js'
|
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-25 11:28:40 +02:00
|
|
|
import { IChartDataset } from '@/types/chart'
|
2021-09-26 08:59:17 +02:00
|
|
|
import { TStatisticsDatasetKeys } from '@/types/statistics'
|
2021-09-04 21:36:59 +02:00
|
|
|
import { formatTooltipValue } from '@/utils/tooltip'
|
2021-09-04 19:40:27 +02:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'Chart',
|
|
|
|
components: {
|
|
|
|
BarChart,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
datasets: {
|
2021-09-25 11:28:40 +02:00
|
|
|
type: Object as PropType<IChartDataset[]>,
|
2021-09-04 19:40:27 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
labels: {
|
|
|
|
type: Object as PropType<unknown[]>,
|
|
|
|
required: true,
|
|
|
|
},
|
2021-09-04 21:36:59 +02:00
|
|
|
displayedData: {
|
2021-09-26 08:59:17 +02:00
|
|
|
type: String as PropType<TStatisticsDatasetKeys>,
|
2021-09-04 21:36:59 +02:00
|
|
|
required: true,
|
|
|
|
},
|
2021-10-03 19:23:17 +02:00
|
|
|
displayedSportIds: {
|
|
|
|
type: Array as PropType<number[]>,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
fullStats: {
|
|
|
|
type: Boolean,
|
|
|
|
required: true,
|
|
|
|
},
|
2021-11-14 12:26:36 +01:00
|
|
|
useImperialUnits: {
|
|
|
|
type: Boolean,
|
|
|
|
required: true,
|
|
|
|
},
|
2021-09-04 19:40:27 +02:00
|
|
|
},
|
|
|
|
setup(props) {
|
2021-09-21 08:51:07 +02:00
|
|
|
const { t } = useI18n()
|
2021-10-02 16:16:58 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-09-20 18:23:05 +02:00
|
|
|
function getNumber(value: any): number {
|
|
|
|
return isNaN(value) ? 0 : +value
|
|
|
|
}
|
2021-10-02 16:16:58 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-09-20 18:23:05 +02:00
|
|
|
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,
|
2021-09-21 14:49:49 +02:00
|
|
|
// 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: {
|
2021-10-03 19:23:17 +02:00
|
|
|
top: props.fullStats ? 40 : 22,
|
2021-09-20 18:23:05 +02:00
|
|
|
},
|
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: {
|
2021-11-24 18:01:38 +01:00
|
|
|
stacked: props.displayedData !== 'average_speed',
|
2021-09-04 19:40:27 +02:00
|
|
|
grid: {
|
2021-09-05 09:41:39 +02:00
|
|
|
drawOnChartArea: false,
|
|
|
|
},
|
|
|
|
ticks: {
|
2021-09-21 18:01:36 +02:00
|
|
|
maxTicksLimit: 6,
|
2021-09-05 09:41:39 +02:00
|
|
|
callback: function (value) {
|
2021-11-14 12:26:36 +01:00
|
|
|
return formatTooltipValue(
|
|
|
|
props.displayedData,
|
|
|
|
+value,
|
|
|
|
props.useImperialUnits,
|
|
|
|
false
|
|
|
|
)
|
2021-09-05 09:41:39 +02:00
|
|
|
},
|
2021-09-04 19:40:27 +02:00
|
|
|
},
|
2021-09-21 18:01:36 +02:00
|
|
|
afterFit: function (scale: LayoutItem) {
|
2021-10-03 19:23:17 +02:00
|
|
|
scale.width = props.fullStats ? 75 : 60
|
2021-09-21 18:01:36 +02:00
|
|
|
},
|
2021-09-04 19:40:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
plugins: {
|
2021-09-20 18:23:05 +02:00
|
|
|
datalabels: {
|
|
|
|
anchor: 'end',
|
|
|
|
align: 'end',
|
2021-11-24 18:01:38 +01:00
|
|
|
color: function (context) {
|
|
|
|
return props.displayedData === 'average_speed' &&
|
|
|
|
context.dataset.backgroundColor
|
|
|
|
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
context.dataset.backgroundColor[0]
|
|
|
|
: '#666666'
|
|
|
|
},
|
2021-10-03 19:23:17 +02:00
|
|
|
rotation: function (context) {
|
|
|
|
return props.fullStats && context.chart.chartArea.width < 580
|
|
|
|
? 310
|
|
|
|
: 0
|
|
|
|
},
|
|
|
|
display: function (context) {
|
2021-11-24 18:01:38 +01:00
|
|
|
return props.fullStats && context.chart.chartArea.width < 300
|
|
|
|
? false
|
|
|
|
: props.displayedData === 'average_speed'
|
|
|
|
? 'auto'
|
|
|
|
: true
|
2021-10-03 19:23:17 +02:00
|
|
|
},
|
2021-09-20 18:23:05 +02:00
|
|
|
formatter: function (value, context) {
|
2021-11-24 18:01:38 +01:00
|
|
|
if (props.displayedData === 'average_speed') {
|
|
|
|
return formatTooltipValue(
|
|
|
|
props.displayedData,
|
|
|
|
value,
|
|
|
|
props.useImperialUnits,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// 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 ===
|
|
|
|
props.displayedSportIds.length - 1 && total > 0
|
|
|
|
? formatTooltipValue(
|
|
|
|
props.displayedData,
|
|
|
|
total,
|
|
|
|
props.useImperialUnits,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
}
|
2021-09-20 18:23:05 +02:00
|
|
|
},
|
|
|
|
},
|
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',
|
2021-11-24 18:01:38 +01:00
|
|
|
position:
|
|
|
|
props.displayedData === 'average_speed' ? 'nearest' : 'average',
|
2021-09-04 21:36:59 +02:00
|
|
|
},
|
|
|
|
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,
|
2021-11-14 12:26:36 +01:00
|
|
|
context.parsed.y,
|
|
|
|
props.useImperialUnits
|
2021-09-04 21:36:59 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return label
|
|
|
|
},
|
|
|
|
footer: function (tooltipItems) {
|
2021-11-24 18:01:38 +01:00
|
|
|
if (props.displayedData === 'average_speed') {
|
|
|
|
return ''
|
|
|
|
}
|
2021-09-04 21:36:59 +02:00
|
|
|
let sum = 0
|
|
|
|
tooltipItems.map((tooltipItem) => {
|
|
|
|
sum += tooltipItem.parsed.y
|
|
|
|
})
|
2021-09-21 08:51:07 +02:00
|
|
|
return (
|
2021-11-02 20:26:43 +01:00
|
|
|
`${t('common.TOTAL')}: ` +
|
2021-11-14 12:26:36 +01:00
|
|
|
formatTooltipValue(
|
|
|
|
props.displayedData,
|
|
|
|
sum,
|
|
|
|
props.useImperialUnits
|
|
|
|
)
|
2021-09-21 08:51:07 +02:00
|
|
|
)
|
2021-09-04 21:36:59 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-09-04 19:40:27 +02:00
|
|
|
},
|
|
|
|
}))
|
|
|
|
const { barChartProps } = useBarChart({
|
|
|
|
chartData,
|
|
|
|
options,
|
|
|
|
})
|
|
|
|
return { barChartProps }
|
|
|
|
},
|
|
|
|
})
|
|
|
|
</script>
|