Add Perspective XY Chart Datasources

Hi there,

I would like to be able to create through scripting (or with a simple binding) a dynamic amount of datasources on a XY Chart in Perspective.
I would like to bind each datasource to a Named Query.

Does it sound possible ?

Edit : I managed to do so using scripting, however i’m still stucked trying to create a query binding on each datasource through scripting.
Is it even possible ? When I copy/paste the JSON code of an XY Chart (with query binding on a datasource) in a text editor, I see that there is a propConfig parameter but it can’t be accessed through scripting.

To clarify are you just trying to handle the dynamic datasets with Named Query bindings instead of through a Script binding for cleanliness sake?

You aren’t able to create bindings through scripting per-se, but I may be able to mockup an idea that would help you out.

Well I would like to be able to add a binding to a datasource on an XY chart using script (or with another way if it is possible).
Actually we have several databases (all of them have the same structure) with data measured by sensors (the number of sensors can be different from a db to another).
I managed to create as many datasources as there are sensors.
For example : db1 has 2 sensors, db2 has 10 sensors. Using scripting i’m able to create 2 empty datasources when I choose db1 and 10 when I choose db2.

What I want now, is to be able to bind each datasource to a named query (specifying the named query path and the parameters).

Do you have any hint ?

Okay I better understand now.

In the script that you are creating the datasource, execute your named queries from there, and set the datasource key = your the return from the named query, like so
image

In the scripting this may look like this (Roughed out for explanations sake)

#I am hardcoding this list of sensors for sake of example, but you would determine them dynamically
if db == "db1":
	sensorCount = 2
elif db == "db2":
	sensorCount = 10

#Define a dictionary to store your datasources in
datasources = {}
#For each sensor
for i in range(len(sensor)):
	#Query for your sensor data based off whatever parameters you need
	sensorData = system.db.runNamedQuery("GetSensorData", parameters={"SensorNumber":i})
	#Assign the datasource to a dicitonary key named "Sensor1" or whatever number you are on
	dataSources["Sensor" + i] = sensorData

#Return the dictionary of sensor datasets to the dataSources prop
return dataSources

However I would also ask the question, is there any reason you could not pull all of the sensor data in the same data source with a varying number of columns?
i.e.

DB 1
t_stamp Sensor1 Sensor2
8:00 AM 123 654
9:00 AM 234 543
10:00 AM 345 321

OR

DB 2
t_stamp Sensor1 Sensor2 Sensor3 Sensor4 Sensor5 Sensor6 Sensor7 Sensor8 Sensor9 Sensor10
8:00 AM 123 654 1185 1716 2247 2778 3309 3840 4371 4902
9:00 AM 234 543 852 1161 1470 1779 2088 2397 2706 3015
10:00 AM 345 321 297 273 249 225 201 177 153 129

That way you are relying on less queries to the database and so it would be a bit more efficient.

This is also a bit of a complex view to wrap your head around sometimes, but a modified varient of the Ad-Hoc trend page on the Exchange may be an easier solution for you as well, just modifying the datasets and using some form of db selector instead of the tag tree. Ignition Exchange | Inductive Automation

Thanks !

In addition to that, I’ll have 2 DateTimeInput component on my view to display data between 2 selected dates (I have 2 date parameters on my Named Query).
How will I refresh my datasources ?

I would make it so the script I wrote above is a script binding on the dataSources property

And then use an expressionStructure binding that includes both your start and end time, that way when you change them it will reevaluate the binding, re-run the scripts and refresh the datasources

Well, thank you very much !

I’ll let you know if I get it working.

1 Like