UNPKG

jiminy

Version:

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

126 lines (93 loc) 4.39 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 _dataset = require('dataset'); var _dataset2 = _interopRequireDefault(_dataset); var _fields = require('fields'); var _fields2 = _interopRequireDefault(_fields); var _charts = require('charts'); var _charts2 = _interopRequireDefault(_charts); 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 Jiminy = function () { function Jiminy(json, chartConfig) { _classCallCheck(this, Jiminy); this._dataset = new _dataset2.default(json); this._fields = new _fields2.default(this._dataset); this._charts = new _charts2.default(chartConfig); } _createClass(Jiminy, [{ key: 'recommendation', /* If fieldNames is present, only return the charts that can be obtained by * the combination of the passed column names. Otherwise, return the types of * charts that can be obtained with any of the columns of the dataset. * NOTE: if fieldNames is set and contains several names, it won't return the * charts obtained with just one of the specified columns. */ value: function recommendation(fieldNames) { var fields = []; var allColumnsInclusive = false; /* All the columns must be used */ if (fieldNames !== null && fieldNames !== undefined) { if (!Array.isArray(fieldNames)) { throw new Error('Jiminy: recommendation should be called without any ' + 'parameter or with an array of column names.'); } else if (fieldNames.length) { allColumnsInclusive = true; fields = this._fields.get(fieldNames); } } if (!fields.length) fields = this._fields.fields; var options = { allInclusive: allColumnsInclusive }; this._availableCharts = this._charts.getAvailable(fields, options); return this._availableCharts.map(function (chart) { return chart.name; }); } /* Return the columns that can be used to compute the chart. If chartName * doesn't correspond to any chart or isn't available, throw an error. */ }, { key: 'columns', value: function columns(chartName, columnName) { if (!chartName) { throw new Error('Jiminy: columns expects the name of the chart as first argument.'); } var chart = this._charts.getChart(chartName); if (!chart) { throw new Error(chartName + ' isn\'t a valid chart name. ' + 'Check the documentation to see existing types of charts.'); } if (columnName !== null && columnName !== undefined && typeof columnName !== 'string') { throw new Error('Jiminy: The second parameter of columns must be a string.'); } /* We want the suggestions for the first column */ if (!columnName) { return chart.computeUsefulFields(this._fields.fields).map(function (field) { return field.name; }); } /* We want the suggestions for the second one */ /* We check if the name of the column exists */ if (!~this._dataset.getColumnNames().indexOf(columnName)) { throw new Error('Unable to find the column "' + columnName + '" inside the dataset'); } var field = this._fields.get([columnName])[0]; return chart.computeUsefulFields(this._fields.fields, field).map(function (field) { return field.name; }); } }, { key: 'name', get: function get() { return 'Jiminy'; } }, { key: 'version', get: function get() { return '0.1.1'; } }]); return Jiminy; }(); exports.default = Jiminy; module.exports = exports.default;