Bar chart scripts in the report

How can I manually enter the data into the bar chart by script in a report?

In the Data tab of the report editor, create a new script data source. Then write your script inside the updateData() function and assign the resulting dataset to a key, ie

data['MyBarChartData'] = system.dataset.toDataSet(myHeader,myData)

You can then assign the MyBarChartData key, or whatever you want to name it, to the bar chart component.

1 Like

How could I distribute these values in this script?

Could you be more specific? I'm not sure what you are asking.

In the image above I made a relationship between days of the month and flow. I would like to "assemble" this table through the report script.

Hi Jeferson, if what you want is an example script on how to build the dataset you’re showing, here:

#To make a dataset, you'll need a list of headers and a list with your data
#The headers list is simple:
myHeader = ['Day','Flow']

#Now, the data list is a list of lists that needs to be the same lenght of the list of headers,
#So if you have 2 headers, your data list will must have 2 itens per row
#You can add more rows by adding more lists itens inside your data list.
data = [[1,100],
	    [2,150],
	    [3,140],
	    [4,160],
	    [5,110],
	    [6,115],
	    [7,140],
	    [8,110],
	    [9,150],]
#Not that we have both, you just need to use the "system.dataset.toDataSet" to create your dataset
#and then use it in the report, or any place that uses datasets, actually.
#And here's the code dkhayes117 provided:
data['MyBarChartData'] = system.dataset.toDataSet(myHeader,data)

Thanks, Leonardo!!!

It is bad practice to assign the list of raw data to the variable data because that is the default dictionary for the data sources.

This will override any data that was previously in the data dictionary wiping out any query source data or parameters. Always name it something like scriptData instead.

#To make a dataset, you'll need a list of headers and a list with your data
#The headers list is simple:
myHeader = ['Day','Flow']

#Now, the data list is a list of lists that needs to be the same lenght of the list of headers,
#So if you have 2 headers, your data list will must have 2 itens per row
#You can add more rows by adding more lists itens inside your data list.
scriptedData = [[1,100],
	    [2,150],
	    [3,140],
	    [4,160],
	    [5,110],
	    [6,115],
	    [7,140],
	    [8,110],
	    [9,150],]
#Not that we have both, you just need to use the "system.dataset.toDataSet" to create your dataset
#and then use it in the report, or any place that uses datasets, actually.
#And here's the code dkhayes117 provided:
data['MyBarChartData'] = system.dataset.toDataSet(myHeader,scriptedData)

I've been burned with this a couple of times myself.

1 Like

Oops, forgot to change that, it’s a good reminder,it’s really annoying making this mistake and finding it

Taking advantage of the topic, in addition to inserting manual values in this script, is it possible to insert values from other queries?
I need to put in the bar chart day x flow, where the days will be entered manually and the flow will be done by queries by historical tags and SQL queries

You can get parameters into your scripting source by using the parameter name as a key…

dateParameter = data['date']

You can get query results like so

queryDS = data['myQuery'].getCoreResults()

That way it returns column and row. How to return only the value?

If I understand you correctly, to get a single value from a query (scalar value) you have a couple of options. You could run system.db.runScalarPrepQuery() inside the script source, or extract the value you want from the query dataset.

ds = data['myQuery'].getCoreResults()
value = ds.getValueAt(0,0)

or 

ds = data['myQuery'].getCoreResults()
pyDS = system.dataset.toPyDataSet(ds) # This makes the ds iterable
value = pyDS[0][0] # access value by indexing
for row in pyDS: # or iterate over the dataset 
    for value in row:
        print value