Hi!
I am creating a system in Perspective where a user can click on a bullet data point on an XY chart, which launches a popup to edit that data, and then closes and is supposed to deselect the point on the XY chart. However, I am having trouble causing the selected point to deselect, it usually remains selected after the popup closes.
Attached is the image of the UndoSelection message handler on the XY Chart, which is meant to disable and re-enable selection on the chart, which should cause the chart's selected field to revert to an empty array. This is a way to get around the read-only attribute of the selected.data field. But, this script does not behave as expected. The data array does not seem to properly clear
Try this:
self.props.selection.data = []
This did the trick on the selection front, but the chart did not revert to the colors it formerly had before the selection. New rabbit hole found!
The problem with your original message handler seems to be that toggling selection.enabled = False
and then True
again means the chart component never notices the transition. You could insert an evil sleep
command in there, but try this event-driven alternative instead:
- On the XY Chart, create a custom property
selectionResetTime : 0
.
- Create an expression binding on
selection.enabled
:
now(1000) - {this.custom.selectionResetTime} > 1000
This will re-enable the selection one second or so after it has been reset by the message handler below.
def onMessageReceived(self, payload):
self.props.selection.enabled = False
self.props.selection.data = []
self.custom.selectionResetTime = system.date.now()
The chart will redraw and the selection color will be reset.
That can fail if there is any comms hiccup. Instead, you should rely on something that comes from the browser as a result of disabling selection. Like, the selection disappearing. Consider not clearing the selection from the script, just disable selection. And only do that if the selection is already non-empty. A change event on selection.data
can then re-enable selection when it goes empty.
I recommend similar techniques for all Perspective components that support selection of some kind. Leverage the natural round trip timing instead of an arbitrary sleep duration. It also avoids excess CPU load from unnecessary polling expression bindings (* cough * now()
).
1 Like
This recommendation did the trick. It seems the first way of doing the message handlers caused the component to miss the call to the handler that refreshed the datasource, and so the colors didn't revert. Now they are reverting as expected! Thank you, Wayne Gretzky of the Ignition forums