-
Notifications
You must be signed in to change notification settings - Fork 121
/
settings.tsx
201 lines (183 loc) · 5.34 KB
/
settings.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
import { PureComponent } from 'react';
import { inject } from 'mobx-react';
import { DomainRange, Position, Rendering, Rotation } from '../chart_types/xy_chart/utils/specs';
import { mergeWithDefaultTheme, PartialTheme, Theme } from '../utils/themes/theme';
import { Domain } from '../utils/domain';
import { TooltipType, TooltipValueFormatter } from '../chart_types/xy_chart/utils/interactions';
import {
BrushEndListener,
ChartStore,
ElementClickListener,
ElementOverListener,
LegendItemListener,
CursorUpdateListener,
} from '../chart_types/xy_chart/store/chart_state';
import { ScaleTypes } from '../utils/scales/scales';
import { LIGHT_THEME } from '../utils/themes/light_theme';
export const DEFAULT_TOOLTIP_TYPE = TooltipType.VerticalCursor;
export const DEFAULT_TOOLTIP_SNAP = true;
interface TooltipProps {
type?: TooltipType;
snap?: boolean;
headerFormatter?: TooltipValueFormatter;
}
/**
* Event used to syncronize cursors between Charts.
*
* fired as callback argument for `CursorUpdateListener`
*/
export interface CursorEvent {
chartId: string;
scale: ScaleTypes;
/**
* @todo
* unit for event (i.e. `time`, `feet`, `count`, etc.)
*/
unit?: string;
value: number | string;
}
function isTooltipProps(config: TooltipType | TooltipProps): config is TooltipProps {
return typeof config === 'object';
}
function isTooltipType(config: TooltipType | TooltipProps): config is TooltipType {
return typeof config === 'string';
}
export interface SettingSpecProps {
chartStore?: ChartStore;
/**
* Full or partial theme to be merged with base
*/
theme?: Theme | PartialTheme;
/**
* Full default theme to use as base
*
* @default `LIGHT_THEME`
*/
baseTheme?: Theme;
rendering: Rendering;
rotation: Rotation;
animateData: boolean;
showLegend: boolean;
/** Either a TooltipType or an object with configuration of type, snap, and/or headerFormatter */
tooltip?: TooltipType | TooltipProps;
debug: boolean;
legendPosition?: Position;
showLegendDisplayValue: boolean;
onElementClick?: ElementClickListener;
onElementOver?: ElementOverListener;
onElementOut?: () => undefined | void;
onBrushEnd?: BrushEndListener;
onLegendItemOver?: LegendItemListener;
onLegendItemOut?: () => undefined | void;
onLegendItemClick?: LegendItemListener;
onLegendItemPlusClick?: LegendItemListener;
onLegendItemMinusClick?: LegendItemListener;
onCursorUpdate?: CursorUpdateListener;
xDomain?: Domain | DomainRange;
}
function getTheme(baseTheme?: Theme, theme?: Theme | PartialTheme): Theme {
const base = baseTheme ? baseTheme : LIGHT_THEME;
return theme ? mergeWithDefaultTheme(theme, base) : base;
}
function updateChartStore(props: SettingSpecProps) {
const {
chartStore,
theme,
baseTheme,
rotation,
rendering,
animateData,
showLegend,
tooltip,
legendPosition,
showLegendDisplayValue,
onElementClick,
onElementOver,
onElementOut,
onBrushEnd,
onLegendItemOver,
onLegendItemOut,
onLegendItemClick,
onLegendItemMinusClick,
onLegendItemPlusClick,
onCursorUpdate,
debug,
xDomain,
} = props;
if (!chartStore) {
return;
}
chartStore.chartTheme = getTheme(baseTheme, theme);
chartStore.chartRotation = rotation;
chartStore.chartRendering = rendering;
chartStore.animateData = animateData;
chartStore.debug = debug;
if (tooltip && isTooltipProps(tooltip)) {
const { type, snap, headerFormatter } = tooltip;
chartStore.tooltipType.set(type!);
chartStore.tooltipSnap.set(snap!);
chartStore.tooltipHeaderFormatter = headerFormatter;
} else if (tooltip && isTooltipType(tooltip)) {
chartStore.tooltipType.set(tooltip);
}
chartStore.setShowLegend(showLegend);
if (legendPosition) {
chartStore.legendPosition.set(legendPosition);
}
chartStore.showLegendDisplayValue.set(showLegendDisplayValue);
chartStore.customXDomain = xDomain;
if (onElementOver) {
chartStore.setOnElementOverListener(onElementOver);
}
if (onElementClick) {
chartStore.setOnElementClickListener(onElementClick);
}
if (onElementOut) {
chartStore.setOnElementOutListener(onElementOut);
}
if (onBrushEnd) {
chartStore.setOnBrushEndListener(onBrushEnd);
}
if (onLegendItemOver) {
chartStore.setOnLegendItemOverListener(onLegendItemOver);
}
if (onLegendItemOut) {
chartStore.setOnLegendItemOutListener(onLegendItemOut);
}
if (onLegendItemClick) {
chartStore.setOnLegendItemClickListener(onLegendItemClick);
}
if (onLegendItemPlusClick) {
chartStore.setOnLegendItemPlusClickListener(onLegendItemPlusClick);
}
if (onLegendItemMinusClick) {
chartStore.setOnLegendItemMinusClickListener(onLegendItemMinusClick);
}
if (onCursorUpdate) {
chartStore.setOnCursorUpdateListener(onCursorUpdate);
}
}
export class SettingsComponent extends PureComponent<SettingSpecProps> {
static defaultProps: Partial<SettingSpecProps> = {
rendering: 'canvas',
rotation: 0,
animateData: true,
showLegend: false,
debug: false,
tooltip: {
type: DEFAULT_TOOLTIP_TYPE,
snap: DEFAULT_TOOLTIP_SNAP,
},
showLegendDisplayValue: true,
};
componentDidMount() {
updateChartStore(this.props);
}
componentDidUpdate() {
updateChartStore(this.props);
}
render() {
return null;
}
}
export const Settings = inject('chartStore')(SettingsComponent);