Skip to content

Commit 40b8583

Browse files
committed
More careful extraction of pointNumber/pointNumbers in plotly.js event data
as it turns out, 0 || 1 === 1, not 0. Closes #1286
1 parent 21871fa commit 40b8583

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

inst/htmlwidgets/plotly.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,15 @@ HTMLWidgets.widget({
360360
_isSimpleKey: trace._isSimpleKey
361361
};
362362

363+
// Use pointNumber by default, but aggregated traces should emit pointNumbers
364+
var ptNum = points[i].pointNumber;
365+
var hasPtNum = typeof ptNum === "number";
366+
var ptNum = hasPtNum ? ptNum : points[i].pointNumbers;
367+
363368
// selecting a point of a "simple" trace means: select the
364369
// entire key attached to this trace, which is useful for,
365370
// say clicking on a fitted line to select corresponding observations
366-
var pts = points[i].pointNumber || points[i].pointNumbers;
367-
var key = trace._isSimpleKey ? trace.key : Array.isArray(pts) ? pts.map(function(idx) { return trace.key[idx]; }) : trace.key[pts];
371+
var key = trace._isSimpleKey ? trace.key : Array.isArray(ptNum) ? ptNum.map(function(idx) { return trace.key[idx]; }) : trace.key[ptNum];
368372
// http://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript
369373
var keyFlat = trace._isNestedKey ? [].concat.apply([], key) : key;
370374

@@ -443,6 +447,8 @@ HTMLWidgets.widget({
443447
}
444448
}
445449

450+
//console.log(e.value);
451+
446452
// accumulate history for persistent selection
447453
if (!x.highlight.persistent) {
448454
selectionHistory = [event];
@@ -467,7 +473,9 @@ HTMLWidgets.widget({
467473
// Set a crosstalk variable selection value, triggering an update
468474
var turnOn = function(e) {
469475
if (e) {
476+
470477
var selectedKeys = pointsToKeys(e.points);
478+
console.log(selectedKeys);
471479
// Keys are group names, values are array of selected keys from group.
472480
for (var set in selectedKeys) {
473481
if (selectedKeys.hasOwnProperty(set)) {

0 commit comments

Comments
 (0)