Web Client 8.0.2

Chart

Chart

A widget displaying the data from its model as a multiline chart. As with all the classes extending AbstractWidget the internal model can be automatically updated by listening to one or more Subscription instances.
In short, once both X and Y axis have been associated to a field through Chart#setXAxis and Chart#addYAxis, each row in the model will be represented as a line in the chart, connecting all the X,Y points corresponding to the subsequent values assumed by the related fields and dynamically extending with new values. Actually, it is possible to associate more fields to the Y axis so that it is possible to have more than one line per row.
According to the axis settings, every time a row enters the model, one or more lines will be added to the chart and corresponding instances of ChartLine will be generated and passed to the ChartListener#onNewLine event to be better configured.
The behavior of the underlying model is described in AbstractWidget, but there is one exception: if this instance is used to listen to events from Subscription instance(s), and the first Subscription it listens to is a DISTINCT Subscription, then the base behavior is overridden and the same behavior defined for MERGE and RAW modes is adopted.

Note that, in order to create a chart, the X axis should be associated with a field whose values are increasing. Anyway for both X and Y axis the used value must be a numeric value. If the original field values are not compliant with such restriction, they can be customized before being used by means of a parser Function.
Also, even if the Chart instance is listening to Subscription events, it is not mandatory to use server-sent fields to plot the chart.
The multiline chart for the visualization of model values is dynamically maintained by an instance of this class inside a container HTML element. The container element must be prepared on the page in the form of any HTML element owning the "data-source='Lightstreamer'" special attribute, together with an HTML "id" attribute that has to be specified in the constructor of this class.

Constructor

new Chart(id)

Creates an object that extends AbstractWidget displaying its values as a multiline chart.
Note that the AbstractWidget#parseHtml method is automatically called by this constructor, hence the container element should have already been prepared on the page DOM. However, preparing the element later and then invoking AbstractWidget#parseHtml manually is also supported.

Parameters:
Name Type Description
id String

The HTML "id" attribute of a DOM Element to which the chart will be attached.

Extends

Methods

addListener(listener)

Adds a listener that will receive events from the Chart instance.
The same listener can be added to several different Chart instances.

Lifecycle: a listener can be added at any time.

Parameters:
Name Type Description
listener ChartListener

An object that will receive the events as shown in the ChartListener interface.
Note that the given instance does not have to implement all of the methods of the ChartListener interface. In fact it may also implement none of the interface methods and still be considered a valid listener. In the latter case it will obviously receive no events.

addYAxis(field, yParseropt)

Adds field(s) to be used as the source of the Y-coordinate for each update An optional parser can be passed to normalize the value before it is used to plot the chart. The resulting values should be in the limits posed by the ChartLine#positionYAxis related to the involved line, otherwise a ChartListener#onYOverflow event is fired to handle the situation. null can also be specified, in which case, if the associated X value is null the chart line will be cleared, otherwise the update will be ignored.
It is possible to specify an array of fields instead of specifying a single field. If that's the case multiple chart lines will be generated per each row in the model.
Note that for each field in the underlying model it is possible to associate only one line. If multiple lines based on the same fields are needed, dedicated fields should be added to the model, through AbstractWidget#updateRow. In case this instance is used to listen to events from Subscription instance(s), updateRow() can be invoked from within SubscriptionListener#onItemUpdate.

Lifecycle: The method can be invoked at any time, in order to add fields to be plotted or in order to change the parser associated to fields already being plotted. Until invoked for the first time, no chart will be printed.

Parameters:
Name Type Attributes Description
field String

A field name representing the Y axis. An array of field names can also be passed. Each field will generate its own line.

yParser CustomParserFunction | Array.<CustomParserFunction> <optional>

A parser function that can be used to normalize the value of the Y field before using it to plot the chart. If the function is not supplied, then the field values should represent valid numbers in JavaScript or be null.
If an array has been specified for the field parameter, then an array of parser functions can also be passed. Each parser will be executed on the field having the same index in the array. On the other hand, if an array of fields is passed but only one parser has been specified, then the parser will be applied to all of the fields.

See:

clean()

Removes all the rows from the model and reflects the change on the view.

Lifecycle: once the AbstractWidget#parseHtml method has been called, this method can be used at any time.

Overrides:
Throws:

if parseHtml has not been executed yet.

Type
IllegalStateException

configureArea(chartCssopt, chartHeightopt, chartWidthopt, chartTopopt, chartLeftopt)

