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

63.16% Statements 48/76
66.67% Branches 22/33
36.84% Functions 7/19
21.88% Lines 7/32
6 statements, 1 function, 6 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 dl from 'datalib';
    import * as upset from 'UpSet';
    import template from './template.html';

    export default class UpSet {
      static get spec () {
        return {
          options: [
            {
              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) {
        this.el = el;
        this.options = options;
      }

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

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

        // Swizzle the data into what UpSet expects (array of arrays)
        let data = [];
        let header = [this.options.id];
        data.push(header);
        this.options.data.forEach(d => {
          data.push([d[this.options.id]]);
        });

        // Add 0/1 sets.
        if (this.options.sets) {
          this.options.sets.forEach(s => header.push(s));
          this.options.data.forEach((d, i) => {
            this.options.sets.forEach(s => {
              data[i + 1].push('' + d[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 = dl.unique(this.options.data, d => d[field]);
            distinct.forEach(v => header.push(field + ' ' + v));
            this.options.data.forEach((d, i) => {
              distinct.forEach(v => {
                data[i + 1].push(v === d[field] ? '1' : '0');
              });
            });
          });
        }

        let datasets = [
          {
            name: 'data',
            data: data,
            header: 0,
            meta: [
              {
                type: 'id',
                index: 0,
                name: 'Name'
              }
            ],
            sets: [
              {
                format: 'binary',
                start: 1,
                end: header.length - 1
              }
            ],
            author: '',
            description: '',
            source: ''
          }
        ];

        upset.UpSet(datasets);
        this.ui = new upset.Ui();
      }
    }