Updating Chart using Object Model

Overview

The AnyChart Stock component offers a unique and convenient way to update configuration of chart embedded in a page dynamically. Once the component is embedded in the page and configured one way or the other, you can obtain its current configuration as a JavaScript Object, then edit the required settings and pass the command to the component to commit the changes.

Unlike with the setConfig() and setSettings() methods, direct interaction with object model allows avoiding the forming of the complete configuration, as object model is already stored in the Flash Object and is available for editing.

to top

Obtaining Access to Configuration Object Model

To obtain access to configuration object model, when embedding the chart you need to explicitly "tell" the component that we are going to need access to the object model.

To do so, set the needConfig property of the JS instance of the AnyChartStock class to true before calling the write() method. Here is a listing of the code that demonstrates turning on this flag:

Once the chart has been initialized, the component converts the passed configuration XML to the JSON format and saves it in the objectModel property. If the initial chart configuration used JSON, it is simply passed to that property.

The default value of the objectModel property is null; the object model gets there only after the entire cycle of the component initialization is over. That point can be traced by the occurrence of the onChartDraw event; i.e. when this event occurs, the object model becomes available for further operations.

Here is a custom sample demonstrating manipulations over the objectModel property. When the chart is initialized, it throws the content of the objectModel property to the text block under the chart:

Online HTML/JavaScript Sample

to top

Changing Settings and Committing Changes

Once you have obtained the component configuration object model, you can manipulate it any way you want: add new elements, delete or modify them.

For a chart modification example you can take changing some settings in a series. The listing below demonstrates an example of changing series color and type:

Once the modifications in the configuration object model are made, they need to be committed. There are three methods for committing changes:

Sample 1: Data Update. Here is a custom sample that demonstrates modifying data and settings in both objects. In the sample, clicking on the "Add" button adds new data set, data provider and series using that data to the object model. To commit the changes, the sample uses the applyConfigChanges() method:

Online HTML/JavaScript Sample

Sample 2: Changing Series Settings. Another custom sample demonstrates committing changes with the applySettingsChanges() method. This sample demonstrates the modification of the series type, color and name:

Online HTML/JavaScript Sample

Sample 3: Event Markers Update. Custom sample shows changing event markers and using of applyEventMarkersChanges():

Online HTML/JavaScript Sample

to top

Helper Methods for Working with Object Model

For your convenience, JavaScript API offers a number of methods for obtaining object model for some elements of chart quickly, by their identifiers, for further modification.

For example, if you need to change the color for a certain series, you can obtain the object of the series with the getSeriesById() method, then modify just one property, color, and then commit the change with the applySettingsChanges() method.

Here are the methods available in the AnyChart Stock class:

To obtain access to the object of a specific element, you must set its identifier in the primary configuration. Here is a listing that demonstrates setting identifiers for all the elements that we need to obtain access to:

XML/JSON Syntax
Plain code
01 <?xml version="1.0" encoding="UTF-8"?>
02 <stock xmlns="http://anychart.com/products/stock/schemas/1.9.0/schema.xsd">
03   <event_markers>
04     <groups>
05       <group id="idKeyDevelopments">
06         <events>
07           <event id="idKeyEvent" />
08         </events>
09       </group>
10     </groups>
11   </event_markers>
12   <settings>
13     <charts>
14       <chart id="idMainChart">
15         <series_list>
16           <series id="idSeriesA" />
17           <series id="idSeriesB" />
18         </series_list>
19         <technical_indicators>
20           <technical_indicator id="idSmaIndicator" />
21         </technical_indicators>
22       </chart>
23     </charts>
24   </settings>
25 </stock>
01{
02  eventMarkers: {
03    groups: [
04      {
05        id: "idKeyDevelopments",
06        events: [
07          {
08            id: "idKeyEvent"
09          }
10        ]
11      }
12    ]
13  },
14  settings: {
15    charts: [
16      {
17        id: "idMainChart",
18        seriesList: [
19          {
20            id: "idSeriesA"
21          },
22          {
23            id: "idSeriesB"
24          }
25        ],
26        technicalIndicators: [
27          {
28            id: "idSmaIndicator"
29          }
30        ]
31      }
32    ]
33  }
34}

