In highcharts I’m trying to add a drag-effect to point’s datalabel. I’ve found a couple of ways to target a given label. In this fiddle you can see some of my attempts. (I’ve combined to different attempts in one fiddle here to show off what I’ve tried)
Now both using chart.series[0].data[0].dataLabel and using the points click event makes me able to change the attributes of a dataLabel. However I’m not able to attach a jquery ui .draggable() to the label. Is is somehow possible to make an element inside a chart draggable?
Please let me know if any more information is needed
You can actually attach
draggablewidgets to the data labels just fine. However, since the data labels are SVG elements, they completely ignore changes made to theirposition,leftandtopCSS properties.Instead, the
<g>elements that wrap the labels expose atransformattribute:We can still use the
draggablewidget (which is quite handy because it abstracts the lower-level drag events) and update thetransformattribute of the data label wrappers in a drag event handler. Since drag coordinates are relative, we’ll have to parse the originaltransformattribute and use the resulting coordinates as the origin.Now, all the label wrappers are themselves children of groups decorated with the
highcharts-data-labelsclass. This provides us with a good starting point, so we can write something like:You will find an updated fiddle here.
A few remarks about the code above:
filter() is used instead of each() because it allows us to filter out elements whose
transformattributes could not be parsed (you never know), instead of outright failing or feeding garbage to the computations in thedraghandler,attr() is used instead of prop() to emphasize the fact we’re modifying attributes in an SVG fragment.
prop()would probably work in the exact same way, so it’s only a matter of style and personal preference.Finally, a caveat: I can only test this on Firefox for now, but it seems to be very difficult to trigger
mousedownevents on some of the labels, especially in “crowded” areas with portions of the curve and the data tip competing for space. Tweaking thezIndexattributes of the data label wrappers did not achieve much. Multiple clicks sometimes help. If this is consistent on all browsers, additional solutions will probably have to be devised (transparent overlays maybe, or simply relayingmousedownevents from the data points themselves to the data labels).