Skip to content

Commit 750d862

Browse files
authored
Merge pull request #620 from plotly/color-pies
Color pie traces with colorscale
2 parents cfeee52 + 5893f00 commit 750d862

File tree

7 files changed

+87
-19
lines changed

7 files changed

+87
-19
lines changed

dev/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'brace/theme/textmate';
1616
import ACCESS_TOKENS from '../accessTokens';
1717

1818
const dataSources = {
19-
ints: [1, 2, 3, 4, 5, 6], // eslint-disable-line no-magic-numbers
19+
ints: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], // eslint-disable-line no-magic-numbers
2020
'jagged ints': [2, 1, 3, 5, 4, 6], // eslint-disable-line no-magic-numbers
2121
'toggle ints': [1, -1, 1, -1, 1, -1], // eslint-disable-line no-magic-numbers
2222
'big ints': [1000, 10100, 10000, 20000, 100000], // eslint-disable-line no-magic-numbers

src/components/fields/Colorscale.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ColorscalePicker from '../widgets/ColorscalePicker';
22
import Field from './Field';
33
import PropTypes from 'prop-types';
44
import React, {Component} from 'react';
5-
import {connectToContainer} from 'lib';
5+
import {connectToContainer, adjustColorscale} from 'lib';
66

77
class Colorscale extends Component {
88
constructor(props) {
@@ -12,6 +12,18 @@ class Colorscale extends Component {
1212

1313
onUpdate(colorscale, colorscaleType) {
1414
if (Array.isArray(colorscale)) {
15+
if (this.context.container.type === 'pie') {
16+
const numPieSlices = this.context.graphDiv.calcdata[0].length + 1;
17+
const adjustedColorscale = adjustColorscale(
18+
colorscale,
19+
numPieSlices,
20+
colorscaleType,
21+
{repeat: true}
22+
);
23+
this.props.updatePlot(adjustedColorscale);
24+
return;
25+
}
26+
1527
this.props.updatePlot(
1628
colorscale.map((c, i) => {
1729
let step = i / (colorscale.length - 1);
@@ -50,6 +62,11 @@ Colorscale.propTypes = {
5062
...Field.propTypes,
5163
};
5264

65+
Colorscale.contextTypes = {
66+
container: PropTypes.object,
67+
graphDiv: PropTypes.object,
68+
};
69+
5370
export default connectToContainer(Colorscale, {
5471
modifyPlotProps: (props, context, plotProps) => {
5572
if (
@@ -65,5 +82,30 @@ export default connectToContainer(Colorscale, {
6582
.filter(t => context.traceIndexes.includes(t.index))
6683
.map(t => [0, t.marker.color]);
6784
}
85+
86+
if (
87+
context &&
88+
context.container &&
89+
context.graphDiv &&
90+
(!plotProps.fullValue ||
91+
(Array.isArray(plotProps.fullValue) && !plotProps.fullValue.length)) &&
92+
context.container.type === 'pie' &&
93+
context.graphDiv.calcdata
94+
) {
95+
plotProps.fullValue = context.graphDiv.calcdata[0].map(d => [0, d.color]);
96+
}
97+
98+
if (
99+
props.attr === 'marker.colors' &&
100+
plotProps.fullValue &&
101+
Array.isArray(plotProps.fullValue) &&
102+
!plotProps.fullValue.every(el => Array.isArray(el))
103+
) {
104+
plotProps.fullValue = plotProps.fullValue.map(c => [0, c]);
105+
}
106+
107+
if (context.container.type === 'pie' && context.traceIndexes.length > 1) {
108+
plotProps.isVisible = false;
109+
}
68110
},
69111
});

src/components/fields/MarkerColor.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Field from './Field';
22
import PropTypes from 'prop-types';
33
import React, {Component, Fragment} from 'react';
4-
import {connectToContainer} from 'lib';
4+
import {adjustColorscale, connectToContainer} from 'lib';
55
import RadioBlocks from '../widgets/RadioBlocks';
66
import Color from './Color';
77
import Colorscale from './Colorscale';
@@ -11,7 +11,6 @@ import Info from './Info';
1111
import DataSelector from './DataSelector';
1212
import VisibilitySelect from './VisibilitySelect';
1313
import {MULTI_VALUED, COLORS} from 'lib/constants';
14-
import {getColorscale} from 'react-colorscales';
1514

1615
class UnconnectedMarkerColor extends Component {
1716
constructor(props, context) {
@@ -107,26 +106,14 @@ class UnconnectedMarkerColor extends Component {
107106
let adjustedColors = colors;
108107

109108
if (colorscaleType !== 'categorical') {
110-
adjustedColors = getColorscale(
111-
colors,
112-
numberOfTraces,
113-
null,
114-
null,
115-
colorscaleType
116-
);
109+
adjustedColors = adjustColorscale(colors, numberOfTraces);
117110
}
118111

119112
if (
120113
adjustedColors.every(c => c === adjustedColors[0]) ||
121114
colorscaleType === 'categorical'
122115
) {
123-
const repetitions = Math.ceil(numberOfTraces / colors.length);
124-
const newArray = new Array(repetitions).fill(colors);
125-
adjustedColors = newArray
126-
.reduce((a, b) => {
127-
return a.concat(b);
128-
}, [])
129-
.slice(0, numberOfTraces);
116+
adjustedColors = adjustColorscale(colors, numberOfTraces, {repeat: true});
130117
}
131118

132119
const updates = adjustedColors.map(color => ({

src/default_panels/GraphCreatePanel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const GraphCreatePanel = (props, {localize: _}) => {
2828
<PlotlySection name={_('Data')}>
2929
<DataSelector label={_('Latitude')} attr="lat" />
3030
<DataSelector label={_('Longitude')} attr="lon" />
31-
<DataSelector label={_('Labels')} attr="labels" />
3231
<DataSelector label={_('Values')} attr="values" />
32+
<DataSelector label={_('Labels')} attr="labels" />
3333
<DataSelector label={_('Locations')} attr="locations" />
3434
<Radio
3535
label={_('Orientation')}

src/default_panels/StyleTracesPanel.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ const StyleTracesPanel = (props, {localize: _}) => (
4848
/>
4949
<NumericFraction label={_('Opacity')} attr="opacity" />
5050
<ColorPicker label={_('Color')} attr="color" />
51+
<ColorscalePicker
52+
label={_('Colors')}
53+
attr="marker.colors"
54+
initialCategory="categorical"
55+
/>
5156
<Dropdown
5257
label={_('Histogram Normalization')}
5358
options={[

src/lib/customTraceType.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ export function traceTypeToPlotlyInitFigure(traceType, gl = '') {
7676
type: 'scatter3d',
7777
mode: 'markers',
7878
};
79+
case 'pie':
80+
return {
81+
marker: {
82+
colors: [],
83+
},
84+
type: 'pie',
85+
};
7986
default:
8087
return {type: traceType};
8188
}

src/lib/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
camelCase,
3939
pascalCase,
4040
} from './strings';
41+
import {getColorscale} from 'react-colorscales';
4142

4243
const TOO_LIGHT_FACTOR = 0.8;
4344

@@ -175,7 +176,33 @@ function maybeAdjustSrc(src, srcAttributePath, traceType, config) {
175176
return config && config.fromSrc ? config.fromSrc(src, traceType) : src;
176177
}
177178

179+
function adjustColorscale(
180+
colorscale,
181+
numberOfNeededColors,
182+
colorscaleType,
183+
config
184+
) {
185+
if (config.repeat) {
186+
const repetitions = Math.ceil(numberOfNeededColors / colorscale.length);
187+
const newArray = new Array(repetitions).fill(colorscale);
188+
return newArray
189+
.reduce((a, b) => {
190+
return a.concat(b);
191+
}, [])
192+
.slice(0, numberOfNeededColors);
193+
}
194+
195+
return getColorscale(
196+
colorscale,
197+
numberOfNeededColors,
198+
null,
199+
null,
200+
colorscaleType
201+
);
202+
}
203+
178204
export {
205+
adjustColorscale,
179206
axisIdToAxisName,
180207
bem,
181208
capitalize,

0 commit comments

Comments
 (0)