forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorSchemeDropdown.tsx
137 lines (120 loc) · 3.96 KB
/
ColorSchemeDropdown.tsx
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
import React from "react"
import { computed, action } from "mobx"
import Select from "react-select"
import { ChartTypeName } from "@ourworldindata/types"
import {
ColorScheme,
getColorSchemeForChartType,
} from "@ourworldindata/grapher"
import { observer } from "mobx-react"
import { bind } from "decko"
export interface ColorSchemeOption {
colorScheme?: ColorScheme
gradient?: string
label: string
value: string
}
interface ColorSchemeDropdownProps {
additionalOptions: ColorSchemeOption[]
value?: string
gradientColorCount: number
invertedColorScheme: boolean
chartType: ChartTypeName
onChange: (selected: ColorSchemeOption) => void
}
@observer
export class ColorSchemeDropdown extends React.Component<ColorSchemeDropdownProps> {
static defaultProps = {
additionalOptions: [],
gradientColorCount: 6,
invertedColorScheme: false,
}
@computed get additionalOptions() {
return this.props.additionalOptions
}
@computed get gradientColorCount() {
return this.props.gradientColorCount
}
@computed get colorSchemeOptions() {
return Object.entries(getColorSchemeForChartType(this.props.chartType))
.filter(([, v]) => v !== undefined)
.map(([key, scheme]) => {
return {
colorScheme: scheme,
gradient: this.createLinearGradient(
scheme,
this.gradientColorCount
),
label: scheme.name,
value: key,
}
})
}
@computed get allOptions() {
const { additionalOptions } = this
return additionalOptions.concat(this.colorSchemeOptions)
}
createLinearGradient(colorScheme: ColorScheme, count: number) {
const colors = colorScheme.getColors(count)
const step = 100 / count
const gradientEntries = colors.map(
(color, i) => `${color} ${i * step}%, ${color} ${(i + 1) * step}%`
)
return `linear-gradient(90deg, ${gradientEntries.join(", ")})`
}
@action.bound onChange(value: ColorSchemeOption | null) {
if (value) this.props.onChange(value)
}
@bind formatOptionLabel(option: ColorSchemeOption) {
const { invertedColorScheme } = this.props
return (
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
}}
>
<div>{option.label}</div>
{option.gradient && (
<span
style={{
backgroundImage: option.gradient,
width: "6rem",
height: "1.25rem",
border: "1px solid #aaa",
// Mirror the element if color schemes are inverted
transform: invertedColorScheme
? "scaleX(-1)"
: undefined,
}}
/>
)}
</div>
)
}
render() {
return (
<Select
options={this.allOptions}
formatOptionLabel={this.formatOptionLabel}
onChange={this.onChange}
value={this.allOptions.find(
(scheme) => scheme.value === this.props.value
)}
components={{
IndicatorSeparator: null,
}}
styles={{
singleValue: (provided) => {
return {
...provided,
width: "calc(100% - 10px)",
}
},
}}
menuPlacement="auto"
/>
)
}
}