forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorMapTab.tsx
178 lines (162 loc) · 5.79 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
import * as React from "react"
import { computed, action } from "mobx"
import { observer } from "mobx-react"
import { isEmpty } from "../clientUtils/Util"
import {
MapProjectionLabels,
MapProjectionName,
} from "../grapher/mapCharts/MapProjections"
import { ChartEditor } from "./ChartEditor"
import {
NumericSelectField,
NumberField,
SelectField,
Toggle,
Section,
} from "./Forms"
import { EditorColorScaleSection } from "./EditorColorScaleSection"
import { LegacyVariableId } from "../clientUtils/owidTypes"
import { MapConfig } from "../grapher/mapCharts/MapConfig"
import { ChartDimension } from "../grapher/chart/ChartDimension"
import { ColorScale } from "../grapher/color/ColorScale"
import { MapChart } from "../grapher/mapCharts/MapChart"
@observer
class VariableSection extends React.Component<{
mapConfig: MapConfig
filledDimensions: ChartDimension[]
}> {
@action.bound onVariableId(variableId: LegacyVariableId) {
this.props.mapConfig.columnSlug = variableId.toString()
}
@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 variables on data tab first</h2>
</section>
)
const projections = Object.keys(MapProjectionLabels)
const labels = Object.values(MapProjectionLabels)
return (
<Section name="Map">
<NumericSelectField
label="Variable"
value={
mapConfig.columnSlug
? parseInt(mapConfig.columnSlug)
: undefined
}
options={filledDimensions.map((d) => d.variableId)}
optionLabels={filledDimensions.map(
(d) => d.column.displayName
)}
onValue={this.onVariableId}
/>
<SelectField
label="Region"
value={mapConfig.projection}
options={projections}
optionLabels={labels}
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
}
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."
/>
</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 extends React.Component<{ editor: ChartEditor }> {
@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}
/>
{isReady && (
<React.Fragment>
<TimelineSection mapConfig={mapConfig} />
<EditorColorScaleSection
scale={colorScale}
features={{
visualScaling: true,
legendDescription: false,
}}
/>
<TooltipSection mapConfig={mapConfig} />
</React.Fragment>
)}
</div>
)
}
}