monitor-dashboard/src/app/utils/indicator-utils.ts

117 lines
4.6 KiB
TypeScript
Raw Normal View History

import {ChartHelper, Indicator, IndicatorPath} from "./entities/stakeholder";
export class IndicatorUtils {
chartTypes: Map<string, string> = new Map([
['pie', 'pie_chart'],
['table', 'table_chart'],
['line', 'show_chart'],
['column', 'bar_chart'],
['bar', 'bar_chart'],
['image', 'perm_media']
]);
isPublicIcon: Map<boolean, string> = new Map([
[true, 'public'],
[false, 'lock']
]);
isActiveIcon: string = 'brightness_1';
generateIndicatorByChartUrl(url:string):Indicator{
let indicator = new Indicator("","","chart","small",true,true,[]);
let indicatorPath = new IndicatorPath("","","","",[]);
indicator.indicatorPaths.push(indicatorPath);
if(url.indexOf("json=")!=-1){
indicatorPath.url = url.split("json=")[0]+"json=";
indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length-1];
indicatorPath.chartObject = decodeURIComponent(url.split("json=")[1]);
indicatorPath.source = "stats-tool";
let chart = JSON.parse(indicatorPath.chartObject);
indicatorPath.type = this.extractType(chart, indicatorPath);
this.extractTitle(chart,indicatorPath);
this.extractXTitle(chart,indicatorPath);
this.extractYTitle(chart,indicatorPath);
this.extractFunder(chart,indicatorPath);
this.extractStartYear(chart,indicatorPath);
this.extractEndYear(chart,indicatorPath);
indicatorPath.chartObject = JSON.stringify(chart);
}else if(url.indexOf("data=")!=-1){
indicatorPath.url = url.split("data=")[0]+"data=";
indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1]);
indicatorPath.source = "old";
}
return indicator;
}
private extractType(obj,indicatorPath:IndicatorPath ):string{
let defaultTypes = ["column","bar","pie"];
let type= obj["chartDescription"]["queries"][0]["type"];
if(defaultTypes.indexOf(type)==-1){
type = defaultTypes [0];
}else{
obj["chartDescription"]["queries"][0]["type"] = ChartHelper.prefix+"type"+ChartHelper.suffix;
indicatorPath.parameters.set("type",type);
}
return type;
}
private extractFunder(obj,indicatorPath:IndicatorPath ){
let funderName;
for(let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]){
if(filter["groupFilters"][0]["field"].indexOf(".funder")!=-1){
funderName= filter["groupFilters"][0]["values"][0];
filter["groupFilters"][0]["values"][0] = ChartHelper.prefix+"funder_name"+ChartHelper.suffix;
indicatorPath.parameters.set("funder_name",funderName);
}
}
}
private extractStartYear(obj,indicatorPath:IndicatorPath ){
let start_year;
for(let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]){
for(let gfilter of filter["groupFilters"]) {
if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf(">") != -1) {
start_year = gfilter["values"][0];
gfilter["values"][0] = ChartHelper.prefix + "start_year" + ChartHelper.suffix;
indicatorPath.parameters.set("start_year", start_year);
}
}
}
}
private extractEndYear(obj,indicatorPath:IndicatorPath ){
let end_year;
for(let filter of obj["chartDescription"]["queries"][0]["query"]["filters"]){
for(let gfilter of filter["groupFilters"]) {
if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf("<") != -1) {
end_year = gfilter["values"][0];
gfilter["values"][0] = ChartHelper.prefix + "end_year" + ChartHelper.suffix;
indicatorPath.parameters.set("end_year", end_year);
}
}
}
}
private extractTitle(obj,indicatorPath:IndicatorPath ){
let title= obj["chartDescription"]["title"]["text"];
if(obj["chartDescription"]["title"]){
obj["chartDescription"]["title"]["text"] = ChartHelper.prefix+"title"+ChartHelper.suffix;
indicatorPath.parameters.set("title",title);
}
}
private extractXTitle(obj,indicatorPath:IndicatorPath ){
let title= obj["chartDescription"]["xAxis"]["title"]["text"];
if(obj["chartDescription"]["xAxis"]["title"]){
obj["chartDescription"]["xAxis"]["title"]["text"] = ChartHelper.prefix+"xAxisTitle"+ChartHelper.suffix;
indicatorPath.parameters.set("xAxisTitle",title);
}
}
private extractYTitle(obj,indicatorPath:IndicatorPath ){
let title= obj["chartDescription"]["yAxis"]["title"]["text"];
if(obj["chartDescription"]["yAxis"]["title"]){
obj["chartDescription"]["yAxis"]["title"]["text"] = ChartHelper.prefix+"yAxisTitle"+ChartHelper.suffix;
indicatorPath.parameters.set("yAxisTitle",title);
}
}
}