File Exchange

image thumbnail

DragDataTip

version 4.0 (7.81 KB) by Allen
Creates draggable data tips with custom labeling options on most 2-D plots. Custom labels options can also be applied to standard data tips.

11 Downloads

Updated 12 Aug 2019

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function provides a solution to the limited positions allowed by standard MATLAB Data Tips on
most 2-D plots by replacing them with draggable versions when the plot's "Data Tips" mode is
toggled 'off'. Since standard data tips can only be moved to a position where one of its four
corners touches the chosen data point, this often obstructs portions of nearby graphed data.
Draggable data tips can be moved anywhere on the current axes, thus allowing better visibility of
the plot. Several visual features are used to reference the draggable data tip to its data point
and parent object. These include a connecting line between the data tip textbox and its
corresponding data point, and the data tip border will try to utilize the same line properties as
the target line object.

The varargin input argument allows the user to optionally customize the data tips labels. For
example X- and Y-value labels can be replace with something more descriptive such as 'Time' and
'Accel'. In addition to axis labels and when a plot legend is used, a header label can be added to
the data tip. Headers are added above the axis labels and match the target object's display name
as it appears in the legend. While the data tip header feature requires a legend to exist prior to
creation of the data tip, the legend can be deleted after creating the data tip and will not
effect the header label. If a legend does not exist when the data tip is created, the header
option is ignored.

Note that optional labels are applied to all axes on a single figure. However, it is still
possible to use different labels for multiple axes on a single plot. This is done by reapplying
the function with a new set of labels before creating additional data tips containing the new
labels.

An EXAMPLE section is provided in the function notes, but if you have questions or trouble using
this function, leave a comment below.

Please rate it if have a chance; all feedback is welcome.
Thanks and enjoy, but don't forget to also download Francois Bouffard's draggable.m function.

Cite As

Allen (2019). DragDataTip (https://www.mathworks.com/matlabcentral/fileexchange/69799-dragdatatip), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

Dev-iL

Hi Allen, I'm working with simple numeric data. Consider the following example: `X = (-5:5).'; Y = rand(11,1); figure(); hP = plot(X,Y); hT = hP.DataTipTemplate; hT.DataTipRows(1:3) = [dataTipTextRow('Foo', abs(X)), dataTipTextRow('Bar', X.^2), hT.DataTipRows(2)]; hT.DataTipRows(end).Label = "Baz";` <- Then apply DragDataTip to this chart and compare the datatips before and after conversion to draggable. As a matter of fact, I've implemented a fix for this already. If you link your submission to e.g. GitHub, I'd be happy to discuss this in depth with code snippets and rendered outputs (and could also submit a PR).

Allen

Dev-iL, thank you for your comment. I have not yet had the opportunity to work with the new 'dataTipTextRow' and 'DataTipTemplate' features, but will gladly look into providing a fix to the issue you are experiencing. Until then, could you provide me with a sample of the code you are using and indicate what data type(s) you may be working with?

Dev-iL

Thank you Allen for this useful submission! I have one issue with it though: I'm creating multi-row datatip labels with the new functionality of `dataTipTextRow` and `DataTipTemplate` introduced in R2019a (my datatips have 4 rows which include the YData and some other information depending on the XData). Upon conversion of the built-in datatips to draggable ones, this information gets lost, and I'm left with just X and Y information (which I don't need). Is there anything that you can do so that the code respects the existing `DataTipTemplate`?

Allen

Andrew, thank you for your feedback. While originally intended for plot(), loglog(), and semilog plots, version 4.0 addresses some issue associated with various other 2-D plot types including scatter(). Additionally, several other overall improvements have been made. Give the new version a run and let me know if you run into any other problems. -Thanks!

I would warn that this function doesn't seem to work universally with all available 2D plotting functions. Some warnings were generated when I attempted use the draggable data tips with a scatter() plot, but it's easy enough to phrase a plot() operation to look like the output of a scatter() operation.

Allen

bcplay, thank you for pointing that out. I had recently discovered that it does not work with 'duration' data types as well as 'date/time'. It appears to be related to performing power and root calculations to these types of data. I will look into a solution for this issue and will post a fix as soon as I can. -Thanks

bcplay

Doesn't seem to work when x-axis values are dates

Andre Zeug

Updates

4.0

- Incorporate draggable() directly into code to address multiple issues
- Works with datetime data classes
- Fixed bug on log-scales
- Added more data tip deletion opts
- Fixed bugs associated with various 2-D plot types (scatter,area,bar,etc.)

3.2

- Fixed bug to apply function to multiple figures simultaneously.
- Fixed bug to work with 'duration' data types
- Added non-draggable display fixes for 'date/times'. Requires update to draggable.m to make 'date/times' draggable.

3.1

-Added 'on/off' option to the draggable feature. Allows header and labels to be applied to standard data tips.

3.0

- Now works with a single line of code.
- Assigns function to the current figure when the 'hFig' input is not used.
- Improved data tip deletion methods.
- Fixed bug that allowed data tips to be applied to existing data tip leader lines.

2.0

- Changed function behavior to utilize a property listener on the DataCursorManager "Enable" property. This allows toggling of the DataCursorMode between 'on' and 'off' to create the draggable data tips vs the previous need to use the "KeyPressFcn".

1.2

- Updated parsing of varargin.
- Fixed bug when applying custom data tip labels to default MATLAB Data Tips.

1.1

- Apply custom data tip labels to default MATLAB Data Tips before creating draggable versions.
- Fixed bug that prevented default (gca) assignment of hAx input when not provided.

1.0.1

Added Acknowledgements

MATLAB Release Compatibility
Created with R2019a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: draggable, Draggable data tips