Setter method that sets the stylesheet and positioning to be applied to the chart area.

Lifecycle: The chart area stylesheet and position attributes can be set and changed at any time.

Parameters:
Name Type Attributes Default Description
chartCss String <optional>

the name of an existing stylesheet to be applied to the chart. If not set, the stylesheet is inherited from the DOM element containing the chart.

chartHeight Number <optional>

the height in pixels of the chart area. Such height may be set as smaller than the height of the container HTML element in order to make room for the X axis labels. If not set, the whole height of the container HTML element is used.

chartWidth Number <optional>

the width in pixels of the chart area. Such width may be set as smaller than the width of the container HTML element in order to make room for the Y axis labels. If not set, the whole width of the container HTML element is used.

chartTop Number <optional>
0

the distance in pixels between the top margin of the chart area and the top margin of the container HTML element. Such distance may be set as a nonzero value in order to make room for the first Y axis label. If not set, 0 is used.

chartLeft Number <optional>
0

the distance in pixels between the left margin of the chart area and the left margin of the container HTML element. Such distance may be set as a nonzero value in order to make room for the Y axis labels. If not set, 0 is used.

Throws:

if one of the numeric values is not valid.

Type
IllegalArgumentException

getListeners() → {Array.<ChartListener>}

Returns an array containing the ChartListener instances that were added to this client.

Returns:

an array containing the listeners that were added to this instance. Listeners added multiple times are included multiple times in the array.

Type
Array.<ChartListener>

getValue(key, field) → {String}

Returns the value from the model for the specified key/field pair. If the row for the specified key does not exist or if the specified field is not available in the row then null is returned.

Lifecycle: This method can be called at any time.

Parameters:
Name Type Description
key String

The key associated with the row to be read.

field String

The field to be read from the row.

Inherited From:
Returns:

The current value for the specified field of the specified row, possibly null. If the value for the specified field has never been assigned in the model, the method also returns null.

Type
String

onClearSnapshot(itemName, itemPos)

Event handler that is called by Lightstreamer each time a request to clear the snapshot pertaining to an item in the Subscription has been received from the Server. More precisely, this kind of request can occur in two cases:

  • For an item delivered in COMMAND mode, to notify that the state of the item becomes empty; this is equivalent to receiving an update carrying a DELETE command once for each key that is currently active.
  • For an item delivered in DISTINCT mode, to notify that all the previous updates received for the item should be considered as obsolete; hence, if the listener were showing a list of recent updates for the item, it should clear the list in order to keep a coherent view.

Note that, if the involved Subscription has a two-level behavior enabled, the notification refers to the first-level item (which is in COMMAND mode). This kind of notification is not possible for second-level items (which are in MERGE mode).
This event can be sent by the Lightstreamer Server since version 6.0
Parameters:
Name Type Description
itemName String

name of the involved item. If the Subscription was initialized using an "Item Group" then a null value is supplied.

itemPos Number

1-based position of the item within the "Item List" or "Item Group".

Inherited From:

onCommandSecondLevelItemLostUpdates(lostUpdates, key)

Event handler that is called by Lightstreamer to notify that, due to internal resource limitations, Lightstreamer Server dropped one or more updates for an item that was subscribed to as a second-level subscription. Such notifications are sent only if the Subscription was configured in unfiltered mode (second-level items are always in "MERGE" mode and inherit the frequency configuration from the first-level Subscription).
By implementing this method it is possible to perform recovery actions.

Parameters:
Name Type Description
lostUpdates Number

The number of consecutive updates dropped for the item.

key String

The value of the key that identifies the second-level item.

Inherited From:
See:

onCommandSecondLevelSubscriptionError(code, message, key)

Event handler that is called when the Server notifies an error on a second-level subscription.
By implementing this method it is possible to perform recovery actions.

Parameters:
Name Type Description
code Number

The error code sent by the Server. It can be one of the following:

  • 14 - the key value is not a valid name for the Item to be subscribed; only in this case, the error is detected directly by the library before issuing the actual request to the Server
  • 17 - bad Data Adapter name or default Data Adapter not defined for the current Adapter Set
  • 21 - bad Group name
  • 22 - bad Group name for this Schema
  • 23 - bad Schema name
  • 24 - mode not allowed for an Item
  • 26 - unfiltered dispatching not allowed for an Item, because a frequency limit is associated to the item
  • 27 - unfiltered dispatching not supported for an Item, because a frequency prefiltering is applied for the item
  • 28 - unfiltered dispatching is not allowed by the current license terms (for special licenses only)
  • 66 - an unexpected exception was thrown by the Metadata Adapter while authorizing the connection
  • 68 - the Server could not fulfill the request because of an internal error.
  • <= 0 - the Metadata Adapter has refused the subscription or unsubscription request; the code value is dependent on the specific Metadata Adapter implementation

