-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
shame.js
227 lines (199 loc) · 7.05 KB
/
shame.js
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
/*
* DELETE THIS FILE. EVERYTHING NEEDS TO FIND A HOME.
*/
import {getFromId} from 'plotly.js/src/plots/cartesian/axis_ids';
import nestedProperty from 'plotly.js/src/lib/nested_property';
// Temporary fix for:
// https://github.com/plotly/react-chart-editor/issues/103
// We should be able to remove this once the plotly.react method has
// been integrated into react-plotly.js and released:
// https://github.com/plotly/react-plotly.js/issues/2
export const shamefullyClearAxisTypes = (graphDiv, {traceIndexes, update}) => {
if (!Array.isArray(graphDiv._fullData)) {
return;
}
let hasSrc = false;
for (const key in update) {
if (key.substr(key.length - 3) === 'src') {
hasSrc = true;
}
}
if (hasSrc) {
clearAxisTypes(graphDiv, traceIndexes);
}
};
const axLetters = ['x', 'y', 'z'];
function clearAxisTypes(gd, traces) {
for (let i = 0; i < traces.length; i++) {
const trace = gd._fullData[i];
for (let j = 0; j < 3; j++) {
const type = axLetters[j];
const ax = getFromId(gd, trace[type + 'axis'] || type);
// Do not clear log type.
// Log type is never an auto result so must have been intentional.
// We are also skipping clearing 3D which could cause bugs with 3D.
if (ax && ax.type !== 'log') {
const axAttr = ax._name;
const typeAttr = axAttr + '.type';
nestedProperty(gd.layout, typeAttr).set(null);
}
}
}
}
export const shamefullyAdjustAxisRef = (graphDiv, payload) => {
if (payload.axesToBeGarbageCollected) {
payload.axesToBeGarbageCollected.forEach(a => {
const axis = a.charAt(0);
const axisIdNumber = Number(a.slice(1));
nestedProperty(graphDiv.layout, `${axis}axis${axisIdNumber || ''}`).set(null);
Object.keys(graphDiv.layout)
.filter(key => key.startsWith(axis + 'axis'))
.forEach(key => {
if (nestedProperty(graphDiv.layout, `${key}.overlaying`).get() === a) {
nestedProperty(graphDiv.layout, `${key}.overlaying`).set(null);
}
});
});
}
if (payload.subplotToBeGarbageCollected) {
nestedProperty(graphDiv.layout, payload.subplotToBeGarbageCollected).set(null);
}
};
const geoRegex = /^(geo\d*)\./;
export const shamefullyAdjustGeo = ({layout}, {update}) => {
Object.keys(update).forEach(k => {
const geoMatch = geoRegex.exec(k);
if (geoMatch) {
const geo = geoMatch[1];
if (update[geo + '.scope']) {
update[geo + '.projection'] = {};
update[geo + '.center'] = {};
}
if (
// requesting projection change
update[geo + '.projection.type'] &&
(update[geo + '.projection.type'] === 'albers usa' ||
(layout[geo] && layout[geo].scope === 'usa'))
) {
update[geo + '.scope'] = {};
update[geo + '.center'] = {};
}
}
});
};
export const shamefullyAdjustMapbox = gd => {
if (gd.layout && gd.layout.mapbox && gd.layout.mapbox.style) {
return;
}
nestedProperty(gd.layout, 'mapbox.style').set('open-street-map');
};
export const shamefullyAddTableColumns = (graphDiv, {traceIndexes, update}) => {
if (
update['cells.values'] &&
(!graphDiv.data[traceIndexes[0]].header || !graphDiv.data[traceIndexes[0]].header.valuessrc)
) {
update['header.values'] = update['cells.valuessrc'];
} else if (update['header.values'] === null) {
update['header.values'] = graphDiv.data[traceIndexes[0]].cells.valuessrc || null;
} else if (update['cells.values'] === null && !graphDiv.data[traceIndexes[0]].header.valuessrc) {
update['header.values'] = null;
}
};
export const shamefullyAdjustSplitStyleTargetContainers = (graphDiv, {traceIndexes, update}) => {
for (const attr in update) {
if (attr && attr.startsWith('transforms') && attr.endsWith('groups')) {
const transformIndex = parseInt(attr.split('[')[1], 10);
const transform = graphDiv.data[traceIndexes[0]].transforms[transformIndex];
if (transform && transform.type === 'groupby' && transform.styles) {
// Create style containers for all groups
if (!transform.styles.length && update[attr] && Array.isArray(update[attr])) {
const dedupedGroups = [];
update[attr].forEach(group => {
if (!dedupedGroups.includes(group)) {
dedupedGroups.push(group);
}
});
const styles = dedupedGroups.map(groupEl => ({
target: groupEl,
value: {},
}));
update[`transforms[${transformIndex}].styles`] = styles;
}
// When clearing the data selector of groupby transforms, we want to clear
// all the styles we've added
if (transform.styles.length && !update[attr]) {
update[`transforms[${transformIndex}].styles`] = [];
}
}
}
}
};
export const shamefullyCreateSplitStyleProps = (graphDiv, attr, traceIndex, splitTraceGroup) => {
if (!Array.isArray(splitTraceGroup)) {
splitTraceGroup = [splitTraceGroup]; // eslint-disable-line
}
let indexOfSplitTransform = null;
graphDiv.data[traceIndex].transforms.forEach((t, i) => {
if (t.type === 'groupby') {
indexOfSplitTransform = i;
}
});
function getProp(group) {
let indexOfStyleObject = null;
graphDiv.data[traceIndex].transforms[indexOfSplitTransform].styles.forEach((s, i) => {
if (s.target.toString() === group) {
indexOfStyleObject = i;
}
});
let path =
graphDiv.data[traceIndex].transforms[indexOfSplitTransform].styles[indexOfStyleObject].value;
attr.split('.').forEach(p => {
if (!path[p]) {
path[p] = {};
}
path = path[p];
});
return nestedProperty(
graphDiv.data[traceIndex].transforms[indexOfSplitTransform].styles[indexOfStyleObject].value,
attr
);
}
return splitTraceGroup.map(g => getProp(g));
};
export const shamefullyDeleteRelatedAnalysisTransforms = (graphDiv, payload) => {
const parentTraceDataIndex = payload.traceIndexes[0];
const parentUid = graphDiv.data[parentTraceDataIndex].uid;
if (parentUid) {
const relatedAnalysisTraceIndexes = [];
graphDiv.data.forEach((d, i) => {
if (
d.transforms &&
d.transforms.some(
transform =>
['moving-average', 'fit'].includes(transform.type) && transform.inputUid === parentUid
)
) {
relatedAnalysisTraceIndexes.push(i);
}
});
if (relatedAnalysisTraceIndexes.length) {
relatedAnalysisTraceIndexes.forEach(i => {
graphDiv.data.splice(i, 1);
});
}
}
};
export const shamefullyAdjustSizeref = (gd, {update}) => {
const {'marker.size': size = null, 'marker.sizesrc': src = null} = update;
if (size && src) {
const DEFAULT_MAX_AREA_PX = 45;
const scaleFactor = DEFAULT_MAX_AREA_PX * DEFAULT_MAX_AREA_PX;
update['marker.sizeref'] = size.reduce((a, b) => Math.max(a, b)) / scaleFactor;
update['marker.sizemode'] = 'area';
}
};
export const shamefullyAdjustAxisDirection = (gd, {update}) => {
if (update.type === 'funnel' && gd.data.length === 1) {
gd.layout.yaxis.autorange = 'reversed';
}
};