Troubleshooting Vision Chart

I am updating an application that uses a chart. I'm trying to merge two datasets to populate a standard chart in vision. However, the values that I am seeing for the new column show are not showing the correct values. Below is the python code that I wrote in an event script on a refresh button.

The highlight column that I added seem to show indexes and not actual values.

DS = event.source.parent.Data

# Validate dataset  -- the button is disabled if there are no records in the event.source.parent.Data dataset
system.util.getLogger("Debug").info("Row count: {}".format(DS.rowCount))

#if DS is None or DS.rowCount == 0:
#    system.gui.errorBox("Dataset is empty. Cannot proceed.", "No Spectro Data for Location!")
    # raise ValueError("Dataset is empty. Cannot proceed.")

# Add a new column to the dataset
#columnName = "oxygen"
#columnData = [
#    float(i) for i in range(DS.rowCount)
#] 
#DS2 = system.dataset.addColumn(DS, columnData, columnName, float)
headers = list(DS.columnNames) + ["oxygen"]
data = [[DS.getValueAt(i,col) for col in range(DS.columnCount)] + [float(i)]
		for i in range(DS.rowCount)
]
DS2 = system.dataset.toDataSet(headers, data)
event.source.parent.Data_2 = DS2

#Logging the dataset to confirm the change:
system.util.getLogger("Debug").info("Columns after addColumn: {}".format(DS2.getColumnNames()))


# Retrieve dropdown selection parameters
DDElement = event.source.parent.getComponent("DDElement")
NDX = DDElement.selectedIndex
if NDX < 0:
    raise ValueError("No selection made in the dropdown.")

Column = DDElement.selectedLabel
Limit = DDElement.data.getValueAt(NDX, "limit")

# Validate column selection
system.util.getLogger("Debug").info("DS2 columns: {}".format(DS2.getColumnNames()))

if Column not in DS2.getColumnNames():
    system.util.getLogger("Validation").error("Column {} does not exist in dataset.".format(Column)) 
  

# Extract data from the selected column
Ordinal = DS2.getColumnIndex(Column)
Samples = DS2.getColumnAsList(Ordinal)

# Validate samples
if not Samples or len(set(Samples)) == 1:
    raise ValueError("Samples must contain at least two distinct values.")

# Generate I-MR control chart data
IMR = ControlCharts.I_MR(Samples, Limit)
if not IMR or len(IMR) != 2:
    raise ValueError("Invalid I-MR control chart data generated.")

# Update control chart components
Chart_I = event.source.parent.getComponent("Chart_I")
Chart_I.Data = system.dataset.filterColumns(IMR[0], ("Sample", "I"))
Chart_I.Limits = system.dataset.filterColumns(
    IMR[0], ("Sample", "XBAR", "UCL", "LCL", "Limit" if Limit != 0 else ""))

Chart_MR = event.source.parent.getComponent("Chart_MR")
Chart_MR.Data = system.dataset.filterColumns(IMR[1], ("Sample", "MR"))
Chart_MR.Limits = system.dataset.filterColumns(IMR[1], ("Sample", "RBAR", "UCL", "LCL"))

# Update trend chart
if "post_date" in DS2.getColumnNames():
    event.source.parent.getComponent("ChartTrend").Data = system.dataset.filterColumns(DS2, ("post_date", Column))
else:
    system.util.getLogger("Validation").error("Column 'post_date' not found in dataset.")

I don't see where you are actually trying to combine the two datasets. Where is the oxygen data coming from?

This:

data = [[DS.getValueAt(i,col) for col in range(DS.columnCount)] + [float(i)]
		for i in range(DS.rowCount)
]

is really putting the index on the end of your row.

1 Like

this property is bound to a query.

Is it possible for me to reference tags (indirect tags) to do this?

You are not getting information from that property. You are writing the dataset you constructed to it. If the oxygen data is in the Data_2 property, then you need to read it first.

As it stands, there is nothing that is reading the oxygen data that you want to add.

Can you post the window as a sample, with valid incoming data?

It doesn't "seem" to be. It is.

This line adds the row "index" converted to float to the columns of DS. You are not merging two datasets.

If event.source.parent.Data_2 is bound to a Query, then in your script you would be overwriting it.

Yes.

You've also made this way harder than it needs to be. If for instance I have two datasets, and I want to copy the values of a column from ds2 into ds, then that code would look something like this:

ds = event.source.parent.Data
ds2 = event.source.parent.Data_2

samples = ds2.getColumnAsList(ds2.getColumnIndex("oxygen"))
combinedData = system.dataset.addColumn(ds,samples,"oxygen",float)
2 Likes

Ok, so both properties DS and DS2 are bound to queries that look at different tables in different databases. So... looking at this I need to read both databases and then append the results in another variable that represents the new dataset.

Yes. Datasets are immutable, to you need to read both, then manipulate them to make a new dataset that you use somewhere else.

2 Likes