123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633 |
- AmCharts.translations.dataLoader = {};
- AmCharts.addInitHandler( function( chart ) {
-
- if ( undefined === chart.dataLoader || !isObject( chart.dataLoader ) )
- chart.dataLoader = {};
-
- var version = chart.version.split( '.' );
- if ( ( Number( version[ 0 ] ) < 3 ) || ( 3 === Number( version[ 0 ] ) && ( Number( version[ 1 ] ) < 13 ) ) )
- return;
-
- var l = chart.dataLoader;
- l.remaining = 0;
-
- var defaults = {
- 'async': true,
- 'format': 'json',
- 'showErrors': true,
- 'showCurtain': true,
- 'noStyles': false,
- 'reload': 0,
- 'timestamp': false,
- 'delimiter': ',',
- 'skip': 0,
- 'useColumnNames': false,
- 'reverse': false,
- 'reloading': false,
- 'complete': false,
- 'error': false
- };
-
- l.loadData = function() {
-
- if ( 'stock' === chart.type ) {
-
- setTimeout( function() {
-
- if ( 0 > chart.panelsSettings.startDuration ) {
- l.startDuration = chart.panelsSettings.startDuration;
- chart.panelsSettings.startDuration = 0;
- }
-
- for ( var x = 0; x < chart.dataSets.length; x++ ) {
- var ds = chart.dataSets[ x ];
-
- if ( undefined !== ds.dataLoader && undefined !== ds.dataLoader.url ) {
- ds.dataProvider = [];
- applyDefaults( ds.dataLoader );
- loadFile( ds.dataLoader.url, ds, ds.dataLoader, 'dataProvider' );
- }
-
- if ( undefined !== ds.eventDataLoader && undefined !== ds.eventDataLoader.url ) {
- ds.events = [];
- applyDefaults( ds.eventDataLoader );
- loadFile( ds.eventDataLoader.url, ds, ds.eventDataLoader, 'stockEvents' );
- }
- }
- }, 100 );
- } else {
- applyDefaults( l );
- if ( undefined === l.url )
- return;
-
- if ( undefined !== chart.startDuration && ( 0 < chart.startDuration ) ) {
- l.startDuration = chart.startDuration;
- chart.startDuration = 0;
- }
-
- if ( undefined === chart.dataProvider )
- chart.dataProvider = chart.type === 'map' ? {} : [];
- loadFile( l.url, chart, l, 'dataProvider' );
- }
- };
-
- l.loadData();
-
- function loadFile( url, holder, options, providerKey ) {
-
- if ( undefined === providerKey )
- providerKey = 'dataProvider';
-
- if ( options.showCurtain )
- showCurtain( undefined, options.noStyles );
-
- l.remaining++;
-
- AmCharts.loadFile( url, options, function( response ) {
-
- if ( false === response ) {
- callFunction( options.error, options, chart );
- raiseError( AmCharts.__( 'Error loading the file', chart.language ) + ': ' + url, false, options );
- } else {
-
- if ( undefined === options.format ) {
-
- options.format = 'json';
- }
-
- options.format = options.format.toLowerCase();
-
- switch ( options.format ) {
- case 'json':
- holder[ providerKey ] = AmCharts.parseJSON( response );
- if ( false === holder[ providerKey ] ) {
- callFunction( options.error, options, chart );
- raiseError( AmCharts.__( 'Error parsing JSON file', chart.language ) + ': ' + l.url, false, options );
- holder[ providerKey ] = [];
- return;
- } else {
- holder[ providerKey ] = postprocess( holder[ providerKey ], options );
- callFunction( options.load, options, chart );
- }
- break;
- case 'csv':
- holder[ providerKey ] = AmCharts.parseCSV( response, options );
- if ( false === holder[ providerKey ] ) {
- callFunction( options.error, options, chart );
- raiseError( AmCharts.__( 'Error parsing CSV file', chart.language ) + ': ' + l.url, false, options );
- holder[ providerKey ] = [];
- return;
- } else {
- holder[ providerKey ] = postprocess( holder[ providerKey ], options );
- callFunction( options.load, options, chart );
- }
- break;
- default:
- callFunction( options.error, options, chart );
- raiseError( AmCharts.__( 'Unsupported data format', chart.language ) + ': ' + options.format, false, options.noStyles );
- return;
- }
-
- l.remaining--;
-
- if ( 0 === l.remaining ) {
-
- callFunction( options.complete, chart );
-
- if ( options.async ) {
- if ( 'map' === chart.type )
- chart.validateNow( true );
- else {
-
- chart.validateData();
-
- if ( l.startDuration ) {
- if ( 'stock' === chart.type ) {
- chart.panelsSettings.startDuration = l.startDuration;
- for ( var x = 0; x < chart.panels.length; x++ ) {
- chart.panels[ x ].startDuration = l.startDuration;
- chart.panels[ x ].animateAgain();
- }
- } else {
- chart.startDuration = l.startDuration;
- chart.animateAgain();
- }
- }
- }
- }
-
- if ( 'stock' === chart.type && !options.reloading && undefined !== chart.periodSelector )
- chart.periodSelector.setDefaultPeriod();
-
- removeCurtain();
- }
-
- if ( options.reload ) {
- if ( options.timeout )
- clearTimeout( options.timeout );
- options.timeout = setTimeout( loadFile, 1000 * options.reload, url, holder, options );
- options.reloading = true;
- }
- }
- } );
- }
-
- function postprocess( data, options ) {
- if ( undefined !== options.postProcess && isFunction( options.postProcess ) )
- try {
- return options.postProcess.call( this, data, options );
- } catch ( e ) {
- raiseError( AmCharts.__( 'Error loading file', chart.language ) + ': ' + options.url, false, options );
- return data;
- } else
- return data;
- }
-
- function isObject( obj ) {
- return 'object' === typeof( obj );
- }
-
- function isFunction( obj ) {
- return 'function' === typeof( obj );
- }
-
- function applyDefaults( obj ) {
- for ( var x in defaults ) {
- if ( defaults.hasOwnProperty( x ) )
- setDefault( obj, x, defaults[ x ] );
- }
- }
-
- function setDefault( obj, key, value ) {
- if ( undefined === obj[ key ] )
- obj[ key ] = value;
- }
-
- function raiseError( msg, error, options ) {
- if ( options.showErrors )
- showCurtain( msg, options.noStyles );
- else {
- removeCurtain();
- console.log( msg );
- }
- }
-
- function showCurtain( msg, noStyles ) {
-
- removeCurtain();
-
- if ( undefined === msg )
- msg = AmCharts.__( 'Loading data...', chart.language );
-
- var curtain = document.createElement( 'div' );
- curtain.setAttribute( 'id', chart.div.id + '-curtain' );
- curtain.className = 'amcharts-dataloader-curtain';
- if ( true !== noStyles ) {
- curtain.style.position = 'absolute';
- curtain.style.top = 0;
- curtain.style.left = 0;
- curtain.style.width = ( undefined !== chart.realWidth ? chart.realWidth : chart.divRealWidth ) + 'px';
- curtain.style.height = ( undefined !== chart.realHeight ? chart.realHeight : chart.divRealHeight ) + 'px';
- curtain.style.textAlign = 'center';
- curtain.style.display = 'table';
- curtain.style.fontSize = '20px';
- try {
- curtain.style.background = 'rgba(255, 255, 255, 0.3)';
- }
- catch ( e ) {
- curtain.style.background = 'rgb(255, 255, 255)';
- }
- curtain.innerHTML = '<div style="display: table-cell; vertical-align: middle;">' + msg + '</div>';
- } else {
- curtain.innerHTML = msg;
- }
- chart.containerDiv.appendChild( curtain );
- l.curtain = curtain;
- }
-
- function removeCurtain() {
- try {
- if ( undefined !== l.curtain )
- chart.containerDiv.removeChild( l.curtain );
- } catch ( e ) {
-
- }
- l.curtain = undefined;
- }
-
- function callFunction( func, param1, param2, param3 ) {
- if ( 'function' === typeof func )
- func.call( l, param1, param2, param3 );
- }
- }, [ 'pie', 'serial', 'xy', 'funnel', 'radar', 'gauge', 'gantt', 'stock', 'map' ] );
- if ( undefined === AmCharts.__ ) {
- AmCharts.__ = function( msg, language ) {
- if ( undefined !== language && undefined !== AmCharts.translations.dataLoader[ language ] && undefined !== AmCharts.translations.dataLoader[ language ][ msg ] )
- return AmCharts.translations.dataLoader[ language ][ msg ];
- else
- return msg;
- };
- }
- AmCharts.loadFile = function( url, options, handler ) {
-
- var request;
- if ( window.XMLHttpRequest ) {
-
- request = new XMLHttpRequest();
- } else {
-
- request = new ActiveXObject( 'Microsoft.XMLHTTP' );
- }
-
- request.onreadystatechange = function() {
- if ( 4 === request.readyState && 404 === request.status )
- handler.call( this, false );
- else if ( 4 === request.readyState && 200 === request.status )
- handler.call( this, request.responseText );
- };
-
- try {
- request.open( 'GET', options.timestamp ? AmCharts.timestampUrl( url ) : url, options.async );
- request.send();
- } catch ( e ) {
- handler.call( this, false );
- }
- };
- AmCharts.parseJSON = function( response ) {
- try {
- if ( undefined !== JSON )
- return JSON.parse( response );
- else
- return eval( response );
- } catch ( e ) {
- return false;
- }
- };
- AmCharts.parseCSV = function( response, options ) {
-
- var data = AmCharts.CSVToArray( response, options.delimiter );
-
- var res = [];
- var cols = [];
- var col, i;
-
- if ( options.useColumnNames ) {
- cols = data.shift();
-
- for ( var x = 0; x < cols.length; x++ ) {
-
- col = cols[ x ].replace( /^\s+|\s+$/gm, '' );
-
- if ( '' === col )
- col = 'col' + x;
- cols[ x ] = col;
- }
- if ( 0 < options.skip )
- options.skip--;
- }
-
- for ( i = 0; i < options.skip; i++ )
- data.shift();
-
- var row;
- while ( ( row = options.reverse ? data.pop() : data.shift() ) ) {
- var dataPoint = {};
- for ( i = 0; i < row.length; i++ ) {
- col = undefined === cols[ i ] ? 'col' + i : cols[ i ];
- dataPoint[ col ] = row[ i ];
- }
- res.push( dataPoint );
- }
- return res;
- };
- AmCharts.CSVToArray = function( strData, strDelimiter ) {
-
-
- strDelimiter = ( strDelimiter || "," );
-
- var objPattern = new RegExp(
- (
-
- "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
-
- "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
-
- "([^\"\\" + strDelimiter + "\\r\\n]*))"
- ),
- "gi"
- );
-
-
- var arrData = [
- []
- ];
-
-
- var arrMatches = null;
-
-
- while ( ( arrMatches = objPattern.exec( strData ) ) ) {
-
- var strMatchedDelimiter = arrMatches[ 1 ];
-
-
-
-
- if (
- strMatchedDelimiter.length &&
- ( strMatchedDelimiter !== strDelimiter )
- ) {
-
-
- arrData.push( [] );
- }
-
-
-
- var strMatchedValue;
- if ( arrMatches[ 2 ] ) {
-
-
- strMatchedValue = arrMatches[ 2 ].replace(
- new RegExp( "\"\"", "g" ),
- "\""
- );
- } else {
-
- strMatchedValue = arrMatches[ 3 ];
- }
-
-
- arrData[ arrData.length - 1 ].push( strMatchedValue );
- }
-
- return ( arrData );
- };
- AmCharts.timestampUrl = function( url ) {
- var p = url.split( '?' );
- if ( 1 === p.length )
- p[ 1 ] = new Date().getTime();
- else
- p[ 1 ] += '&' + new Date().getTime();
- return p.join( '?' );
- };
|