-
Notifications
You must be signed in to change notification settings - Fork 68
/
LineSeriesExamples.jsx
164 lines (154 loc) Β· 5.01 KB
/
LineSeriesExamples.jsx
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
/* eslint react/no-array-index-key: 0 */
import React from 'react';
import { range } from 'd3-array';
import {
Sparkline,
LineSeries,
PointSeries,
Label,
BandLine,
HorizontalReferenceLine,
VerticalReferenceLine,
WithTooltip,
PatternLines,
LinearGradient,
} from '@data-ui/sparkline';
import { color, allColors } from '@data-ui/theme';
import Example from './Example';
import Spacer from '../shared/Spacer';
PatternLines.displayName = 'PatternLines';
LinearGradient.displayName = 'LinearGradient';
const sparklineProps = {
ariaLabel: 'This is a Sparkline of...',
width: 500,
height: 100,
margin: { top: 24, right: 80, bottom: 24, left: 8 },
};
const curvesData = [-10, 10, -10, 5, -5, 0];
const curves = ['cardinal', 'linear', 'monotoneX', 'basis'];
export default [
{
description: 'LineSeries',
components: [Sparkline, LineSeries],
example: () => (
<Spacer top={2} left={2} flexDirection="column">
<Example title="Default with last point">
<Sparkline
{...sparklineProps}
data={range(50).map(() => Math.random() * (Math.random() > 0.2 ? 1 : 2))}
>
<LineSeries />
<PointSeries
points={['last']}
size={4}
stroke="white"
fill={color.default}
renderLabel={d => d.toFixed(1)}
labelPosition="right"
/>
</Sparkline>
</Example>
{(data => (
<Example title="Filled with gradient + Tooltip">
<WithTooltip renderTooltip={({ datum }) => datum.y.toFixed(2)}>
{({ onMouseMove, onMouseLeave, tooltipData }) => (
<Sparkline
{...sparklineProps}
onMouseLeave={onMouseLeave}
onMouseMove={onMouseMove}
data={data}
>
<LinearGradient
id="area_gradient"
to={allColors.pink[1]}
from={allColors.pink[6]}
/>
<LineSeries showArea fill="url(#area_gradient)" stroke={allColors.pink[4]} />
{tooltipData && [
<VerticalReferenceLine
key="ref-line"
strokeWidth={1}
reference={tooltipData.index}
stroke={allColors.pink[6]}
strokeDasharray="4 4"
/>,
<PointSeries
key="ref-point"
points={[tooltipData.index]}
fill={allColors.pink[6]}
/>,
]}
</Sparkline>
)}
</WithTooltip>
</Example>
))(range(40).map(() => Math.random() * (Math.random() > 0.2 ? 1 : 2)))}
<Example title="Min, max, median, and inner quartiles">
<Sparkline
{...sparklineProps}
data={range(30).map(() => Math.random() * (Math.random() > 0.2 ? 1 : 2))}
>
<PatternLines
id="innerquartile_pattern"
height={4}
width={4}
stroke={allColors.blue[5]}
strokeWidth={1}
orientation={['diagonal']}
/>
<BandLine band="innerquartiles" fill="url(#innerquartile_pattern)" />
<HorizontalReferenceLine
stroke={allColors.blue[6]}
strokeDasharray="3,3"
strokeLinecap="square"
strokeWidth={1}
renderLabel={d => `Median ${d.toFixed(1)}`}
/>
<LineSeries curve="linear" stroke={allColors.blue[4]} />
<PointSeries
points={['min', 'max']}
fill={allColors.blue[6]}
size={4}
stroke="white"
renderLabel={d => d.toFixed(1)}
/>
</Sparkline>
</Example>
<Example title="All the curves">
<Sparkline {...sparklineProps} data={curvesData}>
{curvesData.map((_, i) => (
<VerticalReferenceLine
key={i}
reference={i}
stroke={color.grays[3]}
strokeWidth={1}
strokeLinecap="square"
strokeDasharray="2,3"
labelPosition="right"
/>
))}
{curves.map((curve, i) => (
<LineSeries
key={curve}
curve={curve}
stroke={allColors.cyan[i + 2]}
strokeWidth={2}
/>
))}
{curves.map((curve, i) => (
<Label
key={curve}
textAnchor="start"
x={sparklineProps.width - sparklineProps.margin.right}
dy={8 + i * 12}
label={curve}
fill={allColors.cyan[i + 2]}
strokeWidth={1}
/>
))}
</Sparkline>
</Example>
</Spacer>
),
},
];