Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add barpolar traces #2954

Merged
merged 32 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
347979c
expose setGroupPositions from bar/set_positions.js
etpinard Aug 2, 2018
8ca4101
don't set undefined class names via Lib.ensureSingle
etpinard Aug 28, 2018
52ed444
:hocho: now useless _module.style loop
etpinard Aug 15, 2018
d35d13b
DRY up polar mock cartesian axis 'range' logic
etpinard Aug 15, 2018
eb9307e
rename Polar.prototype.isPtWithinSector -> isPtInside
etpinard Aug 24, 2018
4d20388
move a few things to lib/angles.js + add isPtInsideSector
etpinard Aug 24, 2018
95119c4
move polar polygon logic to polar/helper.js
etpinard Aug 24, 2018
facb2f8
mv arc/sector/annular path fn to angles.js
etpinard Aug 24, 2018
5ea3d52
update polar baselines post pathSector/pathArc cleanup
etpinard Aug 27, 2018
a328b1a
add barpolar :tada:
etpinard Jul 24, 2018
dcdd6b5
fix 'date' radial polar axes
etpinard Aug 28, 2018
6edd45e
:lock: barpolar on funky subplots
etpinard Aug 28, 2018
7ae3b7c
bullet-proof barpolar hover
etpinard Aug 28, 2018
0f62c0f
add barpolar mock to svg mock list
etpinard Aug 28, 2018
235b425
add barpolar hover tests
etpinard Aug 28, 2018
6b5895f
add barpolar select test
etpinard Aug 23, 2018
296f088
fixup jsDoc header
etpinard Aug 27, 2018
a32484a
do not .classed() layers in ensureSingle if className not given
etpinard Aug 31, 2018
e938974
standardize API of polar internal routine
etpinard Aug 31, 2018
cd05d3d
confirm use of tip-of-bar/mid-angle pt for selection and hover labels
etpinard Aug 31, 2018
971c6f6
make reversed-radial-axis hover logic more readable
etpinard Aug 31, 2018
e6c4dad
align hover label to the left for negative radial coords
etpinard Aug 31, 2018
01e15b4
improve barpolar attribute descriptions
etpinard Aug 31, 2018
160f742
:hocho: comment of rounding up bar borders
etpinard Aug 31, 2018
6494302
change polar bargap 0.2 -> 0.2
etpinard Sep 4, 2018
183b734
set miter-limit to 2 + :lock: in polar_bar-overlay mock
etpinard Sep 4, 2018
452e792
compute bar corners p0/p1/s0/s1 in crossTraceCalc
etpinard Sep 5, 2018
e792bc8
add 1e-15 tolerance in isFullCircle
etpinard Sep 5, 2018
8575b27
replace wrap360 with Lib.mod & wrap180 with new Lib.modHalf
etpinard Sep 5, 2018
939bdad
use mod to compute angle delta
etpinard Sep 5, 2018
d2484f3
make modHalf restrict output to "half" of Math.abs(input)
etpinard Sep 5, 2018
235fe5b
fixup doc for Lib.modHalf
etpinard Sep 5, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/barpolar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

module.exports = require('../src/traces/barpolar');
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Plotly.register([
require('./candlestick'),

require('./scatterpolar'),
require('./scatterpolargl')
require('./scatterpolargl'),
require('./barpolar')
]);

// transforms
Expand Down
227 changes: 218 additions & 9 deletions src/lib/angles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,234 @@

var PI = Math.PI;

exports.deg2rad = function(deg) {
function deg2rad(deg) {
return deg / 180 * PI;
};
}

exports.rad2deg = function(rad) {
function rad2deg(rad) {
return rad / PI * 180;
};
}

exports.wrap360 = function(deg) {
function wrap360(deg) {
var out = deg % 360;
return out < 0 ? out + 360 : out;
};
}

exports.wrap180 = function(deg) {
function wrap180(deg) {
if(Math.abs(deg) > 180) deg -= Math.round(deg / 360) * 360;
return deg;
};
}

