-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
Copy pathcolorScheme.ts
281 lines (251 loc) · 8.69 KB
/
colorScheme.ts
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import {
CategoricalColorNamespace,
ensureIsArray,
getCategoricalSchemeRegistry,
getLabelsColorMap,
} from '@superset-ui/core';
import { intersection, omit, pick } from 'lodash';
import { areObjectsEqual } from 'src/reduxUtils';
const EMPTY_ARRAY: string[] = [];
/**
* Force falsy namespace values to undefined to default to GLOBAL
*
* @param namespace
* @returns - namespace or default undefined
*/
export const getColorNamespace = (namespace?: string) => namespace || undefined;
/**
*
* Field shared_label_colors used to be a dict of all colors for all labels.
* Force shared_label_colors field to be a list of actual shared labels.
*
* @param sharedLabelsColors - the shared label colors list
* @returns string[]
*/
export const enforceSharedLabelsColorsArray = (
sharedLabelsColors: string[] | Record<string, string> | undefined,
) => (Array.isArray(sharedLabelsColors) ? sharedLabelsColors : EMPTY_ARRAY);
/**
* Get labels shared across all charts in a dashboard.
* Merges a fresh instance of shared label colors with a stored one.
*
* @param currentSharedLabels - existing shared labels to merge with fresh
* @returns Record<string, string>
*/
export const getFreshSharedLabels = (
currentSharedLabels: string[] = [],
): string[] => {
const { chartsLabelsMap } = getLabelsColorMap();
const allLabels = Array.from(chartsLabelsMap.values()).flatMap(
({ labels }) => labels,
);
const duplicates = Array.from(
allLabels.reduce(
(counts, label) => counts.set(label, (counts.get(label) || 0) + 1),
new Map(),
),
)
.filter(([, count]) => count > 1)
.map(([label]) => label);
return Array.from(
new Set([...ensureIsArray(currentSharedLabels), ...duplicates]),
);
};
export const getSharedLabelsColorMapEntries = (
currentColorMap: Record<string, string>,
sharedLabels: string[],
): Record<string, string> =>
Object.fromEntries(
Object.entries(currentColorMap).filter(([label]) =>
sharedLabels.includes(label),
),
);
/**
* Returns all entries (labels and colors) except custom label colors.
*
* @param customLabelsColor - the custom label colors in label_colors field
* @returns all color entries except custom label colors
*/
export const getFreshLabelsColorMapEntries = (
customLabelsColor: Record<string, string> = {},
): Record<string, string> => {
const labelsColorMapInstance = getLabelsColorMap();
const allEntries = Object.fromEntries(labelsColorMapInstance.getColorMap());
// custom label colors are applied and stored separetely via label_colors
Object.keys(customLabelsColor).forEach(label => {
delete allEntries[label];
});
return allEntries;
};
/**
* Returns all dynamic labels and colors (excluding custom label colors).
*
* @param labelsColorMap - the labels color map
* @param customLabelsColor - the custom label colors in label_colors field
* @returns all color entries except custom label colors
*/
export const getDynamicLabelsColors = (
fullLabelsColors: Record<string, string>,
customLabelsColor: Record<string, string> = {},
): Record<string, string> =>
omit(fullLabelsColors, Object.keys(customLabelsColor));
export const getColorSchemeDomain = (colorScheme: string) =>
getCategoricalSchemeRegistry().get(colorScheme)?.colors || [];
/**
* Compare the current labels color map with a fresh one
*
* @param currentLabelsColorMap - the current labels color map
* @returns true if the labels color map is the same as fresh
*/
export const isLabelsColorMapSynced = (
storedLabelsColors: Record<string, any>,
freshLabelsColors: Record<string, any>,
customLabelColors: Record<string, string>,
): boolean => {
const freshLabelsCount = Object.keys(freshLabelsColors).length;
// still updating, pass
if (!freshLabelsCount) return true;
const commonKeys = intersection(
Object.keys(storedLabelsColors),
Object.keys(freshLabelsColors),
);
const comparableStoredLabelsColors = pick(storedLabelsColors, commonKeys);
const comparableFreshLabelsColors = pick(freshLabelsColors, commonKeys);
const isSynced = areObjectsEqual(
comparableStoredLabelsColors,
comparableFreshLabelsColors,
{
ignoreFields: Object.keys(customLabelColors),
},
);
return isSynced;
};
/**
* Annihilate color maps
*
* @param color_namespace - the categorical namespace
*/
export const resetColors = (color_namespace?: string) => {
const labelsColorMapInstance = getLabelsColorMap();
const categoricalNamespace = CategoricalColorNamespace.getNamespace(
getColorNamespace(color_namespace),
);
categoricalNamespace.resetColors();
labelsColorMapInstance.reset();
};
/**
* Update the labels color map based on current color scheme
* It will respect custom label colors if set via namespace
*
* @param namespace - the color namespace
* @param colorScheme - the current color scheme
*/
export const refreshLabelsColorMap = (
namespace?: string,
colorScheme?: string,
merge = false,
) => {
const colorNameSpace = getColorNamespace(namespace);
const categoricalNamespace =
CategoricalColorNamespace.getNamespace(colorNameSpace);
const labelsColorMapInstance = getLabelsColorMap();
labelsColorMapInstance.updateColorMap(
categoricalNamespace,
colorScheme,
merge,
);
};
/**
* Merge labels colors with custom labels colors
* Apply labels color based on chosen color scheme
*
* @param metadata - the dashboard metadata object
*/
export const applyColors = (
metadata: Record<string, any>,
// Create a fresh color map by changing color scheme
fresh: boolean | string[] = false,
// Catch new labels in the color map as they appear
merge = false,
// Apply only label colors that are shared across multiple charts.
shared = false,
) => {
const colorNameSpace = getColorNamespace(metadata?.color_namespace);
const categoricalNamespace =
CategoricalColorNamespace.getNamespace(colorNameSpace);
const colorScheme = metadata?.color_scheme;
const fullLabelsColor = metadata?.map_label_colors || {};
const sharedLabels = enforceSharedLabelsColorsArray(
metadata?.shared_label_colors,
);
const customLabelsColor = metadata?.label_colors || {};
const sharedLabelsColor = getSharedLabelsColorMapEntries(
fullLabelsColor,
sharedLabels,
);
if (fresh && !Array.isArray(fresh)) {
// reset custom label colors
// re-evaluate all other label colors
categoricalNamespace.resetColors();
}
if (fresh && Array.isArray(fresh)) {
// when a color scheme is not set for the dashboard
// should only reset colors for charts that have changed scheme
// while keeping colors of existing shared label colors intact
// this is used also to reset custom label colors when added or removed
categoricalNamespace.resetColorsForLabels(fresh);
}
if (fresh || merge) {
// re-instantiate a fresh labels color map based on current scheme
// it consider just applied custom label colors if present and all forced colors
// it will merge with the existing color map new labels only when merge is true
refreshLabelsColorMap(metadata?.color_namespace, colorScheme, merge);
}
let applicableColorMapEntries: Record<string, any> = fullLabelsColor;
if (fresh) {
// requires a new map all together
applicableColorMapEntries = {
...getFreshLabelsColorMapEntries(customLabelsColor),
};
}
if (merge) {
// must only add up newly appearing labels
// without overriding existing ones
applicableColorMapEntries = {
...fullLabelsColor,
...getFreshLabelsColorMapEntries(customLabelsColor),
};
}
if (shared) {
// must apply the colors to only shared labels
applicableColorMapEntries = sharedLabelsColor;
}
applicableColorMapEntries = {
...applicableColorMapEntries,
...customLabelsColor,
};
// apply the final color map
if (applicableColorMapEntries) {
Object.keys(applicableColorMapEntries).forEach(label => {
categoricalNamespace.setColor(label, applicableColorMapEntries[label]);
});
}
};