in perspective
I want to create a button to export the data in XYchart to a CSV file through a script. How can I achieve this function
I've done similar on a project by creating a popup to display the CSV data in a Text Area component. As far as I know, there is no Perspective function to copy to the clipboard so I give user instructions for keyboard shortcuts. See below.
- Create the popup with
- Label with text: "Select the comma-separated data in the text area below (Ctrl-A), copy (Ctrl-C) and paste it into Excel (Ctrl-V)." (or whatever you want).
- Text Area
- Add a parameter to the view to accept the chart data.
- Add a Clipboard button to your primary view.
- Add a Navigation event to the button's onActionPerformed. Call the popup and pass the XY Chart's
dataSources.example
to the popup. - Back to the popup: create a script in the popup's
onStartup
event to loop through the data and generated the CSV and add this to the Text Areatext
property.
The script will be something like:
def runAction(self, event):
dataSource = self.getSibling("XYChart").props.dataSources.example
data = ""
for row in dataSource:
data += ','.join([str(v) for k, v in row.items()]) + '\n'
self.getSibling("TextArea").props.text = data
Tip: screengrabs from the computer are better than photos of a screen!