Skip to content

Commit ec8c373

Browse files
committed
unified hoverlabel: style legend item using marker's style
1 parent 609d5ca commit ec8c373

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

src/components/fx/hover.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -953,14 +953,27 @@ function createHoverText(hoverData, opts, gd) {
953953
var texts = getHoverLabelText(hoverData[j], true, hovermode, fullLayout, t0);
954954
var text = texts[0];
955955
var name = texts[1];
956-
hoverData[j].name = name;
956+
var pt = hoverData[j];
957+
pt.name = name;
957958
if(name !== '') {
958-
hoverData[j].text = name + ' : ' + text;
959+
pt.text = name + ' : ' + text;
959960
} else {
960-
hoverData[j].text = text;
961+
pt.text = text;
961962
}
962963

963-
legendOpts.entries.push([hoverData[j]]);
964+
// pass through marker's calcdata to style legend items
965+
var cd = pt.cd[pt.index];
966+
if(cd) {
967+
if(cd.mc) pt.mc = cd.mc;
968+
if(cd.mcc) pt.mc = cd.mcc;
969+
if(cd.mlc) pt.mlc = cd.mlc;
970+
if(cd.mlcc) pt.mlc = cd.mlcc;
971+
if(cd.mlw) pt.mlw = cd.mlw;
972+
if(cd.mrc) pt.mrc = cd.mrc;
973+
}
974+
pt._distinct = true;
975+
976+
legendOpts.entries.push([pt]);
964977
}
965978
legendOpts.layer = container;
966979

src/components/legend/style.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ module.exports = function style(s, gd, legend) {
213213
return valToBound;
214214
}
215215

216-
function pickFirst(array) { return array[0]; }
216+
function pickFirst(array) {
217+
if(d0._distinct && d0.index && array[d0.index]) return array[d0.index];
218+
return array[0];
219+
}
217220

218221
// constrain text, markers, etc so they'll fit on the legend
219222
if(showMarkers || showText || showLines) {

test/jasmine/tests/hover_label_test.js

+78-3
Original file line numberDiff line numberDiff line change
@@ -3688,13 +3688,28 @@ describe('hovermode: (x|y)unified', function() {
36883688
});
36893689
}
36903690