message String

The description of the error sent by the Server; it can be null.

key String

The value of the key that identifies the second-level item.

Inherited From:
See:

onEndOfSnapshot(itemName, itemPos)

Event handler that is called by Lightstreamer to notify that all snapshot events for an item in the Subscription have been received, so that real time events are now going to be received. The received snapshot could be empty. Such notifications are sent only if the items are delivered in DISTINCT or COMMAND subscription mode and snapshot information was indeed requested for the items. By implementing this method it is possible to perform actions which require that all the initial values have been received.
Note that, if the involved Subscription has a two-level behavior enabled, the notification refers to the first-level item (which is in COMMAND mode). Snapshot-related updates for the second-level items (which are in MERGE mode) can be received both before and after this notification.

Parameters:
Name Type Description
itemName String

name of the involved item. If the Subscription was initialized using an "Item Group" then a null value is supplied.

itemPos Number

1-based position of the item within the "Item List" or "Item Group".

Inherited From:
See:

onItemLostUpdates(itemName, itemPos, lostUpdates)

Event handler that is called by Lightstreamer to notify that, due to internal resource limitations, Lightstreamer Server dropped one or more updates for an item in the Subscription. Such notifications are sent only if the items are delivered in an unfiltered mode; this occurs if the subscription mode is:

  • RAW
  • MERGE or DISTINCT, with unfiltered dispatching specified
  • COMMAND, with unfiltered dispatching specified
  • COMMAND, without unfiltered dispatching specified (in this case, notifications apply to ADD and DELETE events only)
By implementing this method it is possible to perform recovery actions.
Parameters:
Name Type Description
itemName String

name of the involved item. If the Subscription was initialized using an "Item Group" then a null value is supplied.

itemPos Number

1-based position of the item within the "Item List" or "Item Group".

lostUpdates Number

The number of consecutive updates dropped for the item.

Inherited From:
See:

onItemUpdate(updateInfo)

Event handler that is called by Lightstreamer each time an update pertaining to an item in the Subscription has been received from the Server.

Parameters:
Name Type Description
updateInfo ItemUpdate

a value object containing the updated values for all the fields, together with meta-information about the update itself and some helper methods that can be used to iterate through all or new values.

Inherited From:

onListenEnd(subscription)

Event handler that receives a notification when the SubscriptionListener instance is removed from a Subscription through Subscription#removeListener. This is the last event to be fired on the listener.

Parameters:
Name Type Description
subscription Subscription

the Subscription this instance was removed from.

Inherited From:

onListenStart(subscription)

Event handler that receives a notification when the SubscriptionListener instance is added to a Subscription through Subscription#addListener. This is the first event to be fired on the listener.

Parameters:
Name Type Description
subscription Subscription

the Subscription this instance was added to.

Overrides:

onRealMaxFrequency(frequency)

