Skip to content

Commit 14508c1

Browse files
committed
move EnergyConsumptionChart to its own folder, fix responsive styles, add rtl support, improve type safety, and add story
1 parent 1d9db9b commit 14508c1

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

.storybook/i18next.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const ns = [
1919
"page-learn",
2020
"page-upgrades",
2121
"page-developers-index",
22+
"page-what-is-ethereum",
2223
] as const
2324
const supportedLngs = Object.keys(baseLocales)
2425

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Meta, StoryObj } from "@storybook/react"
2+
3+
import ChartComponent from "."
4+
5+
const meta = {
6+
title: "Charts / Energy Consumption Chart",
7+
component: ChartComponent,
8+
} satisfies Meta<typeof ChartComponent>
9+
10+
export default meta
11+
12+
const data = {}
13+
14+
export const Basic: StoryObj<typeof meta> = {
15+
args: {
16+
data,
17+
},
18+
}

src/components/EnergyConsumptionChart.tsx renamed to src/components/EnergyConsumptionChart/index.tsx

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CategoryScale,
55
Chart as ChartJS,
66
ChartData,
7+
ChartOptions,
78
Legend,
89
LinearScale,
910
} from "chart.js"
@@ -42,7 +43,7 @@ const EnergyConsumptionChart = () => {
4243
const isRtl = isLangRightToLeft(locale as Lang)
4344

4445
// chart rawData, according to different breakpoints
45-
const rawData = useBreakpointValue({
46+
let rawData = useBreakpointValue({
4647
base: [
4748
{
4849
name: t("energy-consumption-chart-global-data-centers-label"),
@@ -156,33 +157,34 @@ const EnergyConsumptionChart = () => {
156157
],
157158
})
158159

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+
}
164164

165165
// chart options config
166166
const chartOptions = {
167167
// chart styles
168-
barThickness: 38,
169-
borderRadius: 4,
170168
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 },
175176
plugins: {
176177
// required plugin to display labels on top
177178
datalabels: {
178179
// 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
182183
font: {
183-
size: "14px",
184-
} as any, // casted to avoid TS issue,
184+
size: 14,
185+
},
185186
color: useColorModeValue("#333333", "#F2F2F2"),
187+
textAlign: "center",
186188
},
187189
// legend styles
188190
legend: {
@@ -216,18 +218,20 @@ const EnergyConsumptionChart = () => {
216218
autoSkip: false, // avoid long labels to be hidden
217219
padding: 0, // removes default padding betwen x-labels and chart
218220
maxRotation: 0, // turns off rotation
219-
beginAtZero: true,
220221
},
221222
},
222223
},
223-
}
224+
} satisfies ChartOptions
224225

225226
const chartData = {
226227
labels: rawData?.map((item) => wrapLabel(item.name)),
227228
datasets: [
228229
{
229230
label: undefined, // don't remove, needs some value provided to render properly
230231
data: rawData?.map((item) => item.amount) || [],
232+
barThickness: 38,
233+
borderRadius: 4,
234+
backgroundColor: rawData?.map((item) => item.color),
231235
},
232236
],
233237
} satisfies ChartData
@@ -240,9 +244,9 @@ const EnergyConsumptionChart = () => {
240244
maxW="500px"
241245
m="auto"
242246
w="80vw"
247+
h={{ base: "300px", md: "400px" }}
243248
mb={{ base: 4, md: 0 }}
244249
>
245-
{/* TODO: isRtl ? data?.reverse() : data */}
246250
{isClient && (
247251
<Bar options={chartOptions} data={chartData} updateMode="none" />
248252
)}

0 commit comments

Comments
 (0)