209 lines
6.4 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-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'
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,
},
useImperialUnits: {
type: Boolean,
required: true,
},
2021-09-04 19:40:27 +02:00
},
setup(props) {
const { t } = useI18n()
// 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
}
// 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,
// 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: {
drawOnChartArea: false,
2021-09-04 19:40:27 +02:00
},
},
y: {
stacked: props.displayedData !== 'average_speed',
2021-09-04 19:40:27 +02:00
grid: {
drawOnChartArea: false,
},
ticks: {
2021-09-21 18:01:36 +02:00
maxTicksLimit: 6,
callback: function (value) {
return formatTooltipValue(
props.displayedData,
+value,
props.useImperialUnits,
false
)
},
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',
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) {
return props.fullStats && context.chart.chartArea.width < 300
? false
: props.displayedData === 'average_speed'
? props.displayedSportIds.length == 1
? 'auto'
: false
: true
2021-10-03 19:23:17 +02:00
},
2021-09-20 18:23:05 +02:00
formatter: function (value, context) {
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',
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) {
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,
props.useImperialUnits
2021-09-04 21:36:59 +02:00
)
}
return label
},
footer: function (tooltipItems) {
if (props.displayedData === 'average_speed') {
return ''
}
2021-09-04 21:36:59 +02:00
let sum = 0
tooltipItems.map((tooltipItem) => {
sum += tooltipItem.parsed.y
})
return (
`${t('common.TOTAL')}: ` +
formatTooltipValue(
props.displayedData,
sum,
props.useImperialUnits
)
)
2021-09-04 21:36:59 +02:00
},
},
},
2021-09-04 19:40:27 +02:00
},
}))
const { barChartProps } = useBarChart({
chartData,
options,
})
return { barChartProps }
},
})
</script>