Project

General

Profile

1
import {ChartHelper, IndicatorPath} from "./entities/stakeholder";
2

    
3
export class IndicatorUtils {
4

    
5
  chartTypes: Map<string, string> = new Map([
6
    ['pie', 'pie_chart'],
7
    ['table', 'table_chart'],
8
    ['line', 'show_chart'],
9
    ['column', 'bar_chart'],
10
    ['bar', 'bar_chart'],
11
    ['image', 'perm_media']
12
  ]);
13

    
14
  isPublicIcon: Map<boolean, string> = new Map([
15
    [true, 'public'],
16
    [false, 'lock']
17
  ]);
18

    
19
  isActiveIcon: string = 'brightness_1';
20

    
21
  ignoredParameters = ['funder_name'];
22

    
23
  public getFullUrl(indicatorPath: IndicatorPath): string {
24
    let replacedUrl = indicatorPath.chartObject;
25
    if (indicatorPath.parameters) {
26
      Object.entries(indicatorPath.parameters).forEach((key, value) => {
27
        replacedUrl = replacedUrl.replace(ChartHelper.prefix + key + ChartHelper.suffix, value.toString());
28
      });
29
    }
30
    return indicatorPath.url + encodeURIComponent(replacedUrl);
31
  }
32

    
33
  generateIndicatorByChartUrl(source: string, url: string, type: string = null): IndicatorPath {
34
    let indicatorPath = new IndicatorPath("", source, "", "", []);
35
    if (source === 'stats-tool') {
36
      indicatorPath.url = url.split("json=")[0] + "json=";
37
      indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length - 1];
38
      indicatorPath.chartObject = decodeURIComponent(url.split("json=")[1]);
39
      let chart = JSON.parse(indicatorPath.chartObject);
40
      indicatorPath.type = this.extractType(chart, indicatorPath);
41
      this.extractTitle(chart, indicatorPath);
42
      this.extractXTitle(chart, indicatorPath);
43
      this.extractYTitle(chart, indicatorPath);
44
      this.extractFunder(chart, indicatorPath);
45
      this.extractStartYear(chart, indicatorPath);
46
      this.extractEndYear(chart, indicatorPath);
47
      indicatorPath.chartObject = JSON.stringify(chart);
48
    } else if (source === 'old') {
49
      indicatorPath.url = url.split("data=")[0] + "data=";
50
      indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1]);
51
    } else {
52
      indicatorPath.url = url;
53
      indicatorPath.type = type;
54
    }
55
    return indicatorPath;
56
  }
57

    
58
  private extractType(obj, indicatorPath: IndicatorPath): string {
59
    let defaultTypes = ["column", "bar", "pie"];
60
    let type = obj["chartDescription"]["queries"][0]["type"];
61
    if (defaultTypes.indexOf(type) == -1) {
62
      type = defaultTypes [0];
63
    } else {
64
      obj["chartDescription"]["queries"][0]["type"] = ChartHelper.prefix + "type" + ChartHelper.suffix;
65
      indicatorPath.parameters.set("type", type);
66
    }
67
    return type;
68
  }
69

    
70
  private extractFunder(obj, indicatorPath: IndicatorPath) {
71
    let funderName;
72
    for (let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]) {
73
      if (filter["groupFilters"][0]["field"].indexOf(".funder") != -1) {
74
        funderName = filter["groupFilters"][0]["values"][0];
75
        filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "funder_name" + ChartHelper.suffix;
76
        indicatorPath.parameters.set("funder_name", funderName);
77
      }
78
    }
79
  }
80

    
81
  private extractStartYear(obj, indicatorPath: IndicatorPath) {
82
    let start_year;
83
    for (let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]) {
84
      for (let gfilter of filter["groupFilters"]) {
85
        if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf(">") != -1) {
86
          start_year = gfilter["values"][0];
87
          gfilter["values"][0] = ChartHelper.prefix + "start_year" + ChartHelper.suffix;
88
          indicatorPath.parameters.set("start_year", start_year);
89
        }
90
      }
91
    }
92
  }
93

    
94
  private extractEndYear(obj, indicatorPath: IndicatorPath) {
95
    let end_year;
96
    for (let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]) {
97
      for (let gfilter of filter["groupFilters"]) {
98
        if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf("<") != -1) {
99
          end_year = gfilter["values"][0];
100
          gfilter["values"][0] = ChartHelper.prefix + "end_year" + ChartHelper.suffix;
101
          indicatorPath.parameters.set("end_year", end_year);
102
        }
103
      }
104
    }
105
  }
106

    
107
  private extractTitle(obj, indicatorPath: IndicatorPath) {
108
    let title = obj["chartDescription"]["title"]["text"];
109
    if (obj["chartDescription"]["title"]) {
110
      obj["chartDescription"]["title"]["text"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
111
      indicatorPath.parameters.set("title", title);
112

    
113
    }
114
  }
115

    
116
  private extractXTitle(obj, indicatorPath: IndicatorPath) {
117
    let title = obj["chartDescription"]["xAxis"]["title"]["text"];
118
    if (obj["chartDescription"]["xAxis"]["title"]) {
119
      obj["chartDescription"]["xAxis"]["title"]["text"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
120
      indicatorPath.parameters.set("xAxisTitle", title);
121
    }
122
  }
123

    
124
  private extractYTitle(obj, indicatorPath: IndicatorPath) {
125
    let title = obj["chartDescription"]["yAxis"]["title"]["text"];
126
    if (obj["chartDescription"]["yAxis"]["title"]) {
127
      obj["chartDescription"]["yAxis"]["title"]["text"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
128
      indicatorPath.parameters.set("yAxisTitle", title);
129
    }
130
  }
131

    
132
}
(1-1/2)