Please find the example UI Action below - this one exports the corresponding trend data for the current day. itemsToRetrieve has to be set to an array containing the items you wish to export to CSV in the trend specified by its name in trendName:
/**
* Custom UI-Action 'UA_ExportTrend'.
*/
(function() {
var actions = shmi.pkg("visuals.session.userActions"); //get reference to userActions object
/**
* UI-Action 'UA_ExportTrend' implementation
*
* @params {any[]} parameters configured ui-action parameters
*
*/
actions["UA_ExportTrend"] = async function(parameters) {
const trend2 = shmi.visuals.core.TrendManager2,
tsNow = new Date().getTime();
itemsToRetrieve = ["DSaw", "DSin"], // Define items from the Trend to be retrieved here
trendName = "Trend"; // The name of the trend to query
// Set start to 00:00:00 on given timestamp (as an example)
const tsStart = new Date(tsNow);
tsStart.setHours(0);
tsStart.setMinutes(0);
tsStart.setSeconds(0);
// Set end to 23:59:59 on given timestamp (as an example)
const tsEnd = new Date(tsNow);
tsEnd.setHours(23);
tsEnd.setMinutes(59);
tsEnd.setSeconds(59);
// Get values from Trend2
const values = await trend2.query(trendName, itemsToRetrieve, tsStart.getTime(), tsEnd.getTime(), 1000);
let rows = [];
values.forEach((result =>
{
// Get all item values
result.values.item.forEach((dataRow =>
{
// Build csv row data
const tempArr = [result["item_alias"]].concat(dataRow);
rows.push(tempArr);
}
));
}
));
// Build csv file and open download
const csvContent = "data:text/csv;charset=utf-8,Item,TimeStamp,Value\n"
+ rows.map(e => e.join(",")).join("\n");
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
};
}());
Feel free to adapt it to your needs.
... View more