exports.isFullCircle = function(sector) {
/* is sector a full circle?
* ... this comes up a lot in SVG path-drawing routines
*
* @param {2-item array} sector sector angles in *degrees*
* @return {boolean}
*/
function isFullCircle(sector) {
var arc = Math.abs(sector[1] - sector[0]);
return arc === 360;
}

/* angular delta between angle 'a' and 'b'
*
* solution taken from: https://stackoverflow.com/a/2007279
*
* @param {number} a : first angle in *radians*
* @param {number} b : second angle in *radians*
* @return {number} angular delta in *radians*
*/
function angleDelta(a, b) {
var d = b - a;
return Math.atan2(Math.sin(d), Math.cos(d));
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
}

/* angular distance between angle 'a' and 'b'
*
* @param {number} a : first angle in *radians*
* @param {number} b : second angle in *radians*
* @return {number} angular distance in *radians*
*/
function angleDist(a, b) {
return Math.abs(angleDelta(a, b));
}

/* is angle inside sector?
*
* @param {number} a : angle to test in *radians*
* @param {2-item array} sector : sector angles in *degrees*
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean}
*/
function isAngleInsideSector(a, sector) {
if(isFullCircle(sector)) return true;

var s0, s1;

if(sector[0] < sector[1]) {
s0 = sector[0];
s1 = sector[1];
} else {
s0 = sector[1];
s1 = sector[0];
}

s0 = wrap360(s0);
s1 = wrap360(s1);
if(s0 > s1) s1 += 360;

var a0 = wrap360(rad2deg(a));
var a1 = a0 + 360;

return (a0 >= s0 && a0 <= s1) || (a1 >= s0 && a1 <= s1);
}

/* is pt (r,a) inside sector?
*
* @param {number} r : pt's radial coordinate
* @param {number} a : pt's angular coordinate in *radians*
* @param {2-item array} rRng : sector's radial range
* @param {2-item array} sector : sector angles in *degrees*
* @return {boolean}
*/
function isPtInsideSector(r, a, rRng, sector) {
if(!isAngleInsideSector(a, sector)) return false;

var r0, r1;

if(rRng[0] < rRng[1]) {
r0 = rRng[0];
r1 = rRng[1];
} else {
r0 = rRng[1];
r1 = rRng[0];
}

return r >= r0 && r <= r1;
}

// common to pathArc, pathSector and pathAnnulus
function _path(r0, r1, a0, a1, cx, cy, isClosed) {
cx = cx || 0;
cy = cy || 0;

var isCircle = isFullCircle([a0, a1].map(rad2deg));
var aStart, aMid, aEnd;
var rStart, rEnd;

if(isCircle) {
aStart = 0;
aMid = PI;
aEnd = 2 * PI;
} else {
if(a0 < a1) {
aStart = a0;
aEnd = a1;
} else {
aStart = a1;
aEnd = a0;
}
}

if(r0 < r1) {
rStart = r0;
rEnd = r1;
} else {
rStart = r1;
rEnd = r0;
}

// N.B. svg coordinates here, where y increases downward
function pt(r, a) {
return [r * Math.cos(a) + cx, cy - r * Math.sin(a)];
}

var largeArc = Math.abs(aEnd - aStart) <= PI ? 0 : 1;
function arc(r, a, cw) {
return 'A' + [r, r] + ' ' + [0, largeArc, cw] + ' ' + pt(r, a);
}

var p;

if(isCircle) {
if(rStart === null) {
p = 'M' + pt(rEnd, aStart) +
arc(rEnd, aMid, 0) +
arc(rEnd, aEnd, 0) + 'Z';
} else {
p = 'M' + pt(rStart, aStart) +
arc(rStart, aMid, 0) +
arc(rStart, aEnd, 0) + 'Z' +
'M' + pt(rEnd, aStart) +
arc(rEnd, aMid, 1) +
arc(rEnd, aEnd, 1) + 'Z';
}
} else {
if(rStart === null) {
p = 'M' + pt(rEnd, aStart) + arc(rEnd, aEnd, 0);
if(isClosed) p += 'L0,0Z';
} else {
p = 'M' + pt(rStart, aStart) +
'L' + pt(rEnd, aStart) +
arc(rEnd, aEnd, 0) +
'L' + pt(rStart, aEnd) +
arc(rStart, aStart, 1) + 'Z';
}
}

return p;
}

/* path an arc
*
* @param {number} r : radius
* @param {number} a0 : first angular coordinate
* @param {number} a1 : second angular coordinate
* @param {number (optional)} cx : x coordinate of center
* @param {number (optional)} cy : y coordinate of center
* @return {string} svg path
*/
function pathArc(r, a0, a1, cx, cy) {
return _path(null, r, a0, a1, cx, cy, 0);
}

/* path a sector
*
* @param {number} r : radius
* @param {number} a0 : first angular coordinate
* @param {number} a1 : second angular coordinate
* @param {number (optional)} cx : x coordinate of center
* @param {number (optional)} cy : y coordinate of center
* @return {string} svg path
*/
function pathSector(r, a0, a1, cx, cy) {
return _path(null, r, a0, a1, cx, cy, 1);
}

/* path an annulus
*
* @param {number} r0 : first radial coordinate
* @param {number} r1 : second radial coordinate
* @param {number} a0 : first angular coordinate
* @param {number} a1 : second angular coordinate
* @param {number (optional)} cx : x coordinate of center
* @param {number (optional)} cy : y coordinate of center
* @return {string} svg path
*/
function pathAnnulus(r0, r1, a0, a1, cx, cy) {
return _path(r0, r1, a0, a1, cx, cy, 1);
}

module.exports = {
deg2rad: deg2rad,
rad2deg: rad2deg,
wrap360: wrap360,
wrap180: wrap180,
angleDelta: angleDelta,
angleDist: angleDist,
isFullCircle: isFullCircle,
isAngleInsideSector: isAngleInsideSector,
isPtInsideSector: isPtInsideSector,
pathArc: pathArc,
pathSector: pathSector,
pathAnnulus: pathAnnulus
};
10 changes: 9 additions & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ lib.sorterAsc = searchModule.sorterAsc;
lib.sorterDes = searchModule.sorterDes;
lib.distinctVals = searchModule.distinctVals;
lib.roundUp = searchModule.roundUp;
lib.findIndexOfMin = searchModule.findIndexOfMin;

var statsModule = require('./stats');
lib.aggNums = statsModule.aggNums;
Expand All @@ -87,7 +88,14 @@ lib.deg2rad = anglesModule.deg2rad;
lib.rad2deg = anglesModule.rad2deg;
lib.wrap360 = anglesModule.wrap360;
lib.wrap180 = anglesModule.wrap180;
lib.angleDelta = anglesModule.angleDelta;
lib.angleDist = anglesModule.angleDist;
lib.isFullCircle = anglesModule.isFullCircle;
lib.isAngleInsideSector = anglesModule.isAngleInsideSector;
lib.isPtInsideSector = anglesModule.isPtInsideSector;
lib.pathArc = anglesModule.pathArc;
lib.pathSector = anglesModule.pathSector;
lib.pathAnnulus = anglesModule.pathAnnulus;

var geom2dModule = require('./geometry2d');
lib.segmentsIntersect = geom2dModule.segmentsIntersect;
Expand Down Expand Up @@ -729,7 +737,7 @@ lib.ensureSingle = function(parent, nodeType, className, enterFn) {
var sel = parent.select(nodeType + (className ? '.' + className : ''));
if(sel.size()) return sel;

var layer = parent.append(nodeType).classed(className, true);
var layer = parent.append(nodeType).classed(className || '', true);
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
if(enterFn) layer.call(enterFn);

return layer;
Expand Down
24 changes: 24 additions & 0 deletions src/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var isNumeric = require('fast-isnumeric');
var loggers = require('./loggers');
var identity = require('./identity');

// don't trust floating point equality - fraction of bin size to call
// "on the line" and ensure that they go the right way specified by
Expand Down Expand Up @@ -113,3 +114,26 @@ exports.roundUp = function(val, arrayIn, reverse) {
}
return arrayIn[low];
};

/* find index in array 'arr' that minimizes 'fn'
*
* @param {array} arr : array where to search
* @param {fn (optional)} fn : function to minimize,
* if not given, fn is the identity function
* @return {integer}
*/
exports.findIndexOfMin = function(arr, fn) {
fn = fn || identity;

var min = Infinity;
var ind;

for(var i = 0; i < arr.length; i++) {
var v = fn(arr[i]);
if(v < min) {
min = v;
ind = i;
}
}
return ind;
};
2 changes: 1 addition & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ function doCrossTraceCalc(gd) {
fullLayout[sp];

for(j = 0; j < methods.length; j++) {
methods[j](gd, spInfo);
methods[j](gd, spInfo, sp);
}
}
}
Expand Down
Loading