-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathvictory-histogram.js
411 lines (382 loc) · 14.2 KB
/
victory-histogram.js
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*eslint-disable no-magic-numbers*/
import React from "react";
import { storiesOf } from "@storybook/react";
import { VictoryHistogram } from "../packages/victory-histogram/src";
import { VictoryLine } from "../packages/victory-line/src";
import { VictoryScatter } from "../packages/victory-scatter/src";
import { VictoryTooltip } from "../packages/victory-tooltip/src";
import { VictoryStack } from "../packages/victory-stack/src";
import { VictoryTheme } from "../packages/victory-core/src";
import { getChartDecorator } from "./decorators";
import { getData } from "./data";
import { data, timeData } from "./victory-histogram-data";
import * as d3Array from "d3-array";
import * as d3Scale from "d3-scale";
import * as d3Time from "d3-time";
storiesOf("VictoryHistogram", module)
.add("default rendering", () => <VictoryHistogram />)
.add("default rendering with data", () => <VictoryHistogram data={data} />);
/* binSpacing */
storiesOf("VictoryHistogram.binSpacing.vertical", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("bin spacing = 10", () => <VictoryHistogram data={data} binSpacing={10} />)
.add("bin spacing = 5", () => <VictoryHistogram data={data} binSpacing={5} />);
storiesOf("VictoryHistogram.binSpacing.horizontal", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("bin spacing = 10", () => <VictoryHistogram data={data} horizontal binSpacing={10} />)
.add("bin spacing = 5", () => <VictoryHistogram data={data} horizontal binSpacing={5} />);
/* data */
storiesOf("VictoryHistogram.data.dates", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("date data from 2015-2020", () => <VictoryHistogram data={timeData} />)
.add("date data from 2020", () => {
const newTimeData = timeData.map(({ x }) => {
const newDate = new Date(x);
newDate.setFullYear(2020);
return { x: newDate };
});
return <VictoryHistogram data={newTimeData} />;
})
.add("date data from Jan-June 2020", () => {
const newTimeData = timeData.map(({ x }, index) => {
const newDate = new Date(x);
newDate.setFullYear(2020);
newDate.setMonth(Math.ceil(index / 100));
return { x: newDate };
});
return <VictoryHistogram data={newTimeData} />;
})
.add("data date from May 2020", () => {
const newTimeData = timeData.map(({ x }) => {
const newDate = new Date(x);
newDate.setMonth(4);
newDate.setFullYear(2020);
return { x: newDate };
});
return <VictoryHistogram data={newTimeData} />;
});
storiesOf("VictoryHistogram.data", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("with data accessors", () => (
<VictoryHistogram data={data.map(({ x }) => ({ value: x }))} x={({ value }) => value} />
))
.add("with time data accessors", () => (
<VictoryHistogram
bins="year"
data={timeData.map(({ x }) => ({ value: x }))}
x={({ value }) => value}
/>
))
.add("with empty data", () => <VictoryHistogram data={[]} />)
.add("with empty data and numeric bins", () => <VictoryHistogram data={[]} bins={2} />)
.add("with empty data and explicit bins", () => (
<VictoryHistogram data={[]} bins={[0, 30, 100, 150]} />
))
.add("with empty data and explicit date bins", () => (
<VictoryHistogram
scale={{ x: "time" }}
data={[]}
bins={[new Date(2020, 0, 1), new Date(2020, 5, 1), new Date(2021, 0, 1)]}
/>
));
/* bins */
storiesOf("VictoryHistogram.bins.dates", module)
.add("with custom bins = [01/01/2020, 01/06/2020, 01/01/2021]", () => (
<VictoryHistogram
data={timeData}
bins={[new Date(2020, 0, 1), new Date(2020, 5, 1), new Date(2021, 0, 1)]}
/>
))
.add("numeric bins = 2", () => <VictoryHistogram data={timeData} bins={2} />)
.add("numeric bins = 10", () => <VictoryHistogram data={timeData} bins={10} />)
.add("day bins", () => {
const niceTimeScale = d3Scale
.scaleTime()
.domain(d3Array.extent(timeData, ({ x }) => x))
.nice();
const ticks = niceTimeScale.ticks(d3Time.utcDay);
return <VictoryHistogram data={timeData} bins={ticks} />;
})
.add("month bins", () => {
const niceTimeScale = d3Scale
.scaleTime()
.domain(d3Array.extent(timeData, ({ x }) => x))
.nice();
const ticks = niceTimeScale.ticks(d3Time.utcMonth);
return <VictoryHistogram data={timeData} bins={ticks} />;
})
.add("year bins", () => {
const niceTimeScale = d3Scale
.scaleTime()
.domain(d3Array.extent(timeData, ({ x }) => x))
.nice();
const ticks = niceTimeScale.ticks(d3Time.utcYear);
return <VictoryHistogram data={timeData} bins={ticks} />;
})
.add("default", () => <VictoryHistogram data={timeData} />);
storiesOf("VictoryHistogram.bins.vertical", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("numeric bins = 2", () => <VictoryHistogram data={data} bins={2} />)
.add("numeric bins = 8", () => <VictoryHistogram data={data} bins={8} />)
.add("numeric bins = 40", () => <VictoryHistogram data={data} bins={40} />)
.add("custom bins/edges = [0, 30, 50, 100]", () => (
<VictoryHistogram data={data} bins={[0, 30, 50, 100]} />
))
.add("custom bins/edges = [0, 30, 50]", () => <VictoryHistogram data={data} bins={[0, 30, 50]} />)
.add("custom bins/edges = [0, 20, 30, 70, 100]", () => (
<VictoryHistogram data={data} bins={[0, 20, 30, 70, 100]} />
))
.add("custom bins/edges = [0, 30, 70, 100, 130]", () => (
<VictoryHistogram data={data} bins={[0, 30, 70, 100, 130]} />
))
.add("custom bins/edges = [0, 10, 30, 70, 150]", () => (
<VictoryHistogram data={data} bins={[0, 10, 30, 70, 150]} />
))
.add("custom bins/edges = [30, 70, 150]", () => (
<VictoryHistogram data={data} bins={[30, 70, 150]} />
));
storiesOf("VictoryHistogram.bins.horizontal", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("numeric bins = 2", () => <VictoryHistogram data={data} horizontal bins={2} />)
.add("numeric bins = 8", () => <VictoryHistogram data={data} horizontal bins={8} />)
.add("numeric bins = 40", () => <VictoryHistogram data={data} horizontal bins={40} />)
.add("custom bins/edges = [0, 30, 50, 100]", () => (
<VictoryHistogram data={data} horizontal bins={[0, 30, 50, 100]} />
))
.add("custom bins/edges = [0, 30, 50]", () => (
<VictoryHistogram data={data} horizontal bins={[0, 30, 50]} />
))
.add("custom bins/edges = [0, 20, 30, 70, 100]", () => (
<VictoryHistogram data={data} horizontal bins={[0, 20, 30, 70, 100]} />
))
.add("custom bins/edges = [0, 30, 70, 100, 130]", () => (
<VictoryHistogram data={data} horizontal bins={[0, 30, 70, 100, 130]} />
))
.add("custom bins/edges = [0, 10, 30, 70, 150]", () => (
<VictoryHistogram data={data} horizontal bins={[0, 10, 30, 70, 150]} />
))
.add("custom bins/edges = [30, 70, 150]", () => (
<VictoryHistogram data={data} horizontal bins={[30, 70, 150]} />
));
/* styles */
storiesOf("VictoryHistogram.styles", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("with styles", () => (
<VictoryHistogram
data={data}
style={{ data: { transform: "translate(0px, -20px) skew(2deg, 2deg)" } }}
/>
))
.add("with functional styles", () => (
<VictoryHistogram
horizontal
data={data}
style={{
labels: {
fill: ({ datum }) =>
datum.binnedData.some(({ x }) => x === 22) ? "palevioletred" : "black"
},
data: {
stroke: ({ datum }) => (datum.y > 3 ? "red" : "transparent"),
strokeWidth: 3,
opacity: ({ datum }) => (datum.y > 3 ? 1 : 0.4)
}
}}
labels={["one", "two", "three", "four", "five"]}
/>
));
/* theme */
storiesOf("VictoryHistogram.theme", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.material }))
.add("material theme", () => <VictoryHistogram data={data} />);
storiesOf("VictoryHistogram.theme", module)
.addDecorator(getChartDecorator({ theme: VictoryTheme.grayscale }))
.add("grayscale (default) theme", () => <VictoryHistogram data={data} />);
/* corner radius */
storiesOf("VictoryHistogram.cornerRadius", module)
.addDecorator(getChartDecorator())
.add("cornerRadius = 1", () => <VictoryHistogram data={data} cornerRadius={1} />)
.add("cornerRadius = 5", () => <VictoryHistogram data={data} cornerRadius={5} />)
.add("cornerRadius = 7", () => <VictoryHistogram data={data} cornerRadius={7} />)
.add("cornerRadius = 5 (horizontal)", () => (
<VictoryHistogram horizontal data={data} cornerRadius={5} />
));
/* getPath */
storiesOf("VictoryHistogram.getPath", module)
.addDecorator(getChartDecorator({ domainPadding: 25 }))
.add("custom bar path (vertical)", () => {
const getPathFn = (props) => {
const { x0, x1, y0, y1 } = props;
return `M ${x0}, ${y0}
L ${(x1 + x0) / 2}, ${y1}
L ${x1}, ${y0}
z`;
};
return <VictoryHistogram data={data} getPath={getPathFn} />;
})
.add("custom bar path (horizontal)", () => {
const getPathFn = (props) => {
const { x0, x1, y0, y1 } = props;
return `M ${x0}, ${y1}
L ${x1}, ${(y0 + y1) / 2}
L ${x0}, ${y0}
z`;
};
return <VictoryHistogram data={data} horizontal getPath={getPathFn} />;
});
/* labels */
storiesOf("VictoryHistogram.labels", module)
.addDecorator(getChartDecorator({ domainPadding: 25 }))
.add("function labels", () => (
<VictoryHistogram data={data} labels={({ datum }) => `${datum.x0} - ${datum.x1}`} />
))
.add("function labels (horizontal)", () => (
<VictoryHistogram horizontal data={data} labels={({ datum }) => `${datum.x0} - ${datum.x1}`} />
))
.add("array labels", () => (
<VictoryHistogram data={data} labels={["", "", "three", "four", 5, "six"]} />
));
/* tooltips */
storiesOf("VictoryHistogram.tooltips", module)
.addDecorator(getChartDecorator({ domainPadding: 25 }))
.add("tooltips", () => (
<VictoryHistogram
data={data.map(({ x }) => ({ value: x }))}
labels={({ datum }) => `${datum.x0} - ${datum.x1}`}
labelComponent={<VictoryTooltip active />}
x="value"
/>
))
.add("tooltips (horizontal)", () => (
<VictoryHistogram
horizontal
data={data}
labels={({ datum }) => `${datum.x0} - ${datum.x1}`}
labelComponent={<VictoryTooltip active />}
/>
))
.add("array tooltips with long and short strings", () => (
<VictoryHistogram
data={data}
labels={["one", "two", 3, "wow, four tooltips", "five"]}
labelComponent={<VictoryTooltip active />}
/>
));
/* scale */
storiesOf("VictoryHistogram.scale", module)
.addDecorator(getChartDecorator({ domainPadding: 25 }))
.add("time scale", () => <VictoryHistogram bins="month" data={timeData} />)
.add("time scale with labels", () => (
<VictoryHistogram
binSpacing={10}
data={timeData}
bins="year"
labels={({ datum }) => `${datum.x0.getFullYear()} - ${datum.x1.getFullYear()}`}
/>
))
.add("horizontal time scale with labels", () => (
<VictoryHistogram
horizontal
binSpacing={10}
data={timeData}
bins="year"
labels={({ datum }) => `${datum.x0.getFullYear()} - ${datum.x1.getFullYear()}`}
/>
));
storiesOf("VictoryHistogram.scale", module)
.addDecorator(getChartDecorator({ scale: { y: "log" }, domainPadding: 25 }))
.add("log scale", () => <VictoryHistogram data={data} />)
.add("horizontal log scale", () => <VictoryHistogram horizontal data={data} />);
storiesOf("VictoryHistogram.with other charts", module)
.addDecorator(getChartDecorator({ domainPadding: 25, theme: VictoryTheme.material }))
.add("with line", () => [
<VictoryHistogram key="histogram" data={data} />,
<VictoryLine
key="line"
data={[
{ x: 0, y: 5 },
{ x: 10, y: 5 },
{ x: 20, y: 2 },
{ x: 30, y: 9 },
{ x: 50, y: 2 },
{ x: 60, y: 4 },
{ x: 80, y: 12 },
{ x: 120, y: 8 }
]}
/>
])
.add("with scatter", () => [
<VictoryHistogram key="histogram" data={data} />,
<VictoryScatter
key="scatter"
data={[
{ x: 0, y: 5 },
{ x: 10, y: 5 },
{ x: 20, y: 2 },
{ x: 30, y: 9 },
{ x: 50, y: 2 },
{ x: 60, y: 4 },
{ x: 80, y: 12 },
{ x: 120, y: 8 }
]}
/>
])
.add("with line horizontal", () => [
<VictoryHistogram horizontal key="histogram" data={data} />,
<VictoryLine
horizontal
key="line"
data={[
{ x: 0, y: 5 },
{ x: 10, y: 5 },
{ x: 20, y: 2 },
{ x: 30, y: 9 },
{ x: 50, y: 2 },
{ x: 60, y: 4 },
{ x: 80, y: 12 },
{ x: 120, y: 8 }
]}
/>
]);
const stackedData = [
...[50, 30, 100, 32, 50, 10, 49, 78, 20].map((count) => getData(count, count, 100)),
[{ x: 1 }, { x: 3 }, { x: 1 }, { x: 2 }]
];
storiesOf("VictoryHistogram.stacked", module)
.addDecorator(getChartDecorator())
.add("stacked histogram with explicit bins passed to stack", () => (
<VictoryStack colorScale="qualitative" bins={[0, 20, 100]}>
{stackedData.map((d, index) => (
<VictoryHistogram data={d} key={index} />
))}
</VictoryStack>
))
.add("stacked histogram with numeric bins passed to stack", () => (
<VictoryStack colorScale="qualitative" bins={2}>
{stackedData.map((d, index) => (
<VictoryHistogram data={d} key={index} />
))}
</VictoryStack>
))
.add("stacked histogram with bins passed to first child", () => (
<VictoryStack colorScale="qualitative">
{stackedData.map((d, index) => (
<VictoryHistogram data={d} key={index} bins={index === 0 ? [0, 20, 100] : undefined} />
))}
</VictoryStack>
))
.add("stacked histogram", () => (
<VictoryStack colorScale="qualitative">
{stackedData.map((d, index) => (
<VictoryHistogram binSpacing={10} data={d} key={index} />
))}
</VictoryStack>
))
.add("stacked histogram with nested data", () => (
<VictoryStack colorScale="qualitative">
<VictoryHistogram data={data.map(({ x }) => ({ a: { b: { c: x } } }))} x="a.b.c" />
{stackedData.map((d, index) => (
<VictoryHistogram data={d} key={index} />
))}
</VictoryStack>
));