Skip to content

Commit 70f3f70

Browse files
authored
Merge pull request #2200 from plotly/polar
Polar 2.0
2 parents ef181da + 18a3a0d commit 70f3f70

File tree

101 files changed

+11751
-668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+11751
-668
lines changed

.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ dist
33
build
44

55
test/jasmine/assets/jquery-1.8.3.min.js
6-
src/plots/polar/micropolar.js
6+
src/plots/polar/legacy/micropolar.js

lib/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ Plotly.register([
4646
require('./contourcarpet'),
4747

4848
require('./ohlc'),
49-
require('./candlestick')
49+
require('./candlestick'),
50+
51+
require('./scatterpolar')
5052
]);
5153

5254
// transforms

lib/scatterpolar.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = require('../src/traces/scatterpolar');

src/components/calendars/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ module.exports = {
238238
// from yaxis if they only apply to x (rangeselector/rangeslider)
239239
yaxis: {calendar: axisAttrs},
240240
zaxis: {calendar: axisAttrs}
241+
},
242+
polar: {
243+
radialaxis: {calendar: axisAttrs}
241244
}
242245
},
243246
transforms: {

src/components/dragelement/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ dragElement.unhoverRaw = unhover.raw;
7676
* numClicks is how many clicks we've registered within
7777
* a doubleclick time
7878
* e is the original mousedown event
79+
* clampFn (optional, function(dx, dy) return [dx2, dy2])
80+
* Provide custom clamping function for small displacements.
81+
* By default, clamping is done using `minDrag` to x and y displacements
82+
* independently.
7983
*/
8084
dragElement.init = function init(options) {
8185
var gd = options.gd;
@@ -99,6 +103,14 @@ dragElement.init = function init(options) {
99103
element.onmousedown = onStart;
100104
element.ontouchstart = onStart;
101105

106+
function _clampFn(dx, dy, minDrag) {
107+
if(Math.abs(dx) < minDrag) dx = 0;
108+
if(Math.abs(dy) < minDrag) dy = 0;
109+
return [dx, dy];
110+
}
111+
112+
var clampFn = options.clampFn || _clampFn;
113+
102114
function onStart(e) {
103115
// make dragging and dragged into properties of gd
104116
// so that others can look at and modify them
@@ -145,12 +157,11 @@ dragElement.init = function init(options) {
145157

146158
function onMove(e) {
147159
var offset = pointerOffset(e);
148-
var dx = offset[0] - startX;
149-
var dy = offset[1] - startY;
150160
var minDrag = options.minDrag || constants.MINDRAG;
161+
var dxdy = clampFn(offset[0] - startX, offset[1] - startY, minDrag);
162+
var dx = dxdy[0];
163+
var dy = dxdy[1];
151164

152-
if(Math.abs(dx) < minDrag) dx = 0;
153-
if(Math.abs(dy) < minDrag) dy = 0;
154165
if(dx || dy) {
155166
gd._dragged = true;
156167
dragElement.unhover(gd);

src/components/modebar/manage.js

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function getButtonGroups(gd, buttonsToRemove, buttonsToAdd) {
8181
var hasGL2D = fullLayout._has('gl2d');
8282
var hasTernary = fullLayout._has('ternary');
8383
var hasMapbox = fullLayout._has('mapbox');
84+
var hasPolar = fullLayout._has('polar');
8485

8586
var groups = [];
8687

@@ -121,6 +122,9 @@ function getButtonGroups(gd, buttonsToRemove, buttonsToAdd) {
121122
if(hasMapbox || hasGeo) {
122123
dragModeGroup = ['pan2d'];
123124
}
125+
if(hasPolar) {
126+
dragModeGroup = ['zoom2d'];
127+
}
124128
if(isSelectable(fullData)) {
125129
dragModeGroup.push('select2d');
126130
dragModeGroup.push('lasso2d');

src/components/titles/index.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var numStripRE = / [XY][0-9]* /;
5151
* offset - shift up/down in the rotated frame (unused?)
5252
* containerGroup - if an svg <g> element already exists to hold this
5353
* title, include here. Otherwise it will go in fullLayout._infolayer
54+
*
55+
* @return {selection} d3 selection of title container group
5456
*/
5557
Titles.draw = function(gd, titleClass, options) {
5658
var cont = options.propContainer;
@@ -63,13 +65,14 @@ Titles.draw = function(gd, titleClass, options) {
6365
var group = options.containerGroup;
6466

6567
var fullLayout = gd._fullLayout;
66-
var font = cont.titlefont.family;
67-
var fontSize = cont.titlefont.size;
68-
var fontColor = cont.titlefont.color;
68+
var titlefont = cont.titlefont || {};
69+
var font = titlefont.family;
70+
var fontSize = titlefont.size;
71+
var fontColor = titlefont.color;
6972

7073
var opacity = 1;
7174
var isplaceholder = false;
72-
var txt = cont.title.trim();
75+
var txt = (cont.title || '').trim();
7376

7477
// only make this title editable if we positively identify its property
7578
// as one that has editing enabled.
@@ -111,17 +114,28 @@ Titles.draw = function(gd, titleClass, options) {
111114
.attr('class', titleClass);
112115
el.exit().remove();
113116

114-
if(!elShouldExist) return;
117+
if(!elShouldExist) return group;
115118

116119
function titleLayout(titleEl) {
117120
Lib.syncOrAsync([drawTitle, scootTitle], titleEl);
118121
}
119122

120123
function drawTitle(titleEl) {
121-
titleEl.attr('transform', transform ?
122-
'rotate(' + [transform.rotate, attributes.x, attributes.y] +
123-
') translate(0, ' + transform.offset + ')' :
124-
null);
124+
var transformVal;
125+
126+
if(transform) {
127+
transformVal = '';
128+
if(transform.rotate) {
129+
transformVal += 'rotate(' + [transform.rotate, attributes.x, attributes.y] + ')';
130+
}
131+
if(transform.offset) {
132+
transformVal += 'translate(0, ' + transform.offset + ')';
133+
}
134+
} else {
135+
transformVal = null;
136+
}
137+
138+
titleEl.attr('transform', transformVal);
125139

126140
titleEl.style({
127141
'font-family': font,
@@ -236,4 +250,6 @@ Titles.draw = function(gd, titleClass, options) {
236250
});
237251
}
238252
el.classed('js-placeholder', isplaceholder);
253+
254+
return group;
239255
};

src/lib/angles.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var PI = Math.PI;
12+
13+
exports.deg2rad = function(deg) {
14+
return deg / 180 * PI;
15+
};
16+
17+
exports.rad2deg = function(rad) {
18+
return rad / PI * 180;
19+
};
20+
21+
exports.wrap360 = function(deg) {
22+
var out = deg % 360;
23+
return out < 0 ? out + 360 : out;
24+
};
25+
26+
exports.wrap180 = function(deg) {
27+
if(Math.abs(deg) > 180) deg -= Math.round(deg / 360) * 360;
28+
return deg;
29+
};

src/lib/coerce.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var colorscaleNames = Object.keys(require('../components/colorscale/scales'));
1818
var nestedProperty = require('./nested_property');
1919
var counterRegex = require('./regex').counter;
2020
var DESELECTDIM = require('../constants/interactions').DESELECTDIM;
21+
var wrap180 = require('./angles').wrap180;
2122

2223
exports.valObjectMeta = {
2324
data_array: {
@@ -182,10 +183,7 @@ exports.valObjectMeta = {
182183
coerceFunction: function(v, propOut, dflt) {
183184
if(v === 'auto') propOut.set('auto');
184185
else if(!isNumeric(v)) propOut.set(dflt);
185-
else {
186-
if(Math.abs(v) > 180) v -= Math.round(v / 360) * 360;
187-
propOut.set(+v);
188-
}
186+
else propOut.set(wrap180(+v));
189187
}
190188
},
191189
subplotid: {

src/lib/filter_visible.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
/** Filter out object items with visible !== true
@@ -17,13 +16,30 @@
1716
*
1817
*/
1918
module.exports = function filterVisible(container) {
19+
var filterFn = isCalcData(container) ? calcDataFilter : baseFilter;
2020
var out = [];
2121

2222
for(var i = 0; i < container.length; i++) {
2323
var item = container[i];
24-
25-
if(item.visible === true) out.push(item);
24+
if(filterFn(item)) out.push(item);
2625
}
2726

2827
return out;
2928
};
29+
30+
function baseFilter(item) {
31+
return item.visible === true;
32+
}
33+
34+
function calcDataFilter(item) {
35+
return item[0].trace.visible === true;
36+
}
37+
38+
function isCalcData(cont) {
39+
return (
40+
Array.isArray(cont) &&
41+
Array.isArray(cont[0]) &&
42+
cont[0][0] &&
43+
cont[0][0].trace
44+
);
45+
}

src/lib/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ lib.rotationXYMatrix = matrixModule.rotationXYMatrix;
7777
lib.apply2DTransform = matrixModule.apply2DTransform;
7878
lib.apply2DTransform2 = matrixModule.apply2DTransform2;
7979

80+
var anglesModule = require('./angles');
81+
lib.deg2rad = anglesModule.deg2rad;
82+
lib.rad2deg = anglesModule.rad2deg;
83+
lib.wrap360 = anglesModule.wrap360;
84+
lib.wrap180 = anglesModule.wrap180;
85+
8086
var geom2dModule = require('./geometry2d');
8187
lib.segmentsIntersect = geom2dModule.segmentsIntersect;
8288
lib.segmentDistance = geom2dModule.segmentDistance;

src/plot_api/plot_api.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var Queue = require('../lib/queue');
2222
var Registry = require('../registry');
2323
var PlotSchema = require('./plot_schema');
2424
var Plots = require('../plots/plots');
25-
var Polar = require('../plots/polar');
25+
var Polar = require('../plots/polar/legacy');
2626
var initInteractions = require('../plots/cartesian/graph_interact');
2727

2828
var Drawing = require('../components/drawing');
@@ -144,8 +144,11 @@ Plotly.plot = function(gd, data, layout, config) {
144144

145145
var fullLayout = gd._fullLayout;
146146

147-
// Polar plots
148-
if(data && data[0] && data[0].r) return plotPolar(gd, data, layout);
147+
// Legacy polar plots
148+
if(!fullLayout._has('polar') && data && data[0] && data[0].r) {
149+
Lib.log('Legacy polar charts are deprecated!');
150+
return plotPolar(gd, data, layout);
151+
}
149152

150153
// so we don't try to re-call Plotly.plot from inside
151154
// legend and colorbar, if margins changed
@@ -1941,6 +1944,16 @@ function _relayout(gd, aobj) {
19411944
ax.range = (ax.range[1] > ax.range[0]) ? [1, 2] : [2, 1];
19421945
}
19431946

1947+
// clear polar view initial stash for radial range so that
1948+
// value get recomputed in correct units
1949+
if(Array.isArray(fullLayout._subplots.polar) &&
1950+
fullLayout._subplots.polar.length &&
1951+
fullLayout[p.parts[0]] &&
1952+
p.parts[1] === 'radialaxis'
1953+
) {
1954+
delete fullLayout[p.parts[0]]._subplot.viewInitial['radialaxis.range'];
1955+
}
1956+
19441957
// Annotations and images also need to convert to/from linearized coords
19451958
// Shapes do not need this :)
19461959
Registry.getComponentMethod('annotations', 'convertCoords')(gd, parentFull, vi, doextra);
@@ -2865,6 +2878,9 @@ function makePlotFramework(gd) {
28652878
// single cartesian layer for the whole plot
28662879
fullLayout._cartesianlayer = fullLayout._paper.append('g').classed('cartesianlayer', true);
28672880

2881+
// single polar layer for the whole plot
2882+
fullLayout._polarlayer = fullLayout._paper.append('g').classed('polarlayer', true);
2883+
28682884
// single ternary layer for the whole plot
28692885
fullLayout._ternarylayer = fullLayout._paper.append('g').classed('ternarylayer', true);
28702886

src/plot_api/plot_schema.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ var frameAttributes = require('../plots/frame_attributes');
1818
var animationAttributes = require('../plots/animation_attributes');
1919

2020
// polar attributes are not part of the Registry yet
21-
var polarAreaAttrs = require('../plots/polar/area_attributes');
22-
var polarAxisAttrs = require('../plots/polar/axis_attributes');
21+
var polarAreaAttrs = require('../plots/polar/legacy/area_attributes');
22+
var polarAxisAttrs = require('../plots/polar/legacy/axis_attributes');
2323

2424
var editTypes = require('./edit_types');
2525

0 commit comments

Comments
 (0)