File tree 5 files changed +84
-11
lines changed
5 files changed +84
-11
lines changed Original file line number Diff line number Diff line change @@ -92,6 +92,7 @@ Need an API to fetch data? Consider [Cube](https://cube.dev/?ref=eco-vue-chartjs
92
92
93
93
- [ Reactivity] ( https://vue-chartjs.org/guide/#updating-charts )
94
94
- [ Access to Chart instance] ( https://vue-chartjs.org/guide/#access-to-chart-instance )
95
+ - [ Accessibility] ( https://vue-chartjs.org/guide/#accessibility )
95
96
- [ Migration from v4 to v5] ( https://vue-chartjs.org/migration-guides/#migration-from-v4-to-v5/ )
96
97
- [ Migration from vue-chart-3] ( https://vue-chartjs.org/migration-guides/#migration-from-vue-chart-3/ )
97
98
- [ API] ( https://vue-chartjs.org/api/ )
Original file line number Diff line number Diff line change
1
+ import { Chart as ChartJS } from 'chart.js'
1
2
import {
2
3
defineComponent ,
3
- ref ,
4
- shallowRef ,
5
4
h ,
6
- onMounted ,
5
+ nextTick ,
7
6
onBeforeUnmount ,
8
- watch ,
7
+ onMounted ,
8
+ ref ,
9
+ shallowRef ,
9
10
toRaw ,
10
- nextTick
11
+ watch
11
12
} from 'vue'
12
- import { Chart as ChartJS } from 'chart.js'
13
+
13
14
import type { ChartComponent } from './types.js'
14
15
import { Props } from './props.js'
15
16
import {
@@ -23,7 +24,7 @@ import {
23
24
24
25
export const Chart = defineComponent ( {
25
26
props : Props ,
26
- setup ( props , { expose } ) {
27
+ setup ( props , { expose, slots } ) {
27
28
const canvasRef = ref < HTMLCanvasElement | null > ( null )
28
29
const chartRef = shallowRef < ChartJS | null > ( null )
29
30
@@ -112,9 +113,16 @@ export const Chart = defineComponent({
112
113
)
113
114
114
115
return ( ) => {
115
- return h ( 'canvas' , {
116
- ref : canvasRef
117
- } )
116
+ return h (
117
+ 'canvas' ,
118
+ {
119
+ role : 'img' ,
120
+ ariaLabel : props . ariaLabel ,
121
+ ariaDescribedby : props . ariaDescribedby ,
122
+ ref : canvasRef
123
+ } ,
124
+ [ h ( 'p' , { } , [ slots . default ? slots . default ( ) : '' ] ) ]
125
+ )
118
126
}
119
127
}
120
128
} ) as ChartComponent
Original file line number Diff line number Diff line change @@ -30,10 +30,20 @@ export const CommonProps = {
30
30
}
31
31
} as const
32
32
33
+ export const A11yProps = {
34
+ ariaLabel : {
35
+ type : String
36
+ } ,
37
+ ariaDescribedby : {
38
+ type : String
39
+ }
40
+ } as const
41
+
33
42
export const Props = {
34
43
type : {
35
44
type : String as PropType < ChartType > ,
36
45
required : true
37
46
} ,
38
- ...CommonProps
47
+ ...CommonProps ,
48
+ ...A11yProps
39
49
} as const
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ Some basic props are defined in the components provided by `vue-chartjs`.
11
11
| datasetIdKey | Key name to identify the dataset |
12
12
| plugins | Plugins array that is passed into the Chart.js chart |
13
13
| updateMode | Mode string to indicate the transition configuration to be used. |
14
+ | ariaLabel | An [ ARIA label] ( https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-label ) that describes the chart to make it accessible. |
15
+ | ariaDescribedby | A reference to the [ describing element] ( https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby ) . E. g. a table representation of the data. |
14
16
15
17
The rest of the props will fall through to the canvas element.
16
18
Original file line number Diff line number Diff line change @@ -141,6 +141,58 @@ In Vue3 projects:
141
141
const chartInstance = this .$refs .bar .chart
142
142
```
143
143
144
+ ## Accessibility
145
+
146
+ To make your charts accessible to all users, you should label your charts.
147
+ Please refer also to the official [ Chart.js Accessibility notes] ( https://www.chartjs.org/docs/latest/general/accessibility.html ) .
148
+
149
+ ### ` aria-label `
150
+
151
+ You can directly label a chart by passing an ` aria-label ` prop.
152
+
153
+ ``` vue
154
+ <template>
155
+ <BarChart aria-label="Sales figures for the years 2022 to 2024. Sales in 2022: 987, Sales in 2023: 1209, Sales in 2024: 825." />
156
+ </template>
157
+ ```
158
+
159
+ ### ` aria-describedby `
160
+
161
+ You can reference to a describing element such as a table which describes the data by using the ` aria-describedby ` property.
162
+
163
+ ``` vue
164
+ <template>
165
+ <BarChart aria-describedby="my-data-table" />
166
+ <table id="my-data-table">
167
+ <caption>Sales figures for the years 2022 to 2024.</caption>
168
+ <thead>
169
+ <tr>
170
+ <th>2022</th>
171
+ <th>2023</th>
172
+ <th>2024</th>
173
+ </tr>
174
+ </thead>
175
+ <tbody>
176
+ <tr>
177
+ <td>987</td>
178
+ <td>1209</td>
179
+ <td>825</td>
180
+ </tr>
181
+ </tbody>
182
+ </table>
183
+ </template>
184
+ ```
185
+
186
+ ### Fallback-Content
187
+
188
+ In case the Browser is not able to render the ` canvas ` element, you should consider providing fallback content by using the Slot of each component.
189
+
190
+ ``` vue
191
+ <template>
192
+ <BarChart>Chart couldn't be loaded.</BarChart>
193
+ </template>
194
+ ```
195
+
144
196
## Examples
145
197
146
198
### Chart with props
You can’t perform that action at this time.
0 commit comments