3691-
function assertStyle(color) {
3691+
function assertBgcolor(color) {
36923692
var hoverLayer = d3.select('g.hoverlayer');
36933693
var hover = hoverLayer.select('g.legend');
36943694
var bg = hover.select('rect.bg');
36953695
expect(bg.node().style.fill).toBe(color);
36963696
}
36973697

3698+
function assertSymbol(exp) {
3699+
var hoverLayer = d3.select('g.hoverlayer');
3700+
var hover = hoverLayer.select('g.legend');
3701+
var traces = hover.selectAll('g.traces');
3702+
traces.each(function(d, i) {
3703+
var pts = d3.select(this).selectAll('g.legendpoints path');
3704+
pts.each(function() {
3705+
var node = d3.select(this).node();
3706+
expect(node.style.fill).toBe(exp[i][0], 'wrong fill for point ' + i);
3707+
expect(node.style.strokeWidth).toBe(exp[i][1], 'wrong stroke-width for point ' + i);
3708+
expect(node.style.stroke).toBe(exp[i][2], 'wrong stroke for point ' + i);
3709+
});
3710+
});
3711+
}
3712+
36983713
it('set smart defaults for spikeline in x unified', function(done) {
36993714
Plotly.newPlot(gd, [{y: [4, 6, 5]}], {'hovermode': 'x unified', 'xaxis': {'color': 'red'}})
37003715
.then(function(gd) {
@@ -3773,7 +3788,7 @@ describe('hovermode: (x|y)unified', function() {
37733788
.then(done);
37743789
});
37753790

3776-
it('should for finance traces', function(done) {
3791+
it('should work for finance traces', function(done) {
37773792
var mockOhlc = require('@mocks/finance_multicategory.json');
37783793
var mockCopy = Lib.extendDeep({}, mockOhlc);
37793794
mockCopy.layout.hovermode = 'x unified';
@@ -3790,6 +3805,66 @@ describe('hovermode: (x|y)unified', function() {
37903805
.then(done);
37913806
});
37923807

3808+
it('should style scatter symbols accordingly', function(done) {
3809+
var mock = require('@mocks/marker_colorscale_template.json');
3810+
var mockCopy = Lib.extendDeep({}, mock);
3811+
mockCopy.layout.hovermode = 'x unified';
3812+
Plotly.newPlot(gd, mockCopy)
3813+
.then(function(gd) {
3814+
_hover(gd, {xval: 1});
3815+
assertLabel({title: '1', items: ['2']});
3816+
assertSymbol([['rgb(33, 145, 140)', '0px', '']]);
3817+
})
3818+
.then(function() {
3819+
_hover(gd, {xval: 2});
3820+
assertLabel({title: '2', items: ['3']});
3821+
assertSymbol([['rgb(253, 231, 37)', '0px', '']]);
3822+
})
3823+
.catch(failTest)
3824+
.then(done);
3825+
});
3826+
3827+
it('should style bar symbols accordingly', function(done) {
3828+
var mock = require('@mocks/bar-marker-line-colorscales.json');
3829+
var mockCopy = Lib.extendDeep({}, mock);
3830+
mockCopy.layout.hovermode = 'x unified';
3831+
Plotly.newPlot(gd, mockCopy)
3832+
.then(function(gd) {
3833+
_hover(gd, {xval: 10});
3834+
assertLabel({title: '10', items: ['10']});
3835+
assertSymbol([['rgb(94, 216, 43)', '4px', 'rgb(197, 232, 190)']]);
3836+
})
3837+
.then(function() {
3838+
_hover(gd, {xval: 20});
3839+
assertLabel({title: '20', items: ['20']});
3840+
assertSymbol([['rgb(168, 140, 33)', '4px', 'rgb(111, 193, 115)']]);
3841+
})
3842+
.catch(failTest)
3843+
.then(done);
3844+
});
3845+
3846+
it('should style funnel symbols accordingly', function(done) {
3847+
var mock = require('@mocks/funnel_custom.json');
3848+
var mockCopy = Lib.extendDeep({}, mock);
3849+
mockCopy.layout.hovermode = 'x unified';
3850+
Plotly.newPlot(gd, mockCopy)
3851+
.then(function(gd) {
3852+
_hover(gd, {xval: 1});
3853+
// assertLabel({title: 'B', items: ['asdf', 'asdf']});
3854+
assertSymbol([
3855+
['rgb(0, 255, 0)', '0px', ''],
3856+
['rgb(255, 255, 0)', '5px', 'rgb(0, 0, 127)']
3857+
]);
3858+
})
3859+
.then(function() {
3860+
_hover(gd, {xval: 4});
3861+
// assertLabel({title: 'E', items: ['asdf', 'asdf']});
3862+
// assertSymbol([['rgb(168, 140, 33)', '4px', 'rgb(111, 193, 115)']]);
3863+
})
3864+
.catch(failTest)
3865+
.then(done);
3866+
});
3867+
37933868
it('label should have color of paper_bgcolor', function(done) {
37943869
var mockCopy = Lib.extendDeep({}, mock);
37953870
var bgcolor = 'rgb(15, 200, 85)';
@@ -3798,7 +3873,7 @@ describe('hovermode: (x|y)unified', function() {
37983873
.then(function(gd) {
37993874
_hover(gd, { xval: 3 });
38003875

3801-
assertStyle(bgcolor);
3876+
assertBgcolor(bgcolor);
38023877
})
38033878
.catch(failTest)
38043879
.then(done);

0 commit comments

Comments
 (0)