Skip to content

Commit da720aa

Browse files
committed
improve scatterpolar hover
1 parent a5a719c commit da720aa

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

src/plots/polar/layout_defaults.js

+3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ function setConvertAngular(ax) {
221221
// angularaxis 'position' and/or 'direction'
222222
ax.unTransformRad = unTransformRad;
223223

224+
// this version is used on hover
225+
ax._c2rad = _c2rad;
226+
224227
ax.c2rad = function(v, unit) { return transformRad(_c2rad(v, unit)); };
225228
ax.rad2c = function(v, unit) { return _rad2c(unTransformRad(v), unit); };
226229

src/plots/polar/polar.js

+11
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,17 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {
435435
.call(Color.stroke, angularLayout.linecolor);
436436
};
437437

438+
proto.isPtWithinSector = function() {
439+
var sector = this.sector;
440+
441+
if(isFullCircle(sector)) return true;
442+
443+
// check out https://stackoverflow.com/a/13675772/4068492
444+
// for possible solution
445+
// var deg = wrap360(rad2deg(d.rad));
446+
return true;
447+
};
448+
438449
function setScale(ax, axLayout, fullLayout) {
439450
Axes.setConvert(ax, fullLayout);
440451

src/traces/scatterpolar/hover.js

+46
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,58 @@
99
'use strict';
1010

1111
var scatterHover = require('../scatter/hover');
12+
var Axes = require('../../plots/cartesian/axes');
13+
var Lib = require('../../lib');
1214

1315
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
1416
var scatterPointData = scatterHover(pointData, xval, yval, hovermode);
1517
if(!scatterPointData || scatterPointData[0].index === false) return;
1618

1719
var newPointData = scatterPointData[0];
1820

21+
// hovering on fill case
22+
// TODO do we need to constrain the scatter point data further (like for
23+
// ternary subplots) or not?
24+
if(newPointData.index === undefined) {
25+
return scatterPointData;
26+
}
27+
28+
var subplot = pointData.subplot;
29+
var cdi = newPointData.cd[newPointData.index];
30+
31+
if(!subplot.isPtWithinSector(cdi)) return;
32+
33+
newPointData.xLabelVal = undefined;
34+
newPointData.yLabelVal = undefined;
35+
36+
var trace = newPointData.trace;
37+
var radialAxis = subplot.radialAxis;
38+
var angularAxis = subplot.angularAxis;
39+
var hoverinfo = cdi.hi || trace.hoverinfo;
40+
var parts = hoverinfo.split('+');
41+
var text = [];
42+
var rad = angularAxis._c2rad(cdi.theta, trace.thetaunit);
43+
44+
radialAxis._hovertitle = 'r';
45+
angularAxis._hovertitle = 'θ';
46+
47+
// show theta value in unit of angular axis
48+
var theta;
49+
if(angularAxis.type === 'linear' && trace.thetaunit !== angularAxis.thetaunit) {
50+
theta = angularAxis.thetaunit === 'degrees' ? Lib.rad2deg(rad) : rad;
51+
} else {
52+
theta = cdi.theta;
53+
}
54+
55+
function textPart(ax, val) {
56+
text.push(ax._hovertitle + ': ' + Axes.tickText(ax, val, 'hover').text);
57+
}
58+
59+
if(parts.indexOf('all') !== -1) parts = ['r', 'theta'];
60+
if(parts.indexOf('r') !== -1) textPart(radialAxis, cdi.r);
61+
if(parts.indexOf('theta') !== -1) textPart(angularAxis, theta);
62+
63+
newPointData.extraText = text.join('<br>');
64+
1965
return scatterPointData;
2066
};

src/traces/scatterpolar/plot.js

+32
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
'use strict';
1010

11+
var Lib = require('../../lib');
1112
var scatterPlot = require('../scatter/plot');
1213

1314
module.exports = function plot(subplot, moduleCalcData) {
@@ -23,4 +24,35 @@ module.exports = function plot(subplot, moduleCalcData) {
2324
};
2425

2526
scatterPlot(subplot.graphDiv, plotinfo, moduleCalcData);
27+
28+
function pt2deg(p) {
29+
return Lib.rad2deg(Math.atan2(radius - p[1], p[0] - radius));
30+
}
31+
32+
// TODO
33+
// fix polygon testers for segments that wrap around themselves
34+
// about the origin.
35+
for(var i = 0; i < moduleCalcData.length; i++) {
36+
var trace = moduleCalcData[i][0].trace;
37+
38+
if(Array.isArray(trace._polygons)) {
39+
for(var j = 0; j < trace._polygons.length; j++) {
40+
var pts = trace._polygons[j].pts.slice();
41+
pts.pop();
42+
43+
var a0 = pt2deg(pts[0]);
44+
for(var k = 1; k < pts.length; k++) {
45+
var a1 = pt2deg(pts[k]);
46+
var arc = Math.abs(a1 - a0);
47+
var arcWrapped = Math.abs(Lib.wrap360(a1) - Lib.wrap360(a0));
48+
49+
if(arc !== arcWrapped) {
50+
// pts.push(radius, radius);
51+
}
52+
53+
a0 = a1;
54+
}
55+
}
56+
}
57+
}
2658
};

test/jasmine/tests/scatterpolar_test.js

+8
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ describe('Test scatterpolar trace defaults:', function() {
3434
describe('Test scatterpolar calc:', function() {
3535

3636
});
37+
38+
describe('Test scatterpolar hover:', function() {
39+
// gd;
40+
41+
it('', function() {
42+
43+
});
44+
});

0 commit comments

Comments
 (0)