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

58.56% Statements 65/111
62.9% Branches 39/62
44% Functions 11/25
17.31% Lines 9/52
14 statements, 4 functions, 16 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                                                                                                                                                                                                                                                                                                                            
import d3 from 'd3';
import { unique } from 'datalib';
import VisComponent from '../../VisComponent';
import { read } from '../../util';
import * as upset from 'UpSet';
import template from './template.html';
 
export default class UpSet 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',
        optional: true,
        domain: {
          mode: 'field',
          from: 'data',
          fieldTypes: ['integer', 'boolean', 'string']
        }
      },
      {
        name: 'fields',
        type: 'string_list',
        format: 'string_list',
        optional: true,
        domain: {
          mode: 'field',
          from: 'data',
          fieldTypes: ['string', 'date', 'number', 'integer', 'boolean']
        }
      },
      {
        name: 'metadata',
        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;
    }
    if (!this.options.data || this.options.data.length === 0) {
      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) {
      const membershipVals = ['1', 'yes', 'true'];
      this.options.sets.forEach(s => header.push(s));
      this.options.data.forEach((d, i) => {
        this.options.sets.forEach(s => {
          let boolVal = '0';
          const strVal = ('' + d[s]).toLowerCase();
          if (membershipVals.indexOf(strVal) !== -1) {
            boolVal = '1';
          }
          data[i + 1].push(boolVal);
        });
      });
    }
 
    // 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]);
        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');
          });
        });
      });
    }
 
    const setsEnd = header.length - 1;
 
    let meta = [
      {
        type: 'id',
        index: 0,
        name: 'Name'
      }
    ];
 
    // Add metadata fields.
    if (this.options.metadata) {
      if (!this.options.data.__types__) {
        read(this.options.data);
      }
      const upsetTypeMap = {
        string: 'string',
        date: 'integer',
        number: 'float',
        integer: 'integer',
        boolean: 'integer'
      };
      this.options.metadata.forEach(field => {
        header.push(field);
        const type = upsetTypeMap[this.options.data.__types__[field]];
        meta.push({
          type: type,
          index: header.length - 1,
          name: field
        });
        this.options.data.forEach((d, i) => {
          data[i + 1].push('' + d[field]);
        });
      });
    }
 
    let datasets = [
      {
        name: 'data',
        data: data,
        header: 0,
        meta: meta,
        sets: [
          {
            format: 'binary',
            start: 1,
            end: setsEnd
          }
        ],
        author: '',
        description: '',
        source: ''
      }
    ];
 
    upset.UpSet(datasets);
    this.ui = new upset.Ui();
  }
}