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.")