Skip to content

Commit 6d98b52

Browse files
graphieroscoderabbitai[bot]autofix-ci[bot]
authored
feat: generate alt text in charts (#1644)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 26d967e commit 6d98b52

File tree

11 files changed

+1313
-20
lines changed

11 files changed

+1313
-20
lines changed

app/components/Package/TrendsChart.vue

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import type { VueUiXyConfig, VueUiXyDatasetItem } from 'vue-data-ui'
2+
import type { Theme as VueDataUiTheme, VueUiXyConfig, VueUiXyDatasetItem } from 'vue-data-ui'
33
import { VueUiXy } from 'vue-data-ui/vue-ui-xy'
44
import { useDebounceFn, useElementSize } from '@vueuse/core'
55
import { useCssVariables } from '~/composables/useColors'
@@ -18,6 +18,7 @@ import type {
1818
YearlyDataPoint,
1919
} from '~/types/chart'
2020
import { DATE_INPUT_MAX } from '~/utils/input'
21+
import { copyAltTextForTrendLineChart } from '~/utils/charts'
2122
2223
const props = withDefaults(
2324
defineProps<{
@@ -50,6 +51,8 @@ const props = withDefaults(
5051
5152
const { locale } = useI18n()
5253
const { accentColors, selectedAccentColor } = useAccentColor()
54+
const { copy, copied } = useClipboard()
55+
5356
const colorMode = useColorMode()
5457
const resolvedMode = shallowRef<'light' | 'dark'>('light')
5558
const rootEl = shallowRef<HTMLElement | null>(null)
@@ -1406,7 +1409,7 @@ function drawSvgPrintLegend(svg: Record<string, any>) {
14061409
// VueUiXy chart component configuration
14071410
const chartConfig = computed<VueUiXyConfig>(() => {
14081411
return {
1409-
theme: isDarkMode.value ? 'dark' : '',
1412+
theme: isDarkMode.value ? 'dark' : ('' as VueDataUiTheme),
14101413
chart: {
14111414
height: isMobile.value ? 950 : 600,
14121415
backgroundColor: colors.value.bg,
@@ -1418,14 +1421,14 @@ const chartConfig = computed<VueUiXyConfig>(() => {
14181421
fullscreen: false,
14191422
table: false,
14201423
tooltip: false,
1421-
altCopy: false, // TODO: set to true to enable the alt copy feature
1424+
altCopy: true,
14221425
},
14231426
buttonTitles: {
14241427
csv: $t('package.trends.download_file', { fileType: 'CSV' }),
14251428
img: $t('package.trends.download_file', { fileType: 'PNG' }),
14261429
svg: $t('package.trends.download_file', { fileType: 'SVG' }),
14271430
annotator: $t('package.trends.toggle_annotator'),
1428-
altCopy: undefined, // TODO: set to proper translation key
1431+
altCopy: $t('package.trends.copy_alt.button_label'), // Do not make this text dependant on the `copied` variable, since this would re-render the component, which is undesirable if the minimap was used to select a time frame.
14291432
},
14301433
callbacks: {
14311434
img: args => {
@@ -1458,10 +1461,22 @@ const chartConfig = computed<VueUiXyConfig>(() => {
14581461
loadFile(url, buildExportFilename('svg'))
14591462
URL.revokeObjectURL(url)
14601463
},
1461-
// altCopy: ({ dataset: dst, config: cfg }: { dataset: Array<VueUiXyDatasetItem>; config: VueUiXyConfig}) => {
1462-
// // TODO: implement a reusable copy-alt-text-to-clipboard feature based on the dataset & configuration
1463-
// console.log({ dst, cfg})
1464-
// }
1464+
altCopy: ({ dataset: dst, config: cfg }) =>
1465+
copyAltTextForTrendLineChart({
1466+
dataset: dst,
1467+
config: {
1468+
...cfg,
1469+
formattedDatasetValues: (dst?.lines || []).map(d =>
1470+
d.series.map(n => compactNumberFormatter.value.format(n ?? 0)),
1471+
),
1472+
hasEstimation:
1473+
supportsEstimation.value && !isEndDateOnPeriodEnd.value && !isZoomed.value,
1474+
granularity: displayedGranularity.value,
1475+
copy,
1476+
$t,
1477+
numberFormatter: compactNumberFormatter.value.format,
1478+
},
1479+
}),
14651480
},
14661481
},
14671482
grid: {
@@ -1870,7 +1885,10 @@ watch(selectedMetric, value => {
18701885
</template>
18711886
<template #optionAltCopy>
18721887
<span
1873-
class="i-lucide:person-standing w-6 h-6 text-fg-subtle"
1888+
class="w-6 h-6"
1889+
:class="
1890+
copied ? 'i-lucide:check text-accent' : 'i-lucide:person-standing text-fg-subtle'
1891+
"
18741892
style="pointer-events: none"
18751893
aria-hidden="true"
18761894
/>

0 commit comments

Comments
 (0)