UNPKG

jiminy

Version:

Library for inferring which type of data visualization can be rendered from a JSON dataset, and with which data field(s)

179 lines (132 loc) 5.68 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _chart = require('chart'); var _chart2 = _interopRequireDefault(_chart); var _stattype = require('stattype'); var _stattype2 = _interopRequireDefault(_stattype); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Charts = function () { function Charts(config) { _classCallCheck(this, Charts); if (!config || !Array.isArray(config) || !config.length) { throw new Error('Jiminy: You must pass a non-empty chart configuration.'); } this._config = this._parseConfig(config); this._charts = this._createCharts(); } _createClass(Charts, [{ key: '_createCharts', /* Create the Chart instances */ value: function _createCharts() { var charts = []; for (var i = 0, j = this._config.length; i < j; i++) { charts.push(new _chart2.default(this._config[i])); } return charts; } /* This method is tested within the constructor */ }, { key: '_parseConfig', value: function _parseConfig(config) { var charts = []; var _loop = function _loop(i, j) { var chartConfig = config[i]; if (!chartConfig.name || typeof chartConfig.name !== 'string' || !chartConfig.name.length) { console.warn('Jiminy: Each chart must have a name.'); return 'continue'; } if (!chartConfig.acceptedStatTypes || !Array.isArray(chartConfig.acceptedStatTypes) || !chartConfig.acceptedStatTypes.length) { console.warn('Jiminy: Each chart must have a set of rules.'); return 'continue'; } var rules = []; for (var k = 0, l = chartConfig.acceptedStatTypes.length; k < l; k++) { var rule = chartConfig.acceptedStatTypes[k]; if (!rule || !Array.isArray(rule) || !rule.length) { console.warn('Jiminy: A rule for chart "' + chartConfig.name + '" ' + 'has been ignored because invalid.'); continue; } if (rule.length > 2) { console.warn('Jiminy: A rule for chart "' + chartConfig.name + '" ' + 'has been ignored because it owns more than two statistical ' + 'types.'); continue; } var validStatTypes = 0; for (var m = 0, n = rule.length; m < n; m++) { if (typeof rule[m] !== 'string' || !~_stattype2.default.types.indexOf(rule[m])) { console.warn('Jiminy: A rule for chart "' + chartConfig.name + '" ' + 'has been ignored because a statistical type isn\'t valid.'); continue; } validStatTypes++; } if (validStatTypes === rule.length) { rules.push(rule.slice(0)); } } /* We tag the chart as valid if this code has been reached and at least * one statistical rule is valid */ if (rules.length > 0) { if (charts.filter(function (chart) { return chart.name === chartConfig.name; }).length) { console.warn('Jiminy: A chart has been ignored because it ' + ('already exists another with name "' + chartConfig.name + '".')); return 'continue'; } charts.push({ name: chartConfig.name, acceptedStatTypes: rules }); } }; for (var i = 0, j = config.length; i < j; i++) { var _ret = _loop(i, j); if (_ret === 'continue') continue; } if (!charts.length) { throw new Error('Jiminy: The chart configuration is empty or malformed.'); } return charts; } /* Return the available charts according to the available fields. * Options can contain: * - allInclusive (boolean): the charts must be computed with all the columns * (not just some of them) */ }, { key: 'getAvailable', value: function getAvailable(fields, options) { var available = []; options = options || {}; if (!fields || !fields.length) { throw new Error('Jiminy: At least one field is required to compute the available charts.'); } for (var i = 0, j = this._charts.length; i < j; i++) { var _options = {}; if (_options.allInclusive) _options.allInclusive = true; if (this._charts[i].isAvailable(fields, _options)) { available.push(this._charts[i]); } } return available; } /* Return the chart called chartName if exists, null otherwise */ }, { key: 'getChart', value: function getChart(chartName) { for (var i = 0, j = this._charts.length; i < j; i++) { if (this._charts[i].name === chartName) return this._charts[i]; } return null; } }, { key: 'charts', get: function get() { return this._charts; } }]); return Charts; }(); exports.default = Charts;