-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add polling_interval option to Charts
Fixes #544
- Loading branch information
Julien Castelain
committed
Mar 7, 2018
1 parent
3afc46f
commit caef9ca
Showing
6 changed files
with
197 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import {isFunction, isObject, isString} from 'metal'; | ||
import {Config} from 'metal-state'; | ||
import types from './utils/types'; | ||
|
||
/** | ||
* DataComponent prototype. | ||
* | ||
* @mixin | ||
*/ | ||
const DataComponent = { | ||
/** | ||
* Check's the data or columns option and resolves this `data_` accordingly. | ||
* @return {Promise} | ||
* @param {?Array|Function|String} data the data to resolve. | ||
* @protected | ||
*/ | ||
_resolveData(data) { | ||
if (Array.isArray(data) || (isObject(data) && !isFunction(data))) { | ||
return Promise.resolve(data); | ||
} else if (isFunction(data)) { | ||
return data().then(val => val); | ||
} else if (isString(data)) { | ||
return fetch(data, {cors: 'cors'}) | ||
.then(res => res.json()) | ||
.then(res => res.data); | ||
} else { | ||
Promise.reject(`Invalid type for data: ${data}`); | ||
} | ||
}, | ||
|
||
/** | ||
* Sets up the polling interval. | ||
*/ | ||
_setupPolling() { | ||
if (this.pollingInterval) { | ||
if (this._pollingInterval) { | ||
clearInterval(this._pollingInterval); | ||
} | ||
|
||
this._pollingInterval = setInterval(() => { | ||
this._updateData(this.data); | ||
}, this.pollingInterval); | ||
} | ||
}, | ||
}; | ||
|
||
/** | ||
* State definition. | ||
* @static | ||
* @type {!Object} | ||
*/ | ||
DataComponent.STATE = { | ||
/** | ||
* Data that will be rendered to the chart. | ||
* @instance | ||
* @memberof ChartBase | ||
* @type {?Array|undefined} | ||
* @default undefined | ||
*/ | ||
data: Config.oneOfType([ | ||
Config.arrayOf( | ||
Config.shapeOf({ | ||
axis: Config.oneOf(['y', 'y2']), | ||
class: Config.string(), | ||
color: Config.string(), | ||
data: Config.array().required(), | ||
hide: Config.bool(), | ||
id: Config.required().string(), | ||
name: Config.string(), | ||
regions: Config.array(), | ||
type: Config.oneOf(types.all), | ||
x: Config.string(), | ||
}) | ||
), | ||
Config.object(), | ||
Config.func(), | ||
Config.string(), | ||
]), | ||
|
||
/** | ||
* Set an interval (in ms) to fetch the data. | ||
* @instance | ||
* @memberof ChartBase | ||
* @type {?Number} | ||
* @default undefined | ||
*/ | ||
pollingInterval: Config.number(), | ||
}; | ||
|
||
export {DataComponent}; | ||
export default DataComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.