Easy Chart exportExcel rename headers?

image
button script:

easyChart = event.source.parent.getComponent('ChartContainer').getComponent('Easy Chart')
easyChart.exportExcel('myFileName.xls')

image
I want to change the header from 'Cut/jobpress' to 'jobpress'?
How to change?

Tip: Wiki - how to post code on this forum.

Rather than use exportExcel directly, you will need to use exportDatasets from Vision - Easy Chart Scripting Functions - Ignition User Manual 8.1 - Ignition Documentation.

Once you have your dataset(s) then using this example to update the column headers: Rename the column of a dataset - #2 by lrose

Then you can use system.dataset.exportExcel - Ignition User Manual 8.1 - Ignition Documentation to export the data to excel.

1 Like

One approach that I've used for this involves getting the data from the chart indirectly, and then, changing the header names before using system.dataset.exportExcel/CSV to save the file. Here is an example that has been adapted for this use case:

# Get the easyChart component
easyChart = event.source.parent.getComponent('ChartContainer').getComponent('Easy Chart')

# This function extracts the row data from the supplied dataset.
# ...Then, it creates a new dataset using the provided headers
def renameHeaders(dataset, headers):
	values = [[dataset.getValueAt(row,col) for col in range(dataset.columnCount)] for row in range(dataset.rowCount)]
	return system.dataset.toDataSet(headers,values)

# Get the tag paths column as a java list
tagPaths = list(easyChart.tagPens.getColumnAsList(easyChart.tagPens.getColumnIndex('TAG_PATH')))

# Create a list with the desired header names
headers = ['Time Stamp', 'jobpress']

# Get the displayed start date and end date from the chart
startTime = easyChart.zoomedStartDate
endTime = easyChart.zoomedEndDate

# Get the easy chart's dataset indirectly with a tag history query using the easy chart's parameters
tagHistory = system.tag.queryTagHistory(paths=tagPaths, startDate=startTime, endDate=endTime, returnSize=-1, aggregationMode="Maximum", returnFormat='Wide', noInterpolation=True)

# Use the renameHeaders function to replace the headers that were returned by the tag history query
dataset = renameHeaders(tagHistory, headers)

# Create an xls file using the modified dataset
system.dataset.exportExcel('myFileName.xls', True, dataset)

Result:
image

Edit:
@bschroeder posted his reply while I was putting mine together, so I didn't notice it. I tested the exportDatasets he listed and was pleased to see that it also returns only the displayed [zoomed] values. Here is the above example adapted to use that method:

# Get the easyChart component
easyChart = event.source.parent.getComponent('ChartContainer').getComponent('Easy Chart')

# This function extracts the row data from the supplied dataset.
# ...Then, it creates a new dataset using the provided headers
def renameHeaders(dataset, headers):
	values = [[dataset.getValueAt(row,col) for col in range(dataset.columnCount)] for row in range(dataset.rowCount)]
	return system.dataset.toDataSet(headers,values)

# Get the tag paths column as a java list
tagPaths = list(easyChart.tagPens.getColumnAsList(easyChart.tagPens.getColumnIndex('TAG_PATH')))

# Create a list with the desired header names
headers = ['Time Stamp', 'jobpress']

# Use the renameHeaders function in conjunction with exportDatasets
# ...to produce a dataset with the desired header names
dataset = renameHeaders(easyChart.exportDatasets()[0], headers)

# Create an xls file using the modified dataset
system.dataset.exportExcel('myFileName.xls', True, dataset)

Thank you very much!
I used this example in exchange.
Ignition Exchange | Inductive Automation

So in fact, the headers is dynamic.
I don't konw how to get the dynamic headers in easy chart.


# Create a list with the desired header names
headers = ['Time Stamp', 'CUT/jobprss']    # I don't konw how to get the dynamic headers in easy chart

# 定义headers映射
headersMapping = {
    'CUT/jobprss': u'jobprss',
    'COOLER/UTLM16/V2': u'冷冻机组/M16冰水出水温度',
    'COOLER/UTLM16/V3': u'冷冻机组/M16冰水回水温度',
    'COOLER/UTLM17/V2': u'冷冻机组/M17冰水出水温度',
    'COOLER/UTLM17/V3': u'冷冻机组/M17冰水回水温度'
}

# 修改headers
newHeaders = [headersMapping.get(name, name) for name in headers]

It looks like you could get the headers you want dynamically by doing this:

# Get the pen names, some of which contain '/' this character
penNames = easyChart.tagPens.getColumnAsList(0)

# Set the first header to t_stamp
# ...and get anything after the last '/' character in each pen name
headers = ['t_stamp']
for name in penNames:
	headers.append(name.split('/')[-1])

Edit: Almost forgot that the column headers have to include the time stamp

Also, if you need to filter out disable pens, the code could be modified in this way:

for row, name in enumerate(penNames):
	# Filter out disabled pens
	if easyChart.tagPens.getValueAt(row, 'Enabled'):
		headers.append(name.split('/')[-1])

Thank You very much!:hugs:

penNames = easyChart.tagPens.getColumnAsList(1)
print(penNames)

# Set the first header to t_stamp
# ...and get anything after the last '/' character in each pen name
headers = ['t_stamp']
for name in penNames:
    headers.append(name.split(']')[-1].upper())

#print(headers)

# Define headers mapping as a list of tuples
headersMapping = [
    (u'CUT/PRESS', u'剪切机/压力'),
    (u'CUT/INTEMP', u'剪切机/进料温度'),
    (u'CUT/OUTEMP', u'剪切机/出料温度'),
    (u'CUT/JOBPRESS', u'剪切机/工作压力')
]

# Modify headers
newHeaders = [next((new_name for old_name, new_name in headersMapping if old_name == header), header) for header in headers]

print(newHeaders)

image

2 Likes