Event handler that is called by Lightstreamer to notify the client with the real maximum update frequency of the Subscription. It is called immediately after the Subscription is established and in response to a requested change (see Subscription#setRequestedMaxFrequency). Since the frequency limit is applied on an item basis and a Subscription can involve multiple items, this is actually the maximum frequency among all items. For Subscriptions with two-level behavior (see Subscription#setCommandSecondLevelFields and Subscription#setCommandSecondLevelFieldSchema) , the reported frequency limit applies to both first-level and second-level items.
The value may differ from the requested one because of restrictions operated on the server side, but also because of number rounding.
Note that a maximum update frequency (that is, a non-unlimited one) may be applied by the Server even when the subscription mode is RAW or the Subscription was done with unfiltered dispatching.

Parameters:
Name Type Description
frequency String

A decimal number, representing the maximum frequency applied by the Server (expressed in updates per second), or the string "unlimited". A null value is possible in rare cases, when the frequency can no longer be determined.

Inherited From:

onSubscription()

Event handler that is called by Lightstreamer to notify that a Subscription has been successfully subscribed to through the Server. This can happen multiple times in the life of a Subscription instance, in case the Subscription is performed multiple times through LightstreamerClient#unsubscribe and LightstreamerClient#subscribe. This can also happen multiple times in case of automatic recovery after a connection restart.
This notification is always issued before the other ones related to the same subscription. It invalidates all data that has been received previously.
Note that two consecutive calls to this method are not possible, as before a second onSubscription event is fired an onUnsubscription event is eventually fired.
If the involved Subscription has a two-level behavior enabled, second-level subscriptions are not notified.

Inherited From:

onSubscriptionError(code, message)

Event handler that is called when the Server notifies an error on a Subscription. By implementing this method it is possible to perform recovery actions.
Note that, in order to perform a new subscription attempt, LightstreamerClient#unsubscribe and LightstreamerClient#subscribe should be issued again, even if no change to the Subscription attributes has been applied.

Parameters:
Name Type Description
code Number

The error code sent by the Server. It can be one of the following:

  • 15 - "key" field not specified in the schema for a COMMAND mode subscription
  • 16 - "command" field not specified in the schema for a COMMAND mode subscription
  • 17 - bad Data Adapter name or default Data Adapter not defined for the current Adapter Set
  • 21 - bad Group name
  • 22 - bad Group name for this Schema
  • 23 - bad Schema name
  • 24 - mode not allowed for an Item
  • 25 - bad Selector name
  • 26 - unfiltered dispatching not allowed for an Item, because a frequency limit is associated to the item
  • 27 - unfiltered dispatching not supported for an Item, because a frequency prefiltering is applied for the item
  • 28 - unfiltered dispatching is not allowed by the current license terms (for special licenses only)
  • 29 - RAW mode is not allowed by the current license terms (for special licenses only)
  • 30 - subscriptions are not allowed by the current license terms (for special licenses only)
  • 66 - an unexpected exception was thrown by the Metadata Adapter while authorizing the connection
  • 68 - the Server could not fulfill the request because of an internal error.
  • <= 0 - the Metadata Adapter has refused the subscription or unsubscription request; the code value is dependent on the specific Metadata Adapter implementation

message String

The description of the error sent by the Server; it can be null.

Inherited From:
See:

onUnsubscription()

Event handler that is called by Lightstreamer to notify that a Subscription has been successfully unsubscribed from. This can happen multiple times in the life of a Subscription instance, in case the Subscription is performed multiple times through LightstreamerClient#unsubscribe and LightstreamerClient#subscribe. This can also happen multiple times in case of automatic recovery after a connection restart.


After this notification no more events can be recieved until a new SubscriptionListener#onSubscription event.
Note that two consecutive calls to this method are not possible, as before a second onUnsubscription event is fired an onSubscription event is eventually fired.
If the involved Subscription has a two-level behavior enabled, second-level unsubscriptions are not notified.

Inherited From:

parseHtml()

This method is automatically called by the constructor of this class. It will bind the current instance with the HTML element having the id specified in the constructor.

Overrides:

positionXAxis(min, max)

Operation method that sets or changes the limits for the visible part of the X axis of the chart (that is, the minimum and maximum X-coordinates shown in the chart). If these limits are changed while the internal model is not empty then this causes a repaint of the whole chart.
Note that rising the minimum X value shown also clears from the memory all the points whose X value becomes lower. So, those points will not be displayed again after lowering again the minimum X value.

Lifecycle: The X axis limits can be set at any time.

Parameters:
Name Type Description
min Number

lower limit for the visible part of the X axis.

max Number

higher limit for the visible part of the X axis.

Throws:

if the min parameter is greater than the max one.

Type
IllegalArgumentException

removeListener(listener)

Removes a listener from the Chart instance so that it will not receive events anymore.

Lifecycle: a listener can be removed at any time.

Parameters:
Name Type Description
listener ChartListener

The listener to be removed.

removeRow(key)

Removes a row from the internal model and reflects the change on the view. If no row associated with the given key is found nothing is done.

Lifecycle: once the AbstractWidget#parseHtml method has been called, this method can be used at any time.

Parameters:
Name Type Description
key String

The key associated with the row to be removed.

Inherited From:
Throws:

if parseHtml has not been executed yet.

Type
IllegalStateException

removeYAxis(field)

Removes field(s) currently used as the source of the Y-coordinate for each update and all the related ChartLine.
It is possible to specify an array of fields instead of specifying a single field. If that's the case all the specified fields and related chart lines will be removed.

Lifecycle: The method can be invoked at any time, in order to remove plotted fields.

Parameters:
Name Type Description
field String

A field name representing the Y axis. An array of field names can also be passed.

See:

setAutoCleanBehavior(onFirstSubscribe, onLastUnsubscribe)

Utility method that can be used to control part of the behavior of the widget in case it is used as a listener for one or more Subscription instances.
Specifying the two flags it is possible to decide to clean the model and view based on the status (subscribed or not) of the Subscriptions this instance is listening to.

Lifecycle: This method can be called at any time.

Parameters:
Name Type Description
onFirstSubscribe boolean

If true a AbstractWidget#clean call will be automatically performed if in the list of Subscriptions this instance is listening to there is no Subscription in the subscribed status and an onSubscription is fired by one of such Subscriptions.
As a special case, if in the list of Subscriptions this instance is listening to there is no Subscription in the subscribed status and this instance starts listening to a new Subscription that is already in the subscribed status, then it will be considered as if an onSubscription event was fired and thus a clean() call will be performed.

onLastUnsubscribe boolean

If true a AbstractWidget#clean call will be automatically performed if in the list of Subscriptions this instance is listening to there is only one Subscription in the subscribed status and the onUnsubscription for such Subscription is fired.
As a special case, if in the list of Subscriptions this instance is listening to there is only one Subscription in the subscribed status and this instance stops listening to such Subscription then it will be considered as if the onUnsubscription event for that Subscription was fired and thus a clean() call will be performed.

Inherited From:
See:

setXAxis(field, xParseropt)

Setter method that sets the field to be used as the source of the X-coordinate for each update. An optional parser can be passed to normalize the value before it is used to plot the chart. The resulting values should be in the limits posed by the Chart#positionXAxis method, otherwise a ChartListener#onXOverflow event is fired to handle the situation. null can also be specified, in which case, if the associated Y value is null the chart will be cleared, otherwise the update will be ignored.

Lifecycle: The X axis field can be set at any time. Until set, no chart will be printed. If already set, the new setting only affects the new points while all the previously plotted points are cleaned.

Parameters:
Name Type Attributes Description
field String

A field name representing the X axis.

xParser CustomParserFunction <optional>

A parser function that can be used to normalize the value of the X field before using it to plot the chart. If the function is not supplied, then the field values should represent valid numbers in JavaScript or be null.

setXLabels(labelsNum, labelsClassopt, labelsFormatteropt)

Setter method that configures the legend for the X axis. The legend consists of a specified number of labels for the values in the X axis. The labels values are determined based on the axis limits; the labels appearance is controlled by supplying a stylesheet and a formatter function.
Note that the room for the X axis labels on the page is not provided by the library; it should be provided by specifying a chart height smaller than the container element height, through the Chart#configureArea setting. Moreover, as the first and last labels are centered on the chart area borders, a suitable space should be provided also on the left and right of the chart area, through the same method.

Lifecycle: Labels can be configured at any time. If not set, no labels are displayed relative to the X axis.

Parameters:
Name Type Attributes Description
labelsNum Number

the number of labels to be spread on the X axis; it should be 1 or greater.

labelsClass String <optional>

the name of an existing stylesheet, to be applied to the X axis label HTML elements. The parameter is optional; if missing or null, then no specific stylesheet will be applied.

labelsFormatter LabelsFormatter <optional>

a Function instance used to format the X axis values designated for the labels. If the function is not supplied, then the value will be used with no further formatting.

Throws:

if labelsNum is not a valid poisitive integer number.

Type
IllegalArgumentException

updateRow(key, newValues)

Updates a row in the internal model and reflects the change on the view. If no row associated with the given key is found then a new row is created.
Example usage:
myWidget.updateRow("key1", {field1:"val1",field2:"val2"});

Lifecycle: once the AbstractWidget#parseHtml method has been called, this method can be used at any time. If called while an updateRow on the same internal model is still executing (e.g. if called while handling an onVisualUpdate callback), then the new update:

  • if pertaining to a different key and/or if called on a Chart instance, will be postponed until the first updateRow execution terminates;
  • if pertaining to the same key and if called on a StaticGrid / DynaGrid instance, will be merged with the current one.

Parameters:
Name Type Description
key String

The key associated with the row to be updated/added.

newValues Object

A JavaScript object containing name/value pairs to fill the row in the mode.
Note that the internal model does not have a fixed number of fields; each update can add new fields to the model by simply specifying them. Also, an update having fewer fields than the current model will have its missing fields considered as unchanged.

Inherited From:
Throws:

if parseHtml has not been executed yet.

Type
IllegalStateException