-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathAreaChart.stories.tsx
More file actions
392 lines (335 loc) · 11 KB
/
Copy pathAreaChart.stories.tsx
File metadata and controls
392 lines (335 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import React from "react";
import type { Meta, StoryObj } from "@storybook/react";
import { AreaChart } from "components";
import { CustomTooltipProps } from "components/chart-elements/common/CustomTooltipProps";
import { Color, currencyValueFormatter } from "lib";
import {
simpleBaseChartData as data,
simpleBaseChartDataWithNulls,
singleAndMultipleData,
longBaseChartData,
longIndexBaseChartData,
simpleBaseChartWithNegativeValues,
} from "./helpers/testData";
import { valueFormatter } from "./helpers/utils";
const meta: Meta<typeof AreaChart> = {
title: "Visualizations/Chart/AreaChart",
component: AreaChart,
args: { categories: ["Sales", "Successful Payments"], index: "month", data, className: "h-72" },
parameters: {
sourceLink:
"https://github.com/tremorlabs/tremor/tree/main/src/components/chart-elements/AreaChart",
},
};
export default meta;
type Story = StoryObj<typeof AreaChart>;
export const Default: Story = {
args: {},
};
export const DefaultNegativeValues: Story = {
args: {
data: simpleBaseChartWithNegativeValues,
},
};
export const Stacked: Story = {
args: {
stack: true,
},
};
export const ValueFormatter: Story = {
args: { valueFormatter: valueFormatter, yAxisWidth: 60 },
};
export const AutoMinValue: Story = {
args: { autoMinValue: true },
};
export const MinValueAndMaxValue: Story = {
args: { minValue: -1000, maxValue: 4000 },
};
export const OtherColors: Story = {
args: { colors: ["rose", "purple"] },
};
export const CustomColors: Story = {
args: {
colors: ["#32a852", "orange-600"],
},
};
export const NoGradient: Story = {
args: { showGradient: false },
};
export const ChangedCategoriesOrder: Story = {
args: { categories: ["Successful Payments", "Sales"] },
};
export const LongValues: Story = {
args: { categories: ["This is an edge case"] },
};
export const MultipleCategories: Story = {
args: {
categories: ["Sales", "Successful Payments"],
yAxisWidth: 110,
},
};
export const NoData: Story = {
args: { data: [] },
};
export const NoDataText: Story = {
args: { data: [], noDataText: "No data, try again later." },
};
export const NoCategories: Story = {
args: { categories: undefined },
};
export const NoIndex: Story = {
args: { index: undefined },
};
export const CurveTypeNatural: Story = {
args: { curveType: "natural" },
};
export const ConnectNullsTrue: Story = {
args: { data: simpleBaseChartDataWithNulls, connectNulls: true },
};
export const ConnectNullsFalse: Story = {
args: { data: simpleBaseChartDataWithNulls, connectNulls: false },
};
export const Animation: Story = {
args: { showAnimation: true },
};
export const LongAnimationDuration: Story = {
args: { showAnimation: true, animationDuration: 5000 },
};
export const OnValueChange: Story = {
args: { onValueChange: (v: any) => alert(JSON.stringify(v)) },
};
export const OneDataValue: Story = {
args: { data: data.slice(0, 1) },
};
export const OneDataValueAndOnValueChange: Story = {
args: { data: data.slice(0, 1), onValueChange: (v: any) => alert(JSON.stringify(v)) },
};
export const SingleAndMultipleData: Story = {
args: { data: singleAndMultipleData },
};
export const SingleAndMultipleDataAndOnValueChange: Story = {
args: { data: singleAndMultipleData, onValueChange: (v: any) => alert(JSON.stringify(v)) },
};
export const PreserveStartEnd: Story = {
args: { intervalType: "preserveStartEnd" },
};
export const LongDataInput: Story = {
args: { data: longBaseChartData },
};
export const LongDataInputAndPreserveStartEnd: Story = {
args: { data: longBaseChartData, intervalType: "preserveStartEnd" },
};
export const LongIndexName: Story = {
args: { data: longIndexBaseChartData },
};
export const LongIndexNameAndPreserveStartEnd: Story = {
args: { data: longIndexBaseChartData, intervalType: "preserveStartEnd" },
};
export const MultipleZeroValues: Story = {
args: {
data: [
{
month: "May 21",
Sales: 2390,
"Successful Payments": 0,
},
{
month: "Jun 21",
Sales: 2390,
"Successful Payments": 0,
},
{
month: "Jul 21",
Sales: 3490,
"Successful Payments": 0,
},
],
},
};
export const RotateXLabels: Story = {
args: {
data: longBaseChartData,
rotateLabelX: { angle: -45, verticalShift: 15, xAxisHeight: 50 },
},
};
export const LegendSlider: Story = {
args: {
enableLegendSlider: true,
categories: ["Sales", "Successful Payments", "Test"],
},
};
export const NoAxes: Story = {
args: {
showXAxis: false,
showYAxis: false,
},
};
export const NoYAxisStartEndOnly: Story = {
args: { showYAxis: false, startEndOnly: true },
};
//Custom tooltips
const customTooltipColors: Color[] = ["cyan"];
const customTooltipIndex = "month";
const getBadgeColor = (percentage: number | undefined) => {
if (!percentage || percentage === 0) return "gray";
else if (percentage > 0) return "emerald";
else return "red";
};
export const CustomTooltipSimple: Story = {
args: {
yAxisWidth: 65,
index: customTooltipIndex,
categories: ["Sales"],
colors: customTooltipColors,
valueFormatter: currencyValueFormatter,
customTooltip: (props: CustomTooltipProps) => {
const { payload, active, label } = props;
if (!active || !payload) return null;
const categoryPayload = payload?.[0];
if (!categoryPayload) return null;
return (
<div className="w-56 rounded-tremor-default text-tremor-default bg-tremor-background p-2 shadow-tremor-dropdown border border-tremor-border">
<div className="flex flex-1 space-x-2.5">
<div className={`w-1.5 flex flex-col bg-${categoryPayload.color}-500 rounded`} />
<div className="w-full">
<p className="font-medium text-tremor-content-emphasis">{label}</p>
<div className="flex items-center justify-between space-x-8">
<p className="text-right text-tremor-content whitespace-nowrap">
{categoryPayload.dataKey}
</p>
<p className="font-medium text-right whitespace-nowrap text-tremor-content-emphasis">
{currencyValueFormatter(categoryPayload.value as number)}
</p>
</div>
</div>
</div>
</div>
);
},
},
};
export const CustomTooltipPreviousDay: Story = {
args: {
yAxisWidth: 65,
index: customTooltipIndex,
categories: ["Sales"],
colors: customTooltipColors,
valueFormatter: currencyValueFormatter,
customTooltip: (props: CustomTooltipProps) => {
const { payload, active, label } = props;
if (!active || !payload) return null;
const categoryPayload = payload?.[0];
if (!categoryPayload) return null;
const value = categoryPayload.value as number;
const dataKey = categoryPayload.dataKey as number;
const previousIndex = data.findIndex((e) => e[customTooltipIndex] === label);
const previousValues: any = previousIndex > 0 ? data[previousIndex - 1] : {};
const prev = previousValues ? previousValues[dataKey] : undefined;
const percentage = ((value - prev) / prev) * 100 ?? undefined;
const color = getBadgeColor(percentage);
return (
<div className="w-56 translate-y-14 flex items-center justify-between rounded-tremor-default text-tremor-default bg-tremor-background p-2 shadow-tremor-dropdown border border-tremor-border">
<span className="text-right text-tremor-content whitespace-nowrap">{dataKey}</span>
<div className="flex items-center space-x-2">
<span className="font-medium text-right whitespace-nowrap text-tremor-content-emphasis">
{currencyValueFormatter(value)}
</span>
{percentage ? (
<span
className={`inline-flex text-xs px-1.5 py-0.5 bg-${color}-100 text-${color}-600 rounded`}
>
{percentage > 0 ? "+" : ""}
{percentage.toFixed(1)}%
</span>
) : null}
</div>
</div>
);
},
},
};
export const CustomTooltipComplex: Story = {
args: {
yAxisWidth: 65,
index: customTooltipIndex,
categories: ["Sales"],
colors: customTooltipColors,
valueFormatter: currencyValueFormatter,
customTooltip: (props: CustomTooltipProps) => {
const { payload, active, label } = props;
if (!active || !payload) return null;
const categoryPayload = payload?.[0];
if (!categoryPayload) return null;
const value = categoryPayload.value as number;
const dataKey = categoryPayload.dataKey as number;
const previousIndex = data.findIndex((e) => e[customTooltipIndex] === label);
const previousValues: any = previousIndex > 0 ? data[previousIndex - 1] : {};
const prev = previousValues ? previousValues[dataKey] : undefined;
const percentage = ((value - prev) / prev) * 100 ?? undefined;
const badgeColor = getBadgeColor(percentage);
return (
<div className="rounded-tremor-default bg-tremor-background p-2 shadow-tremor-dropdown border border-tremor-border">
<div className="flex flex-1 space-x-2.5">
<div className={`w-1 flex flex-col bg-${categoryPayload.color}-500 rounded`} />
<div className="w-full">
<p className="text-tremor-default font-medium text-tremor-content-emphasis">
{dataKey}
</p>
<p className="text-tremor-default text-tremor-content-subtle">{label}</p>
<p className="mt-2 font-medium whitespace-nowrap text-tremor-content-emphasis">
{currencyValueFormatter(value)}
</p>
{percentage ? (
<div className="mt-1 flex items-end space-x-2">
<div
className={`inline-flex text-tremor-default px-1.5 py-0.5 bg-${badgeColor}-100 text-${badgeColor}-600 rounded`}
>
{percentage > 0 ? "+" : null}
{percentage.toFixed(1)}%
</div>
<div className="whitespace-nowrap text-tremor-default text-tremor-content-subtle">
from previous month
</div>
</div>
) : null}
</div>
</div>
</div>
);
},
},
};
export const tickGap: Story = {
args: {
data: longBaseChartData,
tickGap: 200,
},
};
export const AxisLabels: Story = {
args: {
xAxisLabel: "Month of Year",
yAxisLabel: "Amount (USD)",
},
};
export const DataLabelsSimple: Story = {
args: {
renderLabel: true,
},
};
export const DataLabelsObject: Story = {
args: {
renderLabel: { fill: "white", fontSize: 20, position: "top" },
},
};
const renderCustomizedLabel = ({ x, y, value }: { x: number; y: number; value: number }) => {
return (
<text x={x} y={y} fill="white" textAnchor="end" dominantBaseline="central">
{value}
</text>
);
};
export const DataLabelsFunction: Story = {
args: {
renderLabel: renderCustomizedLabel,
},
};