Here is a custom sample demonstrating the functioning of these methods. Clicking on a button throws the object model of the corresponding element to the text block under the chart:

Online HTML/JavaScript Sample

to top

Event Markers Specifics

When you work with event markers in Object Model you should note one important thing: there are two ways to set timestamp for event marker, as described in Event Markers: Difference between date and locale_based_date article, but no matter how you set it - in object model you should always use date property and this property is an instance of Java Script Date class.

For example, Java Script code to read event marker date can be:

Sample code to set new date:

In this sample you can see how the date is read:

Online HTML/JavaScript Sample

And in this sample you can see how event markers date is updated:

Online HTML/JavaScript Sample

to top

Object Model Update Samples

This section provides the most common chart modification samples.

Turning On/Off Additional Charts Dynamically

The simplest way to turn on additional charts is to toggle the enabled attribute in the <chart> node. Setting this attribute to false tells the component to ignore this chart when calling the new settings commitment methods, and the chart doesn't appear on the canvas. By default, the enabled attribute for all charts is set to true.

Here is a custom sample that demonstrates the use of that technique. The sample uses four charts simultaneously; toggling a checkbox to the right of the component hides or shows the corresponding chart:

Online HTML/JavaScript Sample

to top

Switching Y-Axis Modes

Another sample that can be considered common is switching axis mode for displaying absolute values, value differences and percentage difference. Switching axis mode affects many elements, which are to be configured for operating in the new mode.

When switching mode, it is expedient to change the format of presenting the series in the legend, tooltips and also replace the format of the Y-Axis labels.

The custom sample below demonstrates the option for switching Y-Axis mode on the fly; it modifies all the above mentioned elements for presenting the data with allowance for the selected mode:

Online HTML/JavaScript Sample

to top

Turning On/Off Series Dynamically

To hide or show data series dynamically, use the enabled attribute in the <series> node. This attribute can accept the values true - the series is visible - and false - the series is hidden.

The custom sample below demonstrates the operations with this attribute. The sample page has four checkboxes to the right of the chart; each of them enables/disables one or the other data series:

Online HTML/JavaScript Sample

to top

Turning On/Off Technical Indicators Dynamically

Technical indicators fall into two groups: overlay, i.e. indicators drawn on top of data series they use as source of data; external - that use separate chart for the visualization.

To enable or disable overlay indicators, such as SMA, EMA, BBands and Parabolic SAR, use the enabled attribute in the <technical_indicator> node. This attribute works the same way as it does for the <series> node.

To enable or disable external indicators, such as Volume, Stochastic Oscillator, MACD, etc., you need to enable/disable the chart with that indicator, not the indicator itself.

Here is a custom sample that demonstrates the option for enabling/disabling both overlay and external indicators dynamically:

Online HTML/JavaScript Sample

to top

Changing Technical Indicator Settings

To change technical indicator settings on the fly, modify the required settings for the selected indicator and then call the applySettingsChanges() method.

Let's take the Simple Moving Average indicator for an example. This indicator has the only setting - period. Here is a listing that demonstrates how that setting can be modified:

Here is a custom sample that demonstrates changing settings for technical indicators using the same principle:

Online HTML/JavaScript Sample

to top

Comparing Data Series

Comparing data series could be a very common case too. This scenario uses one primary series - it appears on both the chart and the scroller - and one or several data series, which the primary one is compared with.

In this scenario, the primary series doesn't differ technically from the other ones; that is only a matter of meaning. For comparing value changes or percentage changes in data series, you need to simply switch Y-Axis to the required mode, as it was demonstrated in the Switching Y-Axis Modes sample.

Here is a custom sample demonstrating the classic series comparison routine. On the drop-down menu, select the tick of the company that is going to be the primary series - it will appear first on the legend list, and the scroller will display the data for the company. Now you can select which companies you would like to compare the selected company's stock quotes:

Online HTML/JavaScript Sample

to top