-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathMultiColorPicker.js
174 lines (153 loc) · 5.45 KB
/
MultiColorPicker.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
import ColorPicker from './ColorPicker';
import {UnconnectedColorscalePicker} from './ColorscalePicker';
import Field from './Field';
import Info from './Info';
import PropTypes from 'prop-types';
import RadioBlocks from '../widgets/RadioBlocks';
import React, {Component} from 'react';
import nestedProperty from 'plotly.js/src/lib/nested_property';
import {adjustColorscale, connectToContainer} from 'lib';
const CustomColorscalePicker = connectToContainer(UnconnectedColorscalePicker, {
modifyPlotProps: (props, context, plotProps) => {
if (
props.attr === 'marker.color' &&
context.fullData
.filter(t => context.traceIndexes.includes(t.index))
.every(t => t.marker && t.marker.color) &&
(plotProps.fullValue && typeof plotProps.fullValue === 'string')
) {
plotProps.fullValue =
context.fullData &&
context.fullData
.filter(t => context.traceIndexes.includes(t.index))
.map(t => [0, t.marker.color]);
}
},
});
class UnconnectedMultiColorPicker extends Component {
constructor(props, context) {
super(props, context);
this.state = {
selectedConstantColorOption:
context.traceIndexes.length > 1 &&
props.fullValue &&
props.fullValue.every(v => v[1] === props.fullValue[0][1])
? 'single'
: 'multiple',
};
this.setColor = this.setColor.bind(this);
this.setColors = this.setColors.bind(this);
}
setColor(color) {
if (this.props.setColor) {
this.props.setColor(color);
} else {
this.props.updatePlot(color);
}
}
setColors(colorscale, colorscaleType) {
const numberOfTraces = this.props.tracesToColor.length;
const colors = colorscale.map(c => c[1]);
let adjustedColors = colors;
if (colorscaleType !== 'categorical') {
adjustedColors = adjustColorscale(colors, numberOfTraces, colorscaleType);
}
if (adjustedColors.every(c => c === adjustedColors[0]) || colorscaleType === 'categorical') {
adjustedColors = adjustColorscale(colors, numberOfTraces, colorscaleType, {repeat: true});
}
const updates = adjustedColors.map(color => ({
[this.props.attr]: color,
}));
this.context.updateContainer(updates);
}
render() {
const _ = this.context.localize;
const constantOptions = [
{label: _('Single'), value: 'single'},
{label: _('Multiple'), value: 'multiple'},
];
const selectedConstantColorOption = this.props.parentSelectedConstantColorOption
? this.props.parentSelectedConstantColorOption
: this.state.selectedConstantColorOption;
const multiMessage = this.props.multiColorMessage
? this.props.multiColorMessage
: _('Each will be colored according to the selected colors.');
const singleMessage = this.props.singleColorMessage
? this.props.singleColorMessage
: _('All will be colored in the same color.');
if (this.context.traceIndexes.length > 1) {
return (
<Field {...this.props} suppressMultiValuedMessage>
<RadioBlocks
options={constantOptions}
activeOption={selectedConstantColorOption}
onOptionChange={
this.props.onConstantColorOptionChange
? this.props.onConstantColorOptionChange
: value => this.setState({selectedConstantColorOption: value})
}
/>
<Info>{selectedConstantColorOption === 'single' ? singleMessage : multiMessage}</Info>
{selectedConstantColorOption === 'single' ? (
<ColorPicker attr={this.props.attr} updatePlot={this.setColor} />
) : (
<CustomColorscalePicker
suppressMultiValuedMessage
attr={this.props.attr}
updatePlot={this.setColors}
fullValue={this.props.fullValue}
initialCategory={'categorical'}
/>
)}
</Field>
);
}
return (
<ColorPicker attr={this.props.attr} updatePlot={this.setColor} label={this.props.label} />
);
}
}
UnconnectedMultiColorPicker.propTypes = {
multiColorMessage: PropTypes.string,
singleColorMessage: PropTypes.string,
updatePlot: PropTypes.func,
attr: PropTypes.string,
parentSelectedConstantColorOption: PropTypes.string,
onConstantColorOptionChange: PropTypes.func,
messageKeyWordSingle: PropTypes.string,
messageKeyWordPlural: PropTypes.string,
tracesToColor: PropTypes.array,
...Field.propTypes,
};
UnconnectedMultiColorPicker.contextTypes = {
localize: PropTypes.func,
updateContainer: PropTypes.func,
traceIndexes: PropTypes.array,
fullData: PropTypes.array,
};
export default connectToContainer(UnconnectedMultiColorPicker, {
modifyPlotProps(props, context, plotProps) {
if (plotProps.isVisible) {
const colors = [];
let tracesToColor = [];
const dedupedTraceIndexes = [];
context.traceIndexes.forEach(i => {
if (!dedupedTraceIndexes.includes(i)) {
dedupedTraceIndexes.push(i);
}
});
dedupedTraceIndexes.forEach(traceIndex => {
const traces = context.fullData.filter(trace => trace.index === traceIndex);
tracesToColor = tracesToColor.concat(traces);
traces.forEach(t => {
const value = nestedProperty(t, props.attr).get();
if (value) {
colors.push(value);
}
});
});
plotProps.tracesToColor = tracesToColor;
plotProps.fullValue = colors.map(c => [0, c]);
}
},
});