Skip to content

Commit

Permalink
Merge pull request #1896 from plotly/fix-pie-sankey-event-data
Browse files Browse the repository at this point in the history
Add plotly-esque point data info to pie & sankey event data
  • Loading branch information
etpinard committed Jul 19, 2017
2 parents 1879cf9 + 15dc324 commit 756bd69
Show file tree
Hide file tree
Showing 6 changed files with 625 additions and 497 deletions.
4 changes: 4 additions & 0 deletions src/traces/pie/plot.js
Expand Up @@ -77,6 +77,10 @@ module.exports = function plot(gd, cdpie) {
return;
}

// to have consistent event data compared to other traces
pt.pointNumber = pt.i;
pt.curveNumber = trace.index;

quadrants[pt.pxmid[1] < 0 ? 0 : 1][pt.pxmid[0] < 0 ? 0 : 1].push(pt);

var cx = cd0.cx + depthVector[0],
Expand Down
26 changes: 16 additions & 10 deletions src/traces/sankey/plot.js
Expand Up @@ -115,24 +115,26 @@ function castHoverOption(trace, attr) {
}

module.exports = function plot(gd, calcData) {

var fullLayout = gd._fullLayout;
var svg = fullLayout._paper;
var size = fullLayout._size;

var linkSelect = function(element, d) {
gd._hoverdata = [d.link];
gd._hoverdata.trace = calcData.trace;
var evt = d.link;
evt.originalEvent = d3.event;
gd._hoverdata = [evt];
Fx.click(gd, { target: true });
};

var linkHover = function(element, d, sankey) {
var evt = d.link;
evt.originalEvent = d3.event;
d3.select(element).call(linkHoveredStyle.bind(0, d, sankey, true));
Fx.hover(gd, d.link, 'sankey');
Fx.hover(gd, evt, 'sankey');
};

var linkHoverFollow = function(element, d) {
var trace = gd._fullData[d.traceId];
var trace = d.link.trace;
var rootBBox = gd.getBoundingClientRect();
var boundingBox = element.getBoundingClientRect();
var hoverCenterX = boundingBox.left + boundingBox.width / 2;
Expand Down Expand Up @@ -166,26 +168,30 @@ module.exports = function plot(gd, calcData) {
var linkUnhover = function(element, d, sankey) {
d3.select(element).call(linkNonHoveredStyle.bind(0, d, sankey, true));
gd.emit('plotly_unhover', {
event: d3.event,
points: [d.link]
});

Fx.loneUnhover(fullLayout._hoverlayer.node());
};

var nodeSelect = function(element, d, sankey) {
gd._hoverdata = [d.node];
gd._hoverdata.trace = calcData.trace;
var evt = d.node;
evt.originalEvent = d3.event;
gd._hoverdata = [evt];
d3.select(element).call(nodeNonHoveredStyle, d, sankey);
Fx.click(gd, { target: true });
};

var nodeHover = function(element, d, sankey) {
var evt = d.node;
evt.originalEvent = d3.event;
d3.select(element).call(nodeHoveredStyle, d, sankey);
Fx.hover(gd, d.node, 'sankey');
Fx.hover(gd, evt, 'sankey');
};

var nodeHoverFollow = function(element, d) {
var trace = gd._fullData[d.traceId];
var trace = d.node.trace;
var nodeRect = d3.select(element).select('.nodeRect');
var rootBBox = gd.getBoundingClientRect();
var boundingBox = nodeRect.node().getBoundingClientRect();
Expand Down Expand Up @@ -220,9 +226,9 @@ module.exports = function plot(gd, calcData) {
};

var nodeUnhover = function(element, d, sankey) {

d3.select(element).call(nodeNonHoveredStyle, d, sankey);
gd.emit('plotly_unhover', {
event: d3.event,
points: [d.node]
});

Expand Down
15 changes: 11 additions & 4 deletions src/traces/sankey/render.js
Expand Up @@ -65,7 +65,6 @@ function switchToSankeyFormat(nodes) {
// view models

function sankeyModel(layout, d, i) {

var trace = unwrap(d).trace,
domain = trace.domain,
nodeSpec = trace.node,
Expand All @@ -87,13 +86,15 @@ function sankeyModel(layout, d, i) {

var nodes = nodeSpec.label.map(function(l, i) {
return {
pointNumber: i,
label: l,
color: Lib.isArray(nodeSpec.color) ? nodeSpec.color[i] : nodeSpec.color
};
});

var links = linkSpec.value.map(function(d, i) {
return {
pointNumber: i,
label: linkSpec.label[i],
color: Lib.isArray(linkSpec.color) ? linkSpec.color[i] : linkSpec.color,
source: linkSpec.source[i],
Expand Down Expand Up @@ -121,6 +122,7 @@ function sankeyModel(layout, d, i) {

return {
key: i,
trace: trace,
guid: Math.floor(1e12 * (1 + Math.random())),
horizontal: horizontal,
width: width,
Expand Down Expand Up @@ -150,13 +152,16 @@ function sankeyModel(layout, d, i) {
}

function linkModel(uniqueKeys, d, l) {

var tc = tinycolor(l.color);
var basicKey = l.source.label + '|' + l.target.label;
var foundKey = uniqueKeys[basicKey];
uniqueKeys[basicKey] = (foundKey === void(0) ? foundKey : 0) + 1;
var key = basicKey + (foundKey === void(0) ? '' : '__' + foundKey);

// for event data
l.trace = d.trace;
l.curveNumber = d.trace.index;

return {
key: key,
traceId: d.key,
Expand All @@ -173,7 +178,6 @@ function linkModel(uniqueKeys, d, l) {
}

function nodeModel(uniqueKeys, d, n) {

var tc = tinycolor(n.color),
zoneThicknessPad = c.nodePadAcross,
zoneLengthPad = d.nodePad / 2,
Expand All @@ -185,6 +189,10 @@ function nodeModel(uniqueKeys, d, n) {
uniqueKeys[basicKey] = (foundKey === void(0) ? foundKey : 0) + 1;
var key = basicKey + (foundKey === void(0) ? '' : '__' + foundKey);

// for event data
n.trace = d.trace;
n.curveNumber = d.trace.index;

return {
key: key,
traceId: d.key,
Expand Down Expand Up @@ -416,7 +424,6 @@ function snappingForce(sankeyNode, forceKey, nodes, d) {
// scene graph

module.exports = function(svg, styledData, layout, callbacks) {

var sankey = svg.selectAll('.sankey')
.data(styledData
.filter(function(d) {return unwrap(d).trace.visible;})
Expand Down

0 comments on commit 756bd69

Please sign in to comment.