parseStyle()

in src/app/services/styles.service.ts [143:201]


  parseStyle(propName: string, row: object, rule: StyleRule) {
    const prop = StyleProps.find((p) => p.name === propName);
    let scale = this.scaleCache.get(rule);

    if (!rule.isComputed) {
      // Static value.
      return rule.value
        ? prop.parse(rule.value)
        : DEFAULT_STYLES[propName];

    } else if (!rule.property || !rule.function) {
      // Default value.
      return DEFAULT_STYLES[propName];

    } else if (rule.function === 'identity') {
      // Identity function.
      return prop.parse(row[rule.property]);

    } else if (rule.function === 'categorical') {
      // Categorical function.
      if (!scale) {
        const range = <any[]>rule.range.map((v) => prop.parse(v));
        scale = d3Scale.scaleOrdinal<string>()
          .domain(rule.domain)
          .range(range)
          .unknown(DEFAULT_STYLES[propName]);
        this.scaleCache.set(rule, scale);
      }
      const callableScale = scale as (any) => any;
      return callableScale(row[rule.property]);

    } else if (rule.function === 'interval') {
      // Interval function.
      if (!scale) {
        const range = <any[]>rule.range.map((v) => prop.parse(v));
        const tmpScale = d3Scale.scaleThreshold<number, any>()
          .domain(rule.domain.map(Number))
          .range([...range, DEFAULT_STYLES[propName]]);
        scale = tmpScale as any as d3Scale.ScaleThreshold<number, any>;
        this.scaleCache.set(rule, scale);
      }
      const callableScale = scale as (number) => any;
      return callableScale(Number(row[rule.property]));

    } else if (rule.function === 'linear') {
      // Linear function.
      if (!scale) {
        const range = <any[]>rule.range.map((v) => prop.parse(v));
        scale = d3Scale.scaleLinear<number, any>()
          .domain(rule.domain.map(Number))
          .range(range);
        this.scaleCache.set(rule, scale);
      }
      const callableScale = scale as (number) => any;
      return callableScale(Number(row[rule.property]));

    }
    throw new Error('Unknown style rule function: ' + rule.function);
  }