@@ -8,12 +8,11 @@ const margin = {top: 20, right: 10, bottom: 20, left: 25};
8
8
const red = '#eb6a5b' ;
9
9
const green = '#b6e86f' ;
10
10
const blue = '#52b6ca' ;
11
- const colors = chroma . scale ( [ blue , green , red ] ) ;
11
+ const colors = chroma . scale ( [ blue , green , red ] ) . mode ( 'hsl' ) ;
12
12
13
- class LineChart extends Component {
13
+ class BarChart extends Component {
14
14
state = {
15
- highs : [ ] , // array of rects for high temps
16
- lows : [ ] , // array of rects for low temps
15
+ bars : [ ] , // array of rects
17
16
// d3 helpers
18
17
xScale : d3 . scaleTime ( ) . range ( [ margin . left , width - margin . right ] ) ,
19
18
yScale : d3 . scaleLinear ( ) . range ( [ height - margin . bottom , margin . top ] ) ,
@@ -32,30 +31,24 @@ class LineChart extends Component {
32
31
// data has changed, so recalculate scale domains
33
32
const timeDomain = d3 . extent ( data , d => d . date ) ;
34
33
const tempMax = d3 . max ( data , d => d . high ) ;
35
- const tempMin = d3 . min ( data , d => d . low ) ;
34
+ const colorDomain = d3 . extent ( data , d => d . avg ) ;
36
35
xScale . domain ( timeDomain ) ;
37
36
yScale . domain ( [ 0 , tempMax ] ) ;
38
- colorScale . domain ( [ tempMin , tempMax ] ) ;
37
+ colorScale . domain ( colorDomain ) ;
39
38
40
39
// calculate x and y for each rectangle
41
- const highs = data . map ( d => {
42
- const y = yScale ( d . high ) ;
40
+ const bars = data . map ( d => {
41
+ const y1 = yScale ( d . high ) ;
42
+ const y2 = yScale ( d . low ) ;
43
43
return {
44
44
x : xScale ( d . date ) ,
45
- y, height : height - margin . bottom - y ,
46
- fill : colors ( colorScale ( d . high ) ) ,
47
- }
48
- } ) ;
49
- const lows = data . map ( d => {
50
- const y = yScale ( d . low ) ;
51
- return {
52
- x : xScale ( d . date ) ,
53
- y, height : height - margin . bottom - y ,
54
- fill : colors ( colorScale ( d . low ) ) ,
45
+ y : y1 ,
46
+ height : y2 - y1 ,
47
+ fill : colors ( colorScale ( d . avg ) ) ,
55
48
}
56
49
} ) ;
57
50
58
- return { lows , highs } ;
51
+ return { bars } ;
59
52
}
60
53
61
54
componentDidUpdate ( ) {
@@ -67,9 +60,7 @@ class LineChart extends Component {
67
60
68
61
return (
69
62
< svg width = { width } height = { height } >
70
- { this . state . highs . map ( d =>
71
- ( < rect x = { d . x } y = { d . y } width = '2' height = { d . height } fill = { d . fill } /> ) ) }
72
- { this . state . lows . map ( d =>
63
+ { this . state . bars . map ( d =>
73
64
( < rect x = { d . x } y = { d . y } width = '2' height = { d . height } fill = { d . fill } /> ) ) }
74
65
< g >
75
66
< g ref = 'xAxis' transform = { `translate(0, ${ height - margin . bottom } )` } />
@@ -80,4 +71,4 @@ class LineChart extends Component {
80
71
}
81
72
}
82
73
83
- export default LineChart ;
74
+ export default BarChart ;
0 commit comments