-
Notifications
You must be signed in to change notification settings - Fork 323
/
LevelThermometer.js
126 lines (115 loc) · 3.77 KB
/
LevelThermometer.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
// @flow
import * as d3 from 'd3'
import { pointsToLevels, categoryPointsFromMilestoneMap, categoryColorScale, categoryIds } from '../constants'
import React from 'react'
import type { MilestoneMap } from '../constants'
const margins = {
top: 30,
right: 20,
bottom: 30,
left: 10
}
const height = 150
const width = 550
type Props = {
milestoneByTrack: MilestoneMap,
}
class LevelThermometer extends React.Component<Props> {
pointScale: any
topAxisFn: any
bottomAxisFn: any
topAxis: *
bottomAxis: *
constructor(props: *) {
super(props)
this.pointScale = d3.scaleLinear()
.domain([0, 135])
.rangeRound([0, width - margins.left - margins.right]);
this.topAxisFn = d3.axisTop()
.scale(this.pointScale)
.tickValues(Object.keys(pointsToLevels))
.tickFormat(points => pointsToLevels[points])
this.bottomAxisFn = d3.axisBottom()
.scale(this.pointScale)
.tickValues(Object.keys(pointsToLevels))
}
componentDidMount() {
d3.select(this.topAxis).call(this.topAxisFn)
.selectAll('text')
.attr("y", 0)
.attr("x", -25)
.attr("transform", "rotate(90)")
.attr("dy", ".35em")
.style('font-size', '12px')
.style('text-anchor', 'start')
d3.select(this.bottomAxis).call(this.bottomAxisFn)
.selectAll('text')
.attr("y", 0)
.attr("x", 10)
.attr("transform", "rotate(90)")
.attr("dy", ".35em")
.style('font-size', '12px')
.style('text-anchor', 'start')
}
rightRoundedRect(x: *, y: *, width: *, height: *, radius: *) {
return "M" + x + "," + y
+ "h" + (width - radius)
+ "a" + radius + "," + radius + " 0 0 1 " + radius + "," + radius
+ "v" + (height - 2 * radius)
+ "a" + radius + "," + radius + " 0 0 1 " + -radius + "," + radius
+ "h" + (radius - width)
+ "z";
}
render() {
let categoryPoints = categoryPointsFromMilestoneMap(this.props.milestoneByTrack)
let lastCategoryIndex = 0
categoryPoints.forEach((categoryPoint, i) => {
if (categoryPoint.points) lastCategoryIndex = i
})
let cumulativePoints = 0
return (
<figure>
<style jsx>{`
figure {
margin: 0 0 0 -10px;
}
svg {
width: ${width}px;
height: ${height + 10}px;
}
`}</style>
<svg>
<g transform={`translate(${margins.left},${margins.top})`}>
{categoryPoints.map((categoryPoint, i) => {
const x = this.pointScale(cumulativePoints)
const width = this.pointScale(cumulativePoints + categoryPoint.points) - x
cumulativePoints += categoryPoint.points
return (i != lastCategoryIndex ?
<rect
key={categoryPoint.categoryId}
x={x}
y={0}
width={width}
height={height - margins.top - margins.bottom}
style={{fill: categoryColorScale(categoryPoint.categoryId), borderRight: "1px solid #000"}}
/> :
<path
key={categoryPoint.categoryId}
d={this.rightRoundedRect(x, 0, width, height - margins.top - margins.bottom, 3)}
style={{fill: categoryColorScale(categoryPoint.categoryId)}}
/>
)
})}
<g ref={ref => this.topAxis = ref} className="top-axis"
transform={`translate(0, -2)`}
/>
<g ref={ref => this.bottomAxis = ref} className="bottom-axis"
transform={`translate(0,${height - margins.top - margins.bottom + 1})`}
/>
</g>
</svg>
</figure>
)
}
}
export default LevelThermometer