Combining Datasets

Hello,

I have two, single column datasets, that are the same length. One is a series of X values while the other is Y values. I’d like to combine them into a single two column dataset so it can be graphed.

From what I’ve seen it has to be done in script but I can’t seem to get the scripting right.

Any input is appreciated.

Thanks!

Try something like this. Obviously you’ll have your own datasets, and not the ones I’ve created in script for the purpose of this example:

toPyDS = system.dataset.toPyDataSet

xDataSet = toPyDS(system.dataset.toDataSet(['x'], [[1],[2],[3]]))
yDataSet = toPyDS(system.dataset.toDataSet(['y'], [[4],[5],[6]]))

xs = [row[0] for row in xDataSet]
ys = [row[0] for row in yDataSet]

rows = zip(xs, ys)
xyDataSet = system.dataset.toDataSet(['x', 'y'], rows)

event.source.parent.getComponent('Table').data = xyDataSet

That did exactly what I was looking for. However, when I tie it to a chart it has gives an error. I should have noted earlier that these are being read from an OPC server and are coming in as a string. After I do the split I guess the resulting dataset is all strings and the spaces or string datatype may be causing the chart error? Do I need to do something different with the base datasets?

Chart error: “IllegalArgumentException: Illegal type for X value: class java.lang.String (column ‘x’)”

Background:
I remove the brackets from the string, then do a split on “,” in an expression to get the base dataset.

split(substring({Wavelengths}, 1, indexOf({Wavelengths}, "]")),",")

The resulting data set looks like this:


After running the code Kevin posted my combined dataset:

Thanks again.

Casting the x and y values from strings to numbers before zipping may be the answer:

toPyDS = system.dataset.toPyDataSet

xDataSet = toPyDS(system.dataset.toDataSet(['x'], [['1.1'],['2.2'],['3.3']]))
yDataSet = toPyDS(system.dataset.toDataSet(['y'], [['4.4'],['5.5'],['6.6']]))

xs = [float(row[0]) for row in xDataSet]
ys = [float(row[0]) for row in yDataSet]

rows = zip(xs, ys)
xyDataSet = system.dataset.toDataSet(['x', 'y'], rows)

event.source.parent.getComponent('Table').data = xyDataSet
1 Like

Perfect! Thanks for the help.