Skip to content

Commit

Permalink
Added chart object to event handler callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
martynasma committed May 26, 2015
1 parent ee7062c commit 3fc7a28
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
20 changes: 10 additions & 10 deletions dataloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Plugin Name: amCharts Data Loader
Description: This plugin adds external data loading capabilities to all amCharts libraries.
Author: Martynas Majeris, amCharts
Version: 1.0.4
Version: 1.0.5
Author URI: http://www.amcharts.com/
Copyright 2015 amCharts
Expand Down Expand Up @@ -172,7 +172,7 @@ AmCharts.addInitHandler( function( chart ) {

// error?
if ( false === response ) {
callFunction( options.error, url, options );
callFunction( options.error, options, chart );
raiseError( AmCharts.__( 'Error loading the file', chart.language ) + ': ' + url, false, options );
} else {

Expand All @@ -193,13 +193,13 @@ AmCharts.addInitHandler( function( chart ) {
holder[ providerKey ] = AmCharts.parseJSON( response );

if ( false === holder[ providerKey ] ) {
callFunction( options.error, options );
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 );
callFunction( options.load, options, chart );
}

break;
Expand All @@ -209,19 +209,19 @@ AmCharts.addInitHandler( function( chart ) {
holder[ providerKey ] = AmCharts.parseCSV( response, options );

if ( false === holder[ providerKey ] ) {
callFunction( options.error, options );
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 );
callFunction( options.load, options, chart );
}

break;

default:
callFunction( options.error, options );
callFunction( options.error, options, chart );
raiseError( AmCharts.__( 'Unsupported data format', chart.language ) + ': ' + options.format, false, options.noStyles );
return;
}
Expand All @@ -233,7 +233,7 @@ AmCharts.addInitHandler( function( chart ) {
if ( 0 === l.remaining ) {

// callback
callFunction( options.complete );
callFunction( options.complete, chart );

// take in the new data
if ( options.async ) {
Expand Down Expand Up @@ -400,9 +400,9 @@ AmCharts.addInitHandler( function( chart ) {
/**
* Execute callback function
*/
function callFunction( func, param1, param2 ) {
function callFunction( func, param1, param2, param3 ) {
if ( 'function' === typeof func )
func.call( l, param1, param2 );
func.call( l, param1, param2, param3 );
}

}, [ 'pie', 'serial', 'xy', 'funnel', 'radar', 'gauge', 'gantt', 'stock', 'map' ] );
Expand Down
2 changes: 1 addition & 1 deletion dataloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/serial2_json.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"dataLoader": {
"url": "data/serial2.json",
"showErrors": true,
"complete": function () {
"complete": function ( chart ) {
console.log( "Loading complete" );
},
"load": function ( options ) {
"load": function ( options, chart ) {
console.log( "File loaded: ", options.url );
},
"error": function ( options ) {
"error": function ( options, chart ) {
console.log( "Error occured loading file: ", options.url );
}
},
Expand Down
40 changes: 38 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# amCharts Data Loader

Version: 1.0.4
Version: 1.0.5


## Description
Expand Down Expand Up @@ -65,7 +65,7 @@ AmCharts.makeChart( "chartdiv", {
"dataSets": [{
...,
"dataLoader": {
"url": "data.csv"
"url": "data.csv",
"format": "csv",
"delimiter": ",", // column separator
"useColumnNames": true, // use first row for column names
Expand Down Expand Up @@ -162,6 +162,38 @@ Once chart is initialized, you can trigger the reload of all data manually by
calling `chart.dataLoader.loadData()` function. (replace "chart" with the actual
variable that holds reference to your chart object)

## Using callback functions

Data Loader can call your own function when certain event happens, like data
loading is complete, error occurs, etc.

To set custom event handlers, use these config options:

* "complete"

Example:

```
AmCharts.makeChart( "chartdiv", {
...,
"dataSets": [{
...,
"dataLoader": {
"url": "data.json",
"load": function ( options, chart ) {
console.log( 'Loaded file: ' + options.url );
},
"complete": function ( chart ) {
console.log( 'Woohoo! Finished loading' );
},
"error": function ( options, chart ) {
console.log( 'Ummm something went wrong loading this file: ' + options.url );
}
}
}]
} );
```


## Translating into other languages

Expand Down Expand Up @@ -238,6 +270,10 @@ http://www.apache.org/licenses/LICENSE-2.0

## Changelog

### 1.0.5
* Fixed JS error if periodSelector was not defined in chart config
* Now all callback functions (complete, error, load) receive additional parameter: chart

### 1.0.4
* Added chart.dataLoader.loadData() function which can be used to manually trigger all data reload

Expand Down

0 comments on commit 3fc7a28

Please sign in to comment.