4
4
CategoryScale ,
5
5
Chart as ChartJS ,
6
6
ChartData ,
7
+ ChartOptions ,
7
8
Legend ,
8
9
LinearScale ,
9
10
} from "chart.js"
@@ -42,7 +43,7 @@ const EnergyConsumptionChart = () => {
42
43
const isRtl = isLangRightToLeft ( locale as Lang )
43
44
44
45
// chart rawData, according to different breakpoints
45
- const rawData = useBreakpointValue ( {
46
+ let rawData = useBreakpointValue ( {
46
47
base : [
47
48
{
48
49
name : t ( "energy-consumption-chart-global-data-centers-label" ) ,
@@ -156,33 +157,34 @@ const EnergyConsumptionChart = () => {
156
157
] ,
157
158
} )
158
159
159
- const aspectRatioValue = useBreakpointValue ( {
160
- base : 0.55 ,
161
- sm : 0.75 ,
162
- md : 1.1 ,
163
- } )
160
+ // reverse data if RTL
161
+ if ( isRtl ) {
162
+ rawData = rawData ?. reverse ( )
163
+ }
164
164
165
165
// chart options config
166
166
const chartOptions = {
167
167
// chart styles
168
- barThickness : 38 ,
169
- borderRadius : 4 ,
170
168
responsive : true ,
171
- aspectRatio : aspectRatioValue ,
172
- maintainAspectRatio : true ,
173
- hover : { mode : null } as any , // casted to avoid TS issue
174
- backgroundColor : rawData ?. map ( ( item ) => item . color ) as any , // casted to avoid TS issue
169
+ maintainAspectRatio : false ,
170
+ layout : {
171
+ padding : {
172
+ top : 10 ,
173
+ } ,
174
+ } ,
175
+ hover : { mode : undefined } ,
175
176
plugins : {
176
177
// required plugin to display labels on top
177
178
datalabels : {
178
179
// https://chartjs-plugin-datalabels.netlify.app/guide/positioning.html#alignment-and-offset
179
- anchor : "end" as any , // position of the labels (start, end, center), casted to avoid TS issue
180
- align : "end" as any , // alignment of the labels (start, end, center), casted to avoid TS issue
181
- offset : - 0.5 , // distance (in pixels) to pull the label away from the anchor point
180
+ anchor : "end" , // position of the labels (start, end, center)
181
+ align : "end" , // alignment of the labels (start, end, center)
182
+ offset : 0 , // distance (in pixels) to pull the label away from the anchor point
182
183
font : {
183
- size : "14px" ,
184
- } as any , // casted to avoid TS issue ,
184
+ size : 14 ,
185
+ } ,
185
186
color : useColorModeValue ( "#333333" , "#F2F2F2" ) ,
187
+ textAlign : "center" ,
186
188
} ,
187
189
// legend styles
188
190
legend : {
@@ -216,18 +218,20 @@ const EnergyConsumptionChart = () => {
216
218
autoSkip : false , // avoid long labels to be hidden
217
219
padding : 0 , // removes default padding betwen x-labels and chart
218
220
maxRotation : 0 , // turns off rotation
219
- beginAtZero : true ,
220
221
} ,
221
222
} ,
222
223
} ,
223
- }
224
+ } satisfies ChartOptions
224
225
225
226
const chartData = {
226
227
labels : rawData ?. map ( ( item ) => wrapLabel ( item . name ) ) ,
227
228
datasets : [
228
229
{
229
230
label : undefined , // don't remove, needs some value provided to render properly
230
231
data : rawData ?. map ( ( item ) => item . amount ) || [ ] ,
232
+ barThickness : 38 ,
233
+ borderRadius : 4 ,
234
+ backgroundColor : rawData ?. map ( ( item ) => item . color ) ,
231
235
} ,
232
236
] ,
233
237
} satisfies ChartData
@@ -240,9 +244,9 @@ const EnergyConsumptionChart = () => {
240
244
maxW = "500px"
241
245
m = "auto"
242
246
w = "80vw"
247
+ h = { { base : "300px" , md : "400px" } }
243
248
mb = { { base : 4 , md : 0 } }
244
249
>
245
- { /* TODO: isRtl ? data?.reverse() : data */ }
246
250
{ isClient && (
247
251
< Bar options = { chartOptions } data = { chartData } updateMode = "none" />
248
252
) }
0 commit comments