Skip to content

Commit f5bff69

Browse files
authored
Fix FigureWidget selection behaviour of histograms (#2711)
* fixes selection behaviour of histogram plots * non ES6 reduce and every * moved variable assignemnt of flatPointIndex inside of if statement * removed line at the end of the last if statement * sorting of point indices
1 parent fb84751 commit f5bff69

File tree

1 file changed

+38
-9
lines changed
  • packages/javascript/plotlywidget/src

1 file changed

+38
-9
lines changed

Diff for: packages/javascript/plotlywidget/src/Figure.js

+38-9
Original file line numberDiff line numberDiff line change
@@ -888,18 +888,47 @@ var FigureView = widgets.DOMWidgetView.extend({
888888
// Most cartesian plots
889889
var pointObjects = data["points"];
890890
var numPoints = pointObjects.length;
891+
892+
var hasNestedPointObjects = true;
893+
for (let i = 0; i < numPoints; i++) {
894+
hasNestedPointObjects = (hasNestedPointObjects && pointObjects[i].hasOwnProperty("pointNumbers"));
895+
if (!hasNestedPointObjects) break;
896+
}
897+
var numPointNumbers = numPoints;
898+
if (hasNestedPointObjects) {
899+
numPointNumbers = 0;
900+
for (let i = 0; i < numPoints; i++) {
901+
numPointNumbers += pointObjects[i]["pointNumbers"].length;
902+
}
903+
}
891904
pointsObject = {
892-
trace_indexes: new Array(numPoints),
893-
point_indexes: new Array(numPoints),
894-
xs: new Array(numPoints),
895-
ys: new Array(numPoints),
905+
trace_indexes: new Array(numPointNumbers),
906+
point_indexes: new Array(numPointNumbers),
907+
xs: new Array(numPointNumbers),
908+
ys: new Array(numPointNumbers),
896909
};
897910

898-
for (var p = 0; p < numPoints; p++) {
899-
pointsObject["trace_indexes"][p] = pointObjects[p]["curveNumber"];
900-
pointsObject["point_indexes"][p] = pointObjects[p]["pointNumber"];
901-
pointsObject["xs"][p] = pointObjects[p]["x"];
902-
pointsObject["ys"][p] = pointObjects[p]["y"];
911+
if (hasNestedPointObjects) {
912+
var flatPointIndex = 0;
913+
for (var p = 0; p < numPoints; p++) {
914+
for (let i = 0; i < pointObjects[p]["pointNumbers"].length; i++, flatPointIndex++) {
915+
pointsObject["point_indexes"][flatPointIndex] = pointObjects[p]["pointNumbers"][i]
916+
// also add xs, ys and traces so that the array doesn't get truncated later
917+
pointsObject["xs"][flatPointIndex] = pointObjects[p]["x"];
918+
pointsObject["ys"][flatPointIndex] = pointObjects[p]["y"];
919+
pointsObject["trace_indexes"][flatPointIndex] = pointObjects[p]["curveNumber"];
920+
}
921+
}
922+
pointsObject["point_indexes"].sort(function(a, b) {
923+
return a - b;
924+
});
925+
} else {
926+
for (var p = 0; p < numPoints; p++) {
927+
pointsObject["trace_indexes"][p] = pointObjects[p]["curveNumber"];
928+
pointsObject["point_indexes"][p] = pointObjects[p]["pointNumber"];
929+
pointsObject["xs"][p] = pointObjects[p]["x"];
930+
pointsObject["ys"][p] = pointObjects[p]["y"];
931+
}
903932
}
904933

905934
// Add z if present

0 commit comments

Comments
 (0)