Historical data
Datalinks are used also to get historical data. In this example we will see how to get historical data from them.
The example shows how to:
- expose a property that can be linked to a model property with Datalink
- every time the time interval changes fetch historical data
To do this we will:
- override
BaseGraphicWgt::addDatalink
to get the reference to the source widget and the source property - override of
BaseGraphicWgt::setPropertyValue
to trigger the data fetching every time the time interval changes - define the method
fetch
to get historical data using the apireadHistData
BaseGraphic Methods
BaseGraphicWgt::addDatalink
The method addDatalink
is called every time a datalink is added to the widget.
BaseGraphicWgt::setPropertyValue
The method setPropertyValue
is called every time a widget property is changed.
Example
// Override BaseGraphicWgt::addDatalink
public addDatalink( dataLink: IDataLinkConstructorArgs )
{
const newDatalink = super.addDatalink(dataLink);
switch( dataLink.tgtProp )
{
case "tagReference":
this.dataSource = newDatalink.getDataSourceWidget();
this.tagName = dataLink.srcProp;
break;
}
return newDatalink;
}
// Override BaseGraphicWgt::setPropertyValue
public setPropertyValue( { prop, value } )
{
super.setPropertyValue( { prop, value } );
switch ( prop )
{
case "from":
case "to":
this.fetchData();
break;
}
}
// Example methods to get historical data from cloud
private async fetch( from: number, to: number ): Promise<DataValue[]>
{
if ( !this.dataSource || !this.tagName )
return [];
// Here and example to request
const data = <DataValue[]>await this.dataSource.readHistData(
this.tagName, // Name of the tag to read
from, // Start date in milliseconds
to, // End date in milliseconds
undefined, // Number of samples if not defined get all samples
false, // apply downsampling
undefined, // Instead of undefined you can request to the server to perform an
// operation of aggreagation
// {
// type: "average",
// sampling: {
// extent: 5,
// size: 2,
// unit: "minutes"
// }
// }
undefined, // specify downsampling size
);
return data;
}