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 spikelines #2247

Merged
merged 12 commits into from
Jan 17, 2018
Merged

Add spikelines #2247

merged 12 commits into from
Jan 17, 2018

Conversation

apalchys
Copy link
Contributor

This PR adds support of spikelines in any hovermode.

Also some new properties were added:

  • spikedistance and hoverdistance to control the range of appearing of spikelines and hoverlabels;
  • spikesnap to control the placement of the spikelines point.

Note: only the cartesian type of axis supports new functionality.

Relates to #2155

@etpinard
Copy link
Contributor

I'll review this tomorrow am (UTC-5). Sorry for the wait.

@etpinard etpinard added bug something broken feature something new status: in progress labels Jan 15, 2018
Copy link
Contributor

@etpinard etpinard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apalchys amazing work!

To the best of my knowledge, this latest iteration has everything @alexcjohnson described in his #2155 (comment) done in a very clear and concise matter.

I found a few ways to make your additions to fx/hover.js even clearer. Let me know what you think!

My most blocking comments are the following:

  • declare hoverdistance and spikedistance with min: -1
  • clicking on spikes button an even number of times in the modebar should get the user back to the original settings

Just as an FYI, if you can addressed these issues before tomorrow night, this PR will be merged and released as part of 1.33.0 on Thursday.


hoverdistance: {
valType: 'integer',
min: 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Infinity isn't JSON-serializable, we declare attributes of the likes (e.g. hoverlabel.namelength) with min: -1, so that users can set hoverdistance: -1 for an infinite search radius. Morevover, hoverdistance: 0 should imply no hover labels/events, similarly for spikedistance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done.

role: 'style',
editType: 'none',
description: [
'Sets the default distance (in points) to look for data',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually write (in pixels) instead of (in points) in our attribute descriptions.

@@ -28,6 +28,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
else hovermodeDflt = 'closest';

coerce('hovermode', hovermodeDflt);
coerce('hoverdistance');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could get fancy here by making this:

var hoverMode = coerce('hovermode', hovermodeDflt);
if(hoverDistance) coerce('hoverdistance');

as hoverdistance is useless when hovermode: false.

Unfortunately, we can't add similar logic for spikedistance here as showspikes is per axis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we can, because if hovermode is false, we are not firing any hover events, so the spikelines are disabled too.
So we can write it like this:

    var hoverMode = coerce('hovermode', hovermodeDflt);
    if(hoverMode) {
        coerce('hoverdistance');
        coerce('spikedistance');
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thanks!

@@ -551,12 +548,10 @@ modeBarButtons.toggleSpikelines = {
click: function(gd) {
var fullLayout = gd._fullLayout;

fullLayout._cartesianSpikesEnabled = fullLayout.hovermode === 'closest' ?
(fullLayout._cartesianSpikesEnabled === 'on' ? 'off' : 'on') : 'on';
fullLayout._cartesianSpikesEnabled = fullLayout._cartesianSpikesEnabled === 'on' ? 'off' : 'on';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might still need something similar here.

For example, with

var layout = {
    xaxis: {
      showspikes: true
    },
   yaxis: {
     showspikes: false
   }
  };

then click on toggleSpikeLines gives:

peek 2018-01-16 11-58

with no way to get back to the original behavior using the mode bar buttons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Thanks!

Now, could you restore and update the modebar_test.js cases you removed in your first commit?

@@ -2048,7 +2048,7 @@ axes.doTicks = function(gd, axid, skipTitle) {
top: pos,
bottom: pos,
left: ax._offset,
rigth: ax._offset + ax._length,
right: ax._offset + ax._length,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Did this cause any bugs previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did in one of my early implementations

return result;
}

if(fullLayout._has('cartesian')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of _has('cartesian') 👌

@@ -1236,3 +1406,12 @@ function hoverChanged(gd, evt, oldhoverdata) {
}
return false;
}

function spikesChanged(gd, oldspikepoints) {
// don't emit any events if nothing changed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this got copied from function hoverChanged() {}, but spike lines don't emit any events whatsoever. Would you mind 🔪 this line to avoid confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use it to avoid the redrawing of the plot because of new spikelines if spikelines points didn't change

if(closestVPoints.length) {
var closestVPt = closestVPoints[0];
if(isNumeric(closestVPt.x0) && isNumeric(closestVPt.y0)) {
tmpPoint = clearClosestPoint(closestVPt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clearClosestPoint method is doing mostly initialisation as opposed to clearing something that exists already.

Would be mind rename it either initClosestPoint or fillClosestPoint? This would make it more aligned with the rest of our codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I've renamed it to fillClosestPoint.

xpx = xa.c2p(xval),
ypx = ya.c2p(yval),
dxy = function(point) {
var rad = Math.max(3, point.mrc || 0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Math.max(3, point.mrc || 0) only applies to scatter traces and is a duplicate of this line in scatter/hover.js.

Perhaps we should make the relevant traces/*/hover.js methods (I can think of scatter at the moment, but there might be more) add a .kink field to their output point data to not have to duplicate the somewhat tricky logic here again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to the point.kink property of the scatter/hover returned object.

var xa,
ya;

var showY = closestPoints.hLinePoint ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe:

var showY = !!closestPoints.hLinePoint;

// or
var showY = Boolean(closestPoints.hLinePoint);

would suffice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed it to var showY = !!closestPoints.hLinePoint;

@apalchys
Copy link
Contributor Author

@etpinard Thank you for the review! I fixed all highlighted issues. Please check.

@apalchys
Copy link
Contributor Author

hmm.. some test is failing but I can't reproduce it locally. Is it possible to re-run tests without commits?

@alexcjohnson
Copy link
Collaborator

hmm.. some test is failing but I can't reproduce it locally. Is it possible to re-run tests without commits?

Mmm, I think I've seen that one fail sporadically. You can't rerun the test, but we can...

@etpinard
Copy link
Contributor

Awesome @apalchys

Once the spike toggling modebar tests are restored and updated (#2247 (comment)), this PR will be good to merge!

@etpinard
Copy link
Contributor

Great PR. Thanks very much @apalchys

💃 💃 💃

@etpinard etpinard merged commit e341edd into plotly:master Jan 17, 2018
@etpinard etpinard added this to the v1.33.0 milestone Jan 17, 2018
@apalchys
Copy link
Contributor Author

@etpinard @alexcjohnson Thank you for your help! 👍

@lambone
Copy link

lambone commented Feb 11, 2018

I found it does not work for scattergl and because the pointData in function hoverPoints lacks of kink attribute.

@alexcjohnson
Copy link
Collaborator

@lambone see #2336 - I will include scattergl in the fix.

@wangyueming
Copy link

wangyueming commented Feb 23, 2018

I checked the version 1.34.0, it does not work yet.

I made a simple fix in src/traces/scattergl/index.js.

    var minRad = (trace.mode.indexOf('markers') !== -1) ? 3 : 0.5;
    Lib.extendFlat(pointData, {
        color: getTraceColor(trace, di),

        x0: xc - rad,
        x1: xc + rad,
        xLabelVal: di.x,

        y0: yc - rad,
        y1: yc + rad,
        yLabelVal: di.y,

        // HACK add kink
        kink: Math.max(minRad, di.mrc || 0),
        cd: fakeCd
    });

@alexcjohnson
Copy link
Collaborator

@wangyueming #2379 was only merged 3 days ago. It will be in 1.35, likely next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken feature something new
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants