Creating trends by using script

import system
import system.date

columns = [
    "name", "tagPath", "aggregationMode", "axis", "subplot", "enabled",
    "penColor", "dashPattern", "renderStyle", "lineWeight", "shape",
    "fillShape", "labels", "group_name", "digital", "overrideAutoColor",
    "hidden", "userSelectable", "sortOrder", "userRemovable"
]

rows = [
    ["Temperature", "[EMS_TRIAL_DATABASE]tag_1-tag_11/tag_1", "Average", 0, 0, True,
     0xFF0000, "", "Line", 2, 0, False, "", "", False, False, False, True, 1, True],

    ["Pressure", "[EMS_TRIAL_DATABASE]tag_1-tag_11/tag_2", "Average", 0, 0, True,
     0x000FFF, "", "Line", 1, 0, False, "", "", False, False, False, True, 2, True]
]

dataset = system.dataset.toDataSet(columns, rows)
print "Columns:", dataset.getColumnNames()

chart = event.source.parent.getComponent("Easy Chart")
chart.tagPens = system.dataset.toDataSet([], [])  # Clear first

endTime = system.date.now()
startTime = system.date.addHours(endTime, -2)

chart.startDate = startTime
chart.endDate = endTime
chart.tagPens = dataset

i am using this script to create trends in easy chart in vision.but i am getting the following error

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
** at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)**
** at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)**
** at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)**
** at java.base/java.util.Objects.checkIndex(Unknown Source)**
** at java.base/java.util.ArrayList.get(Unknown Source)**
** at com.inductiveautomation.ignition.common.AbstractDataset.getColumnType(AbstractDataset.java:105)**
** at com.inductiveautomation.factorypmi.application.components.chart.easychart.PenData.setData(PenData.java:94)**
** at com.inductiveautomation.factorypmi.application.components.chart.easychart.TagHistoryPenDatasource.setData(TagHistoryPenDatasource.java:99)**
** at com.inductiveautomation.factorypmi.application.components.chart.easychart.AbstractChartQuery.handleQueryReturnedValue(AbstractChartQuery.java:146)**
** at com.inductiveautomation.factorypmi.application.gateway.QueryManager$HandleReturnValueTask.run(QueryManager.java:486)**
** at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)**
** at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)**
** at java.desktop/java.awt.EventQueue$4.run(Unknown Source)**
** at java.desktop/java.awt.EventQueue$4.run(Unknown Source)**
** at java.base/java.security.AccessController.doPrivileged(Unknown Source)**
** at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)**
** at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)**
** at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)**
** at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)**
** at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)**
** at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)**
** at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)**
** at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)**

Please see Wiki - how to post code on this forum.
Then you can edit your post to format the code block and format the error text. Thanks.

A few things:

  1. Do not import system, or system.date
  2. There is no need to "clear" the dataset. Datasets are immutable, so writing over it with the new dataset is sufficient.
  3. You don't need to create a list of headers, you can just get the headers from the tagPens dataset. This will prevent you from missing a column that the chart is expecting.
  4. You sould use actual java.awt.Color objects instaed of hex values.
  5. Be sure to note the types of the columns, so that you can provide the type that the chart is expecting. For instance, the chart expects the render style to be an integer value. The chart component provides static variables for this purpose. You can also, just add a tag manually and edit the render style to find the integer values for each and use that.
  6. The axis should be set to either the name of an Axis you have added to the chart or "Default Axis", which is what is added for you, other wise you may get unexpected results.

This code works as I would expect it to.

from java.awt import Color
chart = event.source.parent.getComponent("Easy Chart")
columns = list(chart.tagPens.columnNames)

rows = [
    ["Temperature", "[EMS_TRIAL_DATABASE]tag_1-tag_11/tag_1", "Average", "Default Axis", 0, True,
     Color.RED, "", chart.STYLE_LINE, 2, 0, False, "", "", False, False, False, True, 1, True],

    ["Pressure", "[EMS_TRIAL_DATABASE]tag_1-tag_11/tag_2", "Average", "Default Axis", 0, True,
     Color(0,15,255), "", chart.STYLE_LINE, 1, 0, False, "", "", False, False, False, True, 2, True]
]

pens= system.dataset.toDataSet(columns, rows)
chart.endDate = system.date.now()
chart.startDate = system.date.addHours(chart.endDate, -2)
chart.tagPens = pens
2 Likes