# all files / candela/components/OnSet/ index.js

57.14% Statements 52/91
59.62% Branches 31/52
41.67% Functions 10/24
15.56% Lines 7/45
13 statements, 4 functions, 13 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    1231×
    1×
    1×
    1×

    1×

    1×

     import d3 from 'd3';
    import { unique } from 'datalib';
    import onset from 'onset';
    import VisComponent from '../../VisComponent';

    export default class OnSet extends VisComponent {
      static get options () {
        return [
          {
            name: 'data',
            type: 'table',
            format: 'objectlist'
          },
          {
            name: 'id',
            type: 'string',
            format: 'text',
            domain: {
              mode: 'field',
              from: 'data',
              fieldTypes: ['string', 'date', 'number', 'integer', 'boolean']
            }
          },
          {
            name: 'sets',
            type: 'string_list',
            format: 'string_list',
            domain: {
              mode: 'field',
              from: 'data',
              fieldTypes: ['integer', 'boolean']
            }
          },
          {
            name: 'fields',
            type: 'string_list',
            format: 'string_list',
            domain: {
              mode: 'field',
              from: 'data',
              fieldTypes: ['string', 'date', 'number', 'integer', 'boolean']
            }
          }
        ];
      }

      constructor (el, options) {
        super(el);
        this.options = options;
      }

      render () {
        if (!this.options.id || (!this.options.sets && !this.options.fields)) {
          return;
        }

        d3.select(this.el).html(onset.template);

        // Swizzle the data into what OnSet expects (csv of form id,set1,set2,...)
        let data = [];
        this.options.data.forEach(d => {
          data.push([d[this.options.id]]);
        });

        // Add 0/1 sets.
        if (this.options.sets) {
          const membershipVals = ['1', 'yes', 'true'];
          this.options.data.forEach((d, i) => {
            this.options.sets.forEach(s => {
              const strVal = ('' + d[s]).toLowerCase();
              if (membershipVals.indexOf(strVal) !== -1) {
                data[i].push(s);
              }
            });
          });
        }

        // Add sets derived from general fields.
        // A set is defined by records sharing a field value.
        if (this.options.fields) {
          this.options.fields.forEach(field => {
            let distinct = unique(this.options.data, d => d[field]);
            this.options.data.forEach((d, i) => {
              distinct.forEach(v => {
                if (v === d[field]) {
                  data[i].push(field + ' ' + v);
                }
              });
            });
          });
        }

        let csvData = '';
        if (this.options.rowSets) {
          data.forEach(d => {
            csvData += d.join(',') + '\n';
          });
        } else {
          let sets = {};
          data.forEach(d => {
            d.forEach((s, i) => {
              if (i === 0) {
                return;
              }
              if (sets[s] === undefined) {
                sets[s] = [s];
              }
              sets[s].push(d[0]);
            });
          });
          Object.keys(sets).forEach(s => {
            csvData += sets[s].join(',') + '\n';
          });
        }

        window.sessionStorage.setItem('datatype', 'custom');
        window.sessionStorage.setItem('data', csvData);

        onset.main();
      }
    }