forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorMapTab.tsx
218 lines (198 loc) · 7.25 KB
/
EditorMapTab.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
ChartTypeName,
GrapherInterface,
MapProjectionName,
} from "@ourworldindata/types"
import {
ChartDimension,
MapChart,
MapConfig,
MapProjectionLabels,
} from "@ourworldindata/grapher"
import { ColumnSlug, isEmpty, ToleranceStrategy } from "@ourworldindata/utils"
import { action, computed } from "mobx"
import { observer } from "mobx-react"
import React from "react"
import { EditorColorScaleSection } from "./EditorColorScaleSection.js"
import { NumberField, Section, SelectField, Toggle } from "./Forms.js"
import { AbstractChartEditor } from "./AbstractChartEditor.js"
@observer
class VariableSection extends React.Component<{
mapConfig: MapConfig
filledDimensions: ChartDimension[]
parentConfig?: GrapherInterface
}> {
@action.bound onColumnSlug(columnSlug: ColumnSlug) {
this.props.mapConfig.columnSlug = columnSlug
}
@action.bound onBlurColumnSlug() {
if (this.props.mapConfig.columnSlug === undefined) {
this.props.mapConfig.columnSlug =
this.props.parentConfig?.map?.columnSlug
}
}
@action.bound onProjection(projection: string | undefined) {
this.props.mapConfig.projection = projection as MapProjectionName
}
render() {
const { mapConfig, filledDimensions } = this.props
if (isEmpty(filledDimensions))
return (
<section>
<h2>Add some indicators on data tab first</h2>
</section>
)
// const projections = Object.keys(MapProjectionLabels)
// const labels = Object.values(MapProjectionLabels)
return (
<Section name="Map">
<SelectField
label="Indicator"
value={mapConfig.columnSlug}
options={filledDimensions.map((d) => ({
value: d.columnSlug,
label: d.column.displayName,
}))}
onValue={this.onColumnSlug}
onBlur={this.onBlurColumnSlug}
/>
<SelectField
label="Region"
value={mapConfig.projection}
options={Object.entries(MapProjectionLabels).map(
([key, val]) => ({ value: key, label: val })
)}
onValue={this.onProjection}
/>
</Section>
)
}
}
@observer
class TimelineSection extends React.Component<{ mapConfig: MapConfig }> {
@action.bound onToggleHideTimeline(value: boolean) {
this.props.mapConfig.hideTimeline = value || undefined
}
@action.bound setMapTime(time: number | undefined) {
this.props.mapConfig.time = time
}
@action.bound onTolerance(tolerance: number | undefined) {
this.props.mapConfig.timeTolerance = tolerance
}
get toleranceStrategyOptions(): {
value: ToleranceStrategy
label: string
}[] {
const toleranceStrategyLabels = {
[ToleranceStrategy.closest]:
"Closest: Consider data points in the past and future",
[ToleranceStrategy.backwards]:
"Backwards: Only consider data points in the past",
[ToleranceStrategy.forwards]:
"Forwards: Only consider data points in the future",
}
return Object.values(ToleranceStrategy).map(
(val: ToleranceStrategy) => ({
value: val,
label: toleranceStrategyLabels[val],
})
)
}
@action.bound onSelectToleranceStrategy(value: string | undefined) {
this.props.mapConfig.toleranceStrategy = value as ToleranceStrategy
}
render() {
const { mapConfig } = this.props
return (
<Section name="Timeline">
<NumberField
label="Target year"
value={mapConfig.time}
onValue={this.setMapTime}
allowNegative
/>
<Toggle
label="Hide timeline"
value={!!mapConfig.hideTimeline}
onValue={this.onToggleHideTimeline}
/>
<NumberField
label="Tolerance of data"
value={mapConfig.timeTolerance}
onValue={this.onTolerance}
helpText="Specify a range of years from which to pull data. For example, if the map shows 1990 and tolerance is set to 1, then data from 1989 or 1991 will be shown if no data is available for 1990."
/>
{(mapConfig.timeTolerance || 0) > 0 && (
<SelectField
label="Tolerance strategy"
value={mapConfig.toleranceStrategy}
options={this.toleranceStrategyOptions}
onValue={this.onSelectToleranceStrategy}
/>
)}
</Section>
)
}
}
@observer
class TooltipSection extends React.Component<{ mapConfig: MapConfig }> {
@action.bound onTooltipUseCustomLabels(tooltipUseCustomLabels: boolean) {
this.props.mapConfig.tooltipUseCustomLabels = tooltipUseCustomLabels
? true
: undefined
}
render() {
const { mapConfig } = this.props
return (
<Section name="Tooltip">
<Toggle
label={
"Show custom label in the tooltip, instead of the numeric value"
}
value={!!mapConfig.tooltipUseCustomLabels}
onValue={this.onTooltipUseCustomLabels}
/>
</Section>
)
}
}
@observer
export class EditorMapTab<
Editor extends AbstractChartEditor,
> extends React.Component<{ editor: Editor }> {
@computed get grapher() {
return this.props.editor.grapher
}
render() {
const { grapher } = this
const mapConfig = grapher.map
const { mapColumnSlug } = grapher
const mapChart = new MapChart({ manager: this.grapher })
const colorScale = mapChart.colorScale
const isReady = !!mapColumnSlug && grapher.table.has(mapColumnSlug)
return (
<div className="EditorMapTab tab-pane">
<VariableSection
mapConfig={mapConfig}
filledDimensions={grapher.filledDimensions}
parentConfig={this.props.editor.activeParentConfig}
/>
{isReady && (
<React.Fragment>
<TimelineSection mapConfig={mapConfig} />
<EditorColorScaleSection
scale={colorScale}
chartType={ChartTypeName.WorldMap}
showLineChartColors={false}
features={{
visualScaling: true,
legendDescription: false,
}}
/>
<TooltipSection mapConfig={mapConfig} />
</React.Fragment>
)}
</div>
)
}
}