-
Notifications
You must be signed in to change notification settings - Fork 691
/
MultiSeriesLine.js
163 lines (152 loc) · 4.58 KB
/
MultiSeriesLine.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
import React from 'react';
import { Group } from '@vx/group';
import { LinePath } from '@vx/shape';
import { curveBasis } from '@vx/curve';
import { cityTemperature } from '@vx/mock-data';
import { AxisLeft, AxisBottom } from '@vx/axis';
import { scaleTime, scaleLinear, scaleOrdinal } from '@vx/scale';
import { extent } from 'd3-array';
import { timeParse } from 'd3-time-format';
import { compose, withState, withHandlers } from 'recompose';
// util
const parseDate = timeParse('%Y%m%d');
// [{date: "", new york: "", san francisco: "", austin: ""}]
const rawData = cityTemperature;
const cityNames = Object.keys(rawData[0]).filter(k => k !== 'date');
// rawData => [{id: "", values: [{ date, temperature }]}, ...]
const data = cityNames.map(cityName => {
return {
id: cityName,
values: rawData.map(d => ({
date: d.date,
temperature: d[cityName],
})),
};
});
// utils
const getCity = cityId => data.find(city => city.id === cityId);
const addCity = (selected, cityId) => selected.concat(cityId);
const removeCity = (selected, cityId) => selected.filter(city => city !== cityId);
const removeCityOrResetSelected = (selected, cityId) => {
let nextSelected = removeCity(selected, cityId);
if (nextSelected.length === 0) nextSelected = initialSelectedState;
return nextSelected;
};
// recompose higher-order function for state and event handlers
const initialSelectedState = cityNames;
const withSelected = compose(
withState('selected', 'setSelected', initialSelectedState),
withHandlers({
updateSelected: ({ selected, setSelected }) => cityId => {
let fn = addCity;
if (selected.includes(cityId)) fn = removeCityOrResetSelected;
setSelected(fn(selected, cityId));
},
resetSelected: ({ setSelected }) => () => {
setSelected(initialSelectedState);
},
}),
);
// the chart
export default withSelected(({ selected, updateSelected, width, height, margin }) => {
// bounds
const xMax = width - margin.left - margin.right;
const yMax = height - margin.top - margin.bottom;
// accessors
const x = d => parseDate(d.date);
const y = d => +d.temperature;
// scales
const xScale = scaleTime({
range: [0, xMax],
domain: extent(rawData, x),
});
const yScale = scaleLinear({
range: [yMax, 0],
domain: extent(
selected.slice().reduce((ret, c) => {
return ret.concat(getCity(c).values);
}, []),
y,
),
});
const color = scaleOrdinal({
range: ['#3b99d8', '#239f85', '#9a5cb4'],
domain: cityNames,
});
return (
<svg width={width} height={height}>
<Group top={margin.top} left={margin.left}>
<AxisBottom label="" top={yMax} scale={xScale} hideAxisLine />
<AxisLeft scale={yScale} label="Temperature (ºF)" />
{selected.map(getCity).map(({ id, values }) => {
const lastDatum = values[values.length - 1];
return (
<g key={`${id}`}>
<LinePath
data={values}
xScale={xScale}
yScale={yScale}
x={x}
y={y}
curve={curveBasis}
stroke={color(id)}
strokeWidth={1}
/>
<text
fontSize={9}
dy="0.35em"
dx={2}
x={xScale(x(lastDatum))}
y={yScale(y(lastDatum))}
>
{id}
</text>
</g>
);
})}
<Legend
data={data}
selected={selected}
xMax={xMax}
yMax={yMax}
color={color}
updateSelected={updateSelected}
/>
</Group>
</svg>
);
});
const Legend = ({ data: legendData, selected, updateSelected, xMax, yMax, color }) => {
const margin = 20;
const xPadding = 60;
const yPadding = 30;
const yOffset = yMax - yPadding;
const xOffset = xMax - xPadding;
const size = 8;
const fontSize = 12;
return (
<g>
{legendData.map(({ id }, i) => {
return (
<g
key={`legend-${id}`}
className="legend-item"
transform={`translate(${xOffset}, ${yOffset - i * margin})`}
onClick={() => updateSelected(id)}
fillOpacity={selected.includes(id) ? 1 : 0.5}
>
<rect width={size} height={size} fill={color(id)} />
<text fill={color(id)} dy=".7em" dx={fontSize} fontSize={fontSize}>
{id}
</text>
</g>
);
})}
<style jsx>{`
.legend-item:hover {
cursor: pointer;
}
`}</style>
</g>
);
};