DataSource
Type class
Represents a single data source - external file with all of its settings, such as format, data parsing, etc.
chart.dataSource.url = "http://www.myweb.com/data.json";
chart.dataSource.parser = am4core.JSONParser;
chart.dataSource.url = "http://www.myweb.com/data.json";
chart.dataSource.parser = am4core.JSONParser;
{
// ...
"dataSource": {
"url": "http://www.myweb.com/data.json",
"parser": "JSONParser"
},
// ...
}
More info: IDataSourceEvents for a list of available events
More info: IDataSourceAdapters for a list of available Adapters
Sources
DataSource can be used (imported) via one of the following packages.
/** * Import from: "core.ts" * Use like: am4core.DataSource */import * as am4core from "@amcharts/amcharts4/core";/** * Include via: <script src="core.js"></script> * Access items like: am4core.DataSource */Inheritance
DataSource extends BaseObjectEvents.
DataSource is not extended by any other symbol.
Properties
adapterType
Adapter<DataSource,IDataSourceAdapters>Defaultnew Adapter<DataSource, IDataSourceAdapters>(this)Adapter.
componentType
ComponentA
Componentrecipient of the data.dataType
anyLoaded and parsed data.
dateFormatterType
DateFormatterA
DateFormatterto use when parsing dates from string formats.Will inherit and use chart's DateFormatter if not ser.
disableCacheType
booleanIf set to
trueit will timestamp all requested URLs to work around browser cache.incrementalType
booleanDefaultfalseShould subsequent reloads be treated as incremental?
Incremental loads will assume that they contain only new data items since the last load.
If
incremental = falsethe loader will replace all of the target's data with each load.This setting does not have any effect trhe first time data is loaded.
NOTE: this setting works only with element's
dataproperty. It won't work with any other externally-loadable data property.incrementalParamsType
objectAn object consisting of key/value pairs to apply to an URL when data source is making an incremental request.
keepCountType
booleanDefaultfalseThis setting is used only when
incremental = true. If set totrue, it will try to retain the same number of data items across each load.E.g. if incremental load yeilded 5 new records, then 5 items from the beginning of data will be removed so that we end up with the same number of data items.
languageType
LanguageLanguage instance to use.
Will inherit and use chart's language, if not set.
lastLoadType
DateHolds the date of the last load.
parserType
DataParserDefaultJSONParserA parser to be used to parse data.
chart.dataSource.url = "http://www.myweb.com/data.json"; chart.dataSource.parser = am4core.JSONParser;chart.dataSource.url = "http://www.myweb.com/data.json"; chart.dataSource.parser = am4core.JSONParser;{ // ... "dataSource": { "url": "http://www.myweb.com/data.json", "parser": { "type": "JSONParser" } }, // ... }reloadFrequencyType
numberData source reload frequency.
If set, it will reload the same URL every X milliseconds.
requestOptionsType
INetRequestOptionsCustom options for HTTP(S) request.
At this moment the only option supported is:
requestHeaders, which holds an array of objects for custom request headers, e.g.:chart.dataSource.requestOptions.requestHeaders = [{ "key": "x-access-token", "value": "123456789" }];chart.dataSource.requestOptions.requestHeaders = [{ "key": "x-access-token", "value": "123456789" }];{ // ... "dataSource": { // ... "requestOptions": { "requestHeaders": [{ "key": "x-access-token", "value": "123456789" }] } } }NOTE: setting this options on an-already loaded DataSource will not trigger a reload.
showPreloaderType
booleanDefaulttrueWill show loading indicator when loading files.
updateCurrentDataType
booleanDefaultfalseSince 2.5.5If set to
true, each subsequent load will be treated as an update to currently loaded data, meaning that it will try to update values on existing data items, not overwrite the whole data.This will work faster than complete update, and also will animate the values to their new positions.
Data sources across loads must contain the same number of data items.
Loader will not truncate the data set if loaded data has fewer data items, and if it is longer, the excess data items will be ignored.
NOTE: this setting is ignored if
incremental = true.urlType
stringURL of the data source.
5 inherited items
clonedFromType
$type.Optional<this>Inherited fromBaseObjectReference to the original object this object was cloned from. We need to keep this so we can disassociate it from source object when this object is disposed.
configType
objectInherited fromBaseObjectUse this property to set JSON-based config. When set, triggers processing routine, which will go through all properties, and try to apply values, create instances, etc.
Use this with caution, as it is a time-consuming process. It's used for initialchart setup only, not routine operations.
eventsType
EventDispatcher<AMEvent<this,IBaseObjectEvents>>Inherited fromBaseObjectEventsAn
EventDispatcherinstanceidType
$type.Optional<string>Inherited fromBaseObjectSets the user-defined id of the element.
uidType
stringInherited fromBaseObjectReturns object's internal unique ID.
Methods
addUrlParams(url:string,
params:object)Returns
stringAdds parameters to
urlas query strings. Will take care of proper separators.constructor(url?:string,
parser?:string|DataParser)Returns
DataSourceConstructor
dispose()Returns
voidDisposes of this object.
load()Returns
voidInitiate the load.
All loading in JavaScript is asynchronous. This function will trigger the load and will exit immediately.
Use DataSource's events to watch for loaded data and errors.
timestampUrl(url:string)Returns
stringAdds current timestamp to the URL.
6 inherited items
clone(cloneId?:string)Returns
thisInherited fromBaseObjectMakes a copy of this object and returns the clone. Try to avoid cloning complex objects like chart, create new instances if you need them.
copyFrom(source:this)Returns
voidInherited fromBaseObjectEventsCopies all parameters from another
Sprite.dispatch(eventType:Key,
data?:any)Returns
voidInherited fromBaseObjectEventsDispatches an event using own event dispatcher. Will automatically populate event data object with event type and target (this element).
It also checks if there are any handlers registered for this sepecific event.
dispatchImmediately(eventType:Key,
data?:any)Returns
voidInherited fromBaseObjectEventsWorks like
dispatch, except event is triggered immediately, without waiting for the next frame cycle.getCurrentThemes()Returns
ITheme[]Inherited fromBaseObjectReturns a list of themes that should be applied to this element. It could either be a list of themes set explicitly on this element, or system-wide.
isDisposed()Returns
booleanInherited fromBaseObjectReturns if this object has been already been disposed.
Events
doneParam { type:
"done", target:DataSource, data:any}Invoked when data source was successfully loaded and parsed.
endedParam { type:
"ended", target:DataSource}Invoked when loading and parsing finishes.
errorParam { type:
"error", target:DataSource, code:number, message:string}Invoked when data source encounters a loading error.
loadendedParam { type:
"loadended", target:DataSource}Invoked when the loading of the data finishes.
loadstartedParam { type:
"loadstarted", target:DataSource}Invoked when loading of the data starts.
parseendedParam { type:
"parseended", target:DataSource}Invoked when parsing of the loaded data finishes.
parseerrorParam { type:
"parseerror", target:DataSource, message:string}Invoked when data source encounters a parsing error.
parsestartedParam { type:
"parsestarted", target:DataSource}Invoked when parsing of the loaded data starts.
startedParam { type:
"started", target:DataSource}Invoked when loading of the data starts.
Adapters
dateFieldsParam
string[]Applied to the array that lists fields in data that hold date-based values.
incrementalParam
booleanApplied to
incrementalsetting.incrementalParamsParam
objectApplied to
incrementalParamssetting.keepCountParam
booleanApplied to
keepCountsetting.numberFieldsParam
string[]Applied to the array that lists fields in data that hold numeric values.
parsedDataParam
anyApplied to the loaded data after it was parsed by a parser.
parserParam
DataParserApplied to a parser type, before parsing starts.
Can be used to supply different parser than the one set/determined by Data Loader.
parserOptionsParam
anyApplied to parser options.
reloadTimeoutParam
numberApplied to the timeout setting.
requestOptionsParam
INetRequestOptionsApplied to the custom request options object.
unparsedDataParam
stringApplied to the loaded data before it is passed to parser.
updateCurrentDataParam
booleanApplied to
updateCurrentDatasetting.urlParam
stringApplied to a data source URL before it is loaded.