forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorCustomizeTab.tsx
641 lines (597 loc) · 24.7 KB
/
EditorCustomizeTab.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import * as React from "react"
import { observable, computed, action } from "mobx"
import { observer } from "mobx-react"
import { ChartEditor } from "./ChartEditor"
import { Grapher } from "../grapher/core/Grapher"
import { ComparisonLineConfig } from "../grapher/scatterCharts/ComparisonLine"
import {
NumberField,
Toggle,
FieldsRow,
Section,
BindAutoString,
BindString,
TextField,
Button,
RadioGroup,
} from "./Forms"
import { debounce, isEqual, omit, trimObject } from "../clientUtils/Util"
import { faPlus } from "@fortawesome/free-solid-svg-icons/faPlus"
import { faMinus } from "@fortawesome/free-solid-svg-icons/faMinus"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { ColorSchemeDropdown, ColorSchemeOption } from "./ColorSchemeDropdown"
import { EditorColorScaleSection } from "./EditorColorScaleSection"
import { ColorSchemeName } from "../grapher/color/ColorConstants"
import { TimeBoundValue } from "../clientUtils/TimeBounds"
import {
FacetAxisDomain,
FacetStrategy,
} from "../grapher/core/GrapherConstants"
import Select from "react-select"
import { SortOrder, SortBy, SortConfig } from "../clientUtils/owidTypes"
@observer
class ColorSchemeSelector extends React.Component<{ grapher: Grapher }> {
@action.bound onChange(selected: ColorSchemeOption) {
// The onChange method can return an array of values (when multiple
// items can be selected) or a single value. Since we are certain that
// we are not using the multi-option select we can force the type to be
// a single value.
this.props.grapher.baseColorScheme = (
selected.value === "default" ? undefined : selected.value
) as ColorSchemeName
// clear out saved, pre-computed colors so the color scheme change is immediately visible
this.props.grapher.seriesColorMap?.clear()
}
@action.bound onInvertColorScheme(value: boolean) {
this.props.grapher.invertColorScheme = value || undefined
this.props.grapher.seriesColorMap?.clear()
}
render() {
const { grapher } = this.props
return (
<React.Fragment>
<FieldsRow>
<div className="form-group">
<label>Color scheme</label>
<ColorSchemeDropdown
value={grapher.baseColorScheme || "default"}
onChange={this.onChange}
invertedColorScheme={!!grapher.invertColorScheme}
additionalOptions={[
{
colorScheme: undefined,
gradient: undefined,
label: "Default",
value: "default",
},
]}
/>
</div>
</FieldsRow>
<FieldsRow>
<Toggle
label="Invert colors"
value={!!grapher.invertColorScheme}
onValue={this.onInvertColorScheme}
/>
</FieldsRow>
</React.Fragment>
)
}
}
interface SortOrderDropdownOption {
label: string
value: Omit<SortConfig, "sortOrder">
display?: { name: string; displayName: string }
}
@observer
class SortOrderSection extends React.Component<{ editor: ChartEditor }> {
@computed get sortConfig(): SortConfig {
return this.grapher._sortConfig
}
@computed get grapher() {
return this.props.editor.grapher
}
@computed get sortOptions(): SortOrderDropdownOption[] {
const { features } = this.props.editor
let dimensionSortOptions: SortOrderDropdownOption[] = []
if (features.canSortByColumn) {
dimensionSortOptions = this.grapher.yColumnsFromDimensions.map(
(column): SortOrderDropdownOption => ({
label: column.displayName,
display: {
name: column.name,
displayName: column.displayName,
},
value: {
sortBy: SortBy.column,
sortColumnSlug: column.slug,
} as SortConfig,
})
)
}
return [
{ label: "Entity name", value: { sortBy: SortBy.entityName } },
{ label: "Total value", value: { sortBy: SortBy.total } },
...dimensionSortOptions,
]
}
@action.bound onSortByChange(selected: SortOrderDropdownOption | null) {
this.grapher.sortBy = selected?.value.sortBy
this.grapher.sortColumnSlug = selected?.value.sortColumnSlug
}
@action.bound onSortOrderChange(sortOrder: string) {
this.grapher.sortOrder = sortOrder as SortOrder
}
render() {
return (
<Section name="Sort Order">
<small className="form-text text-muted">
For line charts the sort order is only applied when it's
collapsed to a bar chart.
</small>
<div className="form-group">
Sort by
<Select
options={this.sortOptions}
onChange={this.onSortByChange}
value={this.sortOptions.find((opt) =>
isEqual(
opt.value,
trimObject(omit(this.sortConfig, "sortOrder"))
)
)}
formatOptionLabel={(opt, { context }) =>
opt.display && context === "menu" ? (
<span>
{opt.display.displayName}
<br />
<small style={{ opacity: 0.8 }}>
{opt.display.name}
</small>
</span>
) : (
opt.label
)
}
menuPlacement="auto"
/>
</div>
<div className="form-group">
Sort order
<RadioGroup
options={[
{ label: "Descending", value: SortOrder.desc },
{ label: "Ascending", value: SortOrder.asc },
]}
value={this.sortConfig.sortOrder}
onChange={this.onSortOrderChange}
/>
</div>
</Section>
)
}
}
@observer
class FacetSection extends React.Component<{ editor: ChartEditor }> {
base: React.RefObject<HTMLDivElement> = React.createRef()
@computed get grapher() {
return this.props.editor.grapher
}
@computed get facetOptions(): Array<{
label: string
value?: FacetStrategy
}> {
return [{ label: "auto" }].concat(
this.grapher.availableFacetStrategies.map((s) => {
return { label: s.toString(), value: s }
})
)
}
@computed get facetSelection(): { label: string; value?: FacetStrategy } {
const strategy = this.grapher.selectedFacetStrategy
if (strategy) {
return { label: strategy.toString(), value: strategy }
}
return { label: "auto" }
}
@action.bound onFacetSelectionChange(
selected: {
label: string
value?: FacetStrategy
} | null
) {
this.grapher.selectedFacetStrategy = selected?.value
}
render() {
const yAxisConfig = this.props.editor.grapher.yAxis
return (
<Section name="Faceting">
<div className="form-group">
Faceting strategy
<Select
options={this.facetOptions}
value={this.facetSelection}
onChange={this.onFacetSelectionChange}
/>
</div>
<FieldsRow>
<Toggle
label={`Facets have uniform y-axis`}
value={
yAxisConfig.facetDomain === FacetAxisDomain.shared
}
onValue={(value) => {
yAxisConfig.facetDomain = value
? FacetAxisDomain.shared
: FacetAxisDomain.independent
}}
/>
</FieldsRow>
</Section>
)
}
}
@observer
class TimelineSection extends React.Component<{ editor: ChartEditor }> {
base: React.RefObject<HTMLDivElement> = React.createRef()
@computed get grapher() {
return this.props.editor.grapher
}
@computed get minTime() {
return this.grapher.minTime
}
@computed get maxTime() {
return this.grapher.maxTime
}
@computed get timelineMinTime() {
return this.grapher.timelineMinTime
}
@computed get timelineMaxTime() {
return this.grapher.timelineMaxTime
}
@action.bound onMinTime(value: number | undefined) {
this.grapher.minTime = value ?? TimeBoundValue.negativeInfinity
}
@action.bound onMaxTime(value: number | undefined) {
this.grapher.maxTime = value ?? TimeBoundValue.positiveInfinity
}
@action.bound onTimelineMinTime(value: number | undefined) {
this.grapher.timelineMinTime = value
}
@action.bound onTimelineMaxTime(value: number | undefined) {
this.grapher.timelineMaxTime = value
}
@action.bound onToggleHideTimeline(value: boolean) {
this.grapher.hideTimeline = value || undefined
}
@action.bound onToggleShowYearLabels(value: boolean) {
this.grapher.showYearLabels = value || undefined
}
render() {
const { features } = this.props.editor
const { grapher } = this
return (
<Section name="Timeline selection">
<FieldsRow>
{features.timeDomain && (
<NumberField
label="Selection start"
value={this.minTime}
onValue={debounce(this.onMinTime)}
allowNegative
/>
)}
<NumberField
label={
features.timeDomain
? "Selection end"
: "Selected year"
}
value={this.maxTime}
onValue={debounce(this.onMaxTime)}
allowNegative
/>
</FieldsRow>
{features.timelineRange && (
<FieldsRow>
<NumberField
label="Timeline min"
value={this.timelineMinTime}
onValue={debounce(this.onTimelineMinTime)}
allowNegative
/>
<NumberField
label="Timeline max"
value={this.timelineMaxTime}
onValue={debounce(this.onTimelineMaxTime)}
allowNegative
/>
</FieldsRow>
)}
<FieldsRow>
<Toggle
label="Hide timeline"
value={!!grapher.hideTimeline}
onValue={this.onToggleHideTimeline}
/>
{features.showYearLabels && (
<Toggle
label="Always show year labels"
value={!!grapher.showYearLabels}
onValue={this.onToggleShowYearLabels}
/>
)}
</FieldsRow>
</Section>
)
}
}
@observer
class ComparisonLineSection extends React.Component<{ editor: ChartEditor }> {
@observable comparisonLines: ComparisonLineConfig[] = []
@action.bound onAddComparisonLine() {
const { grapher } = this.props.editor
grapher.comparisonLines.push({})
}
@action.bound onRemoveComparisonLine(index: number) {
const { grapher } = this.props.editor
grapher.comparisonLines!.splice(index, 1)
}
render() {
const { comparisonLines } = this.props.editor.grapher
return (
<Section name="Comparison line">
<p>
Overlay a line onto the chart for comparison. Supports basic{" "}
<a href="https://github.com/silentmatt/expr-eval#expression-syntax">
mathematical expressions
</a>
.
</p>
<Button onClick={this.onAddComparisonLine}>
<FontAwesomeIcon icon={faPlus} /> Add comparison line
</Button>
{comparisonLines.map((comparisonLine, i) => (
<div key={i}>
{`Line ${i + 1}`}{" "}
<Button onClick={() => this.onRemoveComparisonLine(i)}>
<FontAwesomeIcon icon={faMinus} />
</Button>
<TextField
label={`y=`}
placeholder="x"
value={comparisonLine.yEquals}
onValue={action((value: string) => {
comparisonLine.yEquals = value || undefined
})}
/>
<TextField
label="Label"
value={comparisonLine.label}
onValue={action((value: string) => {
comparisonLine.label = value || undefined
})}
/>
</div>
))}
</Section>
)
}
}
@observer
export class EditorCustomizeTab extends React.Component<{
editor: ChartEditor
}> {
render() {
const xAxisConfig = this.props.editor.grapher.xAxis
const yAxisConfig = this.props.editor.grapher.yAxis
const { features } = this.props.editor
const { grapher } = this.props.editor
return (
<div>
{features.canCustomizeYAxis && (
<Section name="Y Axis">
{features.canCustomizeYAxisScale && (
<React.Fragment>
<FieldsRow>
<NumberField
label={`Min`}
value={yAxisConfig.min}
onValue={(value) =>
(yAxisConfig.min = value)
}
allowDecimal
allowNegative
/>
<NumberField
label={`Max`}
value={yAxisConfig.max}
onValue={(value) =>
(yAxisConfig.max = value)
}
allowDecimal
allowNegative
/>
</FieldsRow>
{features.canRemovePointsOutsideAxisDomain && (
<FieldsRow>
<Toggle
label={`Remove points outside domain`}
value={
yAxisConfig.removePointsOutsideDomain ||
false
}
onValue={(value) =>
(yAxisConfig.removePointsOutsideDomain =
value || undefined)
}
/>
</FieldsRow>
)}
<FieldsRow>
<Toggle
label={`Enable log/linear selector`}
value={
yAxisConfig.canChangeScaleType ||
false
}
onValue={(value) =>
(yAxisConfig.canChangeScaleType =
value || undefined)
}
/>
</FieldsRow>
</React.Fragment>
)}
{features.canCustomizeYAxisLabel && (
<BindString
label="Label"
field="label"
store={yAxisConfig}
/>
)}
</Section>
)}
{features.canCustomizeXAxis && (
<Section name="X Axis">
{features.canCustomizeXAxisScale && (
<React.Fragment>
<FieldsRow>
<NumberField
label={`Min`}
value={xAxisConfig.min}
onValue={(value) =>
(xAxisConfig.min = value)
}
allowDecimal
allowNegative
/>
<NumberField
label={`Max`}
value={xAxisConfig.max}
onValue={(value) =>
(xAxisConfig.max = value)
}
allowDecimal
allowNegative
/>
</FieldsRow>
{features.canRemovePointsOutsideAxisDomain && (
<FieldsRow>
<Toggle
label={`Remove points outside domain`}
value={
xAxisConfig.removePointsOutsideDomain ||
false
}
onValue={(value) =>
(xAxisConfig.removePointsOutsideDomain =
value || undefined)
}
/>
</FieldsRow>
)}
<FieldsRow>
<Toggle
label={`Enable log/linear selector`}
value={
xAxisConfig.canChangeScaleType ||
false
}
onValue={(value) =>
(xAxisConfig.canChangeScaleType =
value || undefined)
}
/>
</FieldsRow>
</React.Fragment>
)}
{features.canCustomizeXAxisLabel && (
<BindString
label="Label"
field="label"
store={xAxisConfig}
/>
)}
</Section>
)}
<TimelineSection editor={this.props.editor} />
<FacetSection editor={this.props.editor} />
<Section name="Color scheme">
<ColorSchemeSelector grapher={grapher} />
</Section>
{features.canSpecifySortOrder && (
<SortOrderSection editor={this.props.editor} />
)}
{grapher.activeColorScaleExceptMap && (
<EditorColorScaleSection
scale={grapher.activeColorScaleExceptMap}
features={{
visualScaling: false,
legendDescription:
grapher.isScatter ||
grapher.isSlopeChart ||
grapher.isStackedBar,
}}
/>
)}
{(features.hideLegend || features.entityType) && (
<Section name="Legend">
<FieldsRow>
{features.hideLegend && (
<Toggle
label={`Hide legend`}
value={!!grapher.hideLegend}
onValue={(value) =>
(grapher.hideLegend =
value || undefined)
}
/>
)}
</FieldsRow>
{features.entityType && (
<BindAutoString
label="Entity name"
field="entityType"
store={grapher}
auto="country"
/>
)}
</Section>
)}
{features.relativeModeToggle && (
<Section name="Controls">
<FieldsRow>
<Toggle
label={`Hide relative toggle`}
value={!!grapher.hideRelativeToggle}
onValue={(value) =>
(grapher.hideRelativeToggle =
value || false)
}
/>
</FieldsRow>
</Section>
)}
{features.canHideTotalValueLabel && (
<Section name="Display">
<FieldsRow>
<Toggle
label={`Hide total value label`}
value={!!grapher.hideTotalValueLabel}
onValue={(value) =>
(grapher.hideTotalValueLabel =
value || false)
}
/>
</FieldsRow>
</Section>
)}
{features.comparisonLine && (
<ComparisonLineSection editor={this.props.editor} />
)}
</div>
)
}
}