chrt-object
Version:
Universal Object components of Chrt
41 lines (34 loc) • 1.24 kB
JavaScript
import {hasData} from '../../helpers';
export default function (data, accessor) {
// console.log('---------------> data', data, accessor, this);
if (!data) {
// console.log('NO DATA return', hasData(this), this._data, hasData(this) ? this._data : this)
return hasData(this) ? (this._data || null) : this;
}
// TODO: not sure what this is doing...
if(!hasData(this)) {
// console.log('NO HAS DATA')
return this;
}
// console.log('HAS DATA')
// // console.log('chrt or series', this.type)
// passing only accessor to inherit/reuse data
if(typeof arguments[0] === 'function') {
this._accessor = arguments[0];
return this;
}
// data is passed
this._orginalData = this._orginalData || data;
// define accessor function to map values
const accessorFunction = accessor || this._accessor;
this._accessor = accessorFunction;
this._data = (accessorFunction && !this._accessorApplied) ? data.map((d, i, arr) => {
if(d instanceof Object) {
return Object.assign({}, d, accessorFunction(d, i, arr));
}
return accessorFunction(d, i, arr);
}) : data;
// console.log('DATA', data)
this._accessorApplied = true; // do not apply the accessor multiple times
return this;
}