Data Feed
When placing a chart into a page one needs to first instantiate a feed. This is a central focal point through which most if not all data requests go through. As mentioned in the how to put chart on a page, the feed needs to be accessible from various places in the chart itself (please see a code example there).
The choice of <FeedClass> is crucial as it then dictates which other classes get used to fetch various data for the chart.
Here’s how the feed conceptually looks like
interface DataFeed { config: Config; getTimeSeriesDataSource(): TimeSeriesSource; getMetaDataSource(): MetaDataSource; getRecordSource(): RecordSource; addChart(elementId, symbolName); removeChart(elementId);}Configuration aside, we have three methods which give back data sources for three important data aspects - in all cases we’re talking interfaces for something which knows how to fetch:
TimeSeriesSource: timeseries data a.k.a. historical dataMetaDataSource: meta data a.k.a. profile dataRecordSource: quote data a.k.a. streaming data
Each implementation of a DataFeed will need to provide an implementation of these three interfaces. Since chart code only knows about the interface, it doesn’t matter whether the feed uses multiple or one and the same implementation of any and all of the data sources.
If you’re integrating the chart library into your own site, please see the dedicated page on library custom integration; otherwise read on for the description of Barchart’s feeds.
Built-in feeds
At the moment, there are two built-in feed implementations:
MarketDataFeedwhich supports using Barchart’s streaming APIBarchartSiteDataFeedwhich grabs profile and quote data directly from a page
Time series data
Both feed implementations use the same implementation for time series data source. This code issues HTTP(S) calls to fetch historical data; in order to avoid cross-site calls, all of the URLs are relative to the site hosting the page containing the chart. The list of the relative URLs can be seen in the config documented here. Any authentication matters are to be handled by the host.
Metadata (AKA profile data)
The MarketDataFeed will provide metadata using the Barchart’s streaming API library. The BarchartSiteDataFeed uses the data already in the page.
The data in this case needs to be in a well-known location which is at the moment a script tag of a special type and a fixed id, like this
<script type="application/json" id="barchart-www-inline-data"> { "ZCH6": { "instrument": { "exchange": "CBOT", "name": "Corn", "pointValue": 50, "tickIncrement": 2, "unitCode": "2" }, "quote": { "close": 381.5, "high": 381.75, "low": 376.25, "open": 376.75, "previousClose": 377, "volume": 127960 } }, "KCH6" : { ... }, ... }</script>So a script tag of ‘invalid’ type (‘application/json’ is not a known script so it won’t be executed) and a fixed id barchart-www-inline-data, contents of which is just JSON, keyed by symbol name.
For each symbol we provide two properties: instrument (profile data, yeah, lots of overlapping terms for the same thing) and quote (quote data).
The data in these is similar if not identical to what Barchart OnDemand would return if asked for the same thing.
The only fields actually needed (the rest is here for completeness) for the respective properties are:
- For instrument, please provide
exchange,nameandunitCode - For quote, please provide
previousClose
Requirements might change in the future so if you have the rest of the data as shown in the example feel free to include it.
Streaming data
If you need streaming data, use MarketDataFeed. In order to avoid the issue of credentials being provided to the chart, you will need to provide an initialized instance of the Connection from the Barchart’s API library to the chart, as soon as you can (in any case prior to adding charts to the page using the addChart call). Simply set it like this (assuming aFeed is the instance of the DataFeed and aConnection is the instance of a Connection) aFeed.connection = aConnection; then you can start adding charts to the page.