import {ChartHelper, Indicator, IndicatorPath, Stakeholder, SubCategory, Topic} from "./entities/stakeholder";
import {Validators} from "@angular/forms";

export interface Option {
  icon?: string,
  iconClass?: string,
  value: any,
  label: string
}

export class StakeholderUtils {
  types: Option[] = [
    {value: 'funder', label: 'Funder'}
  ];

  isPublic: Option[] = [
    {icon: 'public', value: true, label: 'Public'},
    {icon: 'lock', value: false, label: 'Private'},
  ];

  isActive: Option[] = [
    {icon: 'brightness_1', iconClass: '', value: true, label: 'Active'},
    {icon: 'brightness_1', value: false, label: 'Inactive'},
  ];

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

  isActiveIcon: string = 'brightness_1';

  public createFunderFromDefaultProfile(funder: Stakeholder, defaultTopics: Topic[]): Stakeholder {
    funder.topics = defaultTopics;
    for (let topic of funder.topics) {
      for (let category of topic.categories) {
        let subTokeep: SubCategory[] = [];
        for (let subCategory of category.subCategories) {
          let chartsTokeep: Indicator[] = [];
          if (subCategory.recommendedFor.length == 0 || subCategory.recommendedFor.indexOf(funder.index_id) != -1) {
            subTokeep.push(subCategory);
          }
          for (let indicator of subCategory.charts) {
            if (indicator.recommendedFor.length == 0 || indicator.recommendedFor.indexOf(funder.index_id) != -1) {
              chartsTokeep.push(indicator);
            }
            for (let indicatorPath of indicator.indicatorPaths) {
              if (indicatorPath.parameters) {
                Object.keys(indicatorPath.parameters).forEach(key => {
                  if (indicatorPath.parameters[key].indexOf("_funder_name_") != -1) {
                    indicatorPath.parameters[key] = indicatorPath.parameters[key].replace("_funder_name_", funder.index_name);
                  } else if (indicatorPath.parameters[key].indexOf("_fsn_") != -1) {
                    indicatorPath.parameters[key] = indicatorPath.parameters[key].toString().replace("_fsn_", funder.index_shortName.toLowerCase());
                  }
                });
              }
            }

          }
          subCategory.charts = chartsTokeep;
          for (let indicator of subCategory.numbers) {
            for (let indicatorPath of indicator.indicatorPaths) {
              indicatorPath.url = indicatorPath.url.replace("_funder_id_", funder.index_id);
              // if(indicatorPath.parameters) {
              //   indicatorPath.parameters.forEach((value: string, key: string) => {
              //     if (value.indexOf("_funder_name_")!=-1) {
              //       indicatorPath.parameters.set(key,value.toString().replace("_funder_name_", funder.index_name));
              //     }else if (value.indexOf("_fsn_")!=-1) {
              //       indicatorPath.parameters.set(key,value.toString().replace("_fsn_", funder.index_shortName.toLowerCase()));
              //     }
              //   });
              // }
            }
          }

        }
        category.subCategories = subTokeep;
      }
    }
    console.log(funder);
    return funder;
  }
}

export class IndicatorUtils {

  chartTypes: Option[] = [
    {value: 'pie', label: 'Pie'},
    {value: 'table', label: 'Table'},
    {value: 'line', label: 'Line'},
    {value: 'column', label: 'Column'},
    {value: 'bar', label: 'Bar'},
    {value: 'other', label: 'Other'}
  ];

  chartSizes: Option[] = [
    {value: 'small', label: 'Small'},
    {value: 'medium', label: 'Medium'},
    {value: 'large', label: 'Large'}
  ];

  isPublic: Option[] = [
    {icon: 'public', value: true, label: 'Public'},
    {icon: 'lock', value: false, label: 'Private'},
  ];

  isActive: Option[] = [
    {icon: 'brightness_1', iconClass: '', value: true, label: 'Active'},
    {icon: 'brightness_1', value: false, label: 'Inactive'},
  ];

  chartTypesIcons: Map<string, string> = new Map([
    ['pie', 'pie_chart'],
    ['table', 'table_chart'],
    ['line', 'show_chart'],
    ['column', 'bar_chart'],
    ['bar', 'notes'],
    ['other', 'perm_media']
  ]);

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

  isActiveIcon: string = 'brightness_1';

  ignoredParameters = ['funder_name'];

  parametersValidators: Map<string, any> = new Map<string, any>([
    ['start_year', [Validators.required, Validators.pattern('^\\d+$')]],
    ['end_year', [Validators.required, Validators.pattern('^\\d+$')]]
  ]);

  public getFullUrl(indicatorPath: IndicatorPath, fundingL0: string = null, startDate: string = null, endDate: string = null): string {

    let replacedUrl = indicatorPath.chartObject;
    if (indicatorPath.parameters) {
      Object.keys(indicatorPath.parameters).forEach(key => {
        let replacedValue = indicatorPath.parameters[key];
        if (startDate && key == "start_year" && indicatorPath.filters["start_year"]) {
          replacedValue = (replacedValue < startDate) ? startDate : replacedValue;
        }
        if (endDate && key == "end_year" && indicatorPath.filters["end_year"]) {
          replacedValue = (replacedValue > endDate) ? endDate : replacedValue;
        }
        replacedUrl = replacedUrl.replace(ChartHelper.prefix + key + ChartHelper.suffix, replacedValue);
      });
    }
    if (indicatorPath.chartObject) {
      if (fundingL0 && indicatorPath.filters["fundingL0"]) {
        let newJsonObject = JSON.parse(replacedUrl);
        for (let queries of newJsonObject["chartDescription"]["queries"]) {
          if (!queries["query"]["filters"] || queries["query"]["filters"].length == 0) {
            queries["query"]["filters"] = [];
          }
          //TODO check how it works if the query already has a filter
          queries["query"]["filters"].push(JSON.parse(indicatorPath.filters["fundingL0"].replace(ChartHelper.prefix + "fundingL0" + ChartHelper.suffix, fundingL0)));
        }
        replacedUrl = JSON.stringify(newJsonObject);
      }
    }
    return indicatorPath.url + encodeURIComponent(replacedUrl);
  }

  generateIndicatorByForm(form: any, indicatorPaths: IndicatorPath[]): Indicator {
    let indicator: Indicator = new Indicator(form.name, form.description, 'chart',
      form.width, form.isActive, form.isPublic, indicatorPaths);
    indicator._id = form.id;
    form.indicatorPaths.forEach((indicatorPath, index) => {
      indicatorPath.parameters.forEach(parameter => {
        indicator.indicatorPaths[index].parameters[parameter.key] = parameter.value;
        if (parameter.key === 'type') {
          indicator.indicatorPaths[index].type = parameter.value;
        }
      });
    });
    return indicator;
  }

  generateIndicatorByChartUrl(source: string, url: string, type: string = null): IndicatorPath {
    let indicatorPath = new IndicatorPath("", source, "", "", []);
    if (source === 'stats-tool') {
      indicatorPath.url = url.split("json=")[0] + "json=";
      indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length - 1];
      indicatorPath.chartObject = decodeURIComponent(url.split("json=")[1]);
      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);
      this.addResultFilters(chart, indicatorPath);
    } else if (source === 'old') {
      indicatorPath.url = url.split("data=")[0].split("/stats/")[1] + "data=";
      indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1].split("&")[0]);
      indicatorPath.type = type;
      let chart = JSON.parse(indicatorPath.chartObject);
      this.extractOldToolTitle(chart, indicatorPath);
      this.extractOldToolXTitle(chart, indicatorPath);
      this.extractOldToolYTitle(chart, indicatorPath);
      indicatorPath.chartObject = JSON.stringify(chart);
    } else {
      indicatorPath.url = url;
      indicatorPath.type = type;
    }
    return indicatorPath;
  }

  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['type'] = type;
    }
    return type;
  }

  private extractFunder(obj, indicatorPath: IndicatorPath) {
    let funderName;
    for (let query of obj["chartDescription"]["queries"]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["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["funder_name"] = funderName;
        }
      }
    }
  }

  private extractStartYear(obj, indicatorPath: IndicatorPath) {
    let start_year;
    for (let query of obj["chartDescription"]["queries"]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["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["start_year"] = start_year;
          }
        }
      }
    }
  }

  private extractEndYear(obj, indicatorPath: IndicatorPath) {
    let end_year;
    for (let query of obj["chartDescription"]["queries"]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["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["end_year"] = end_year;
          }
        }
      }
    }
  }

  private extractTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["chartDescription"]["title"]) {
      title = obj["chartDescription"]["title"]["text"];
      obj["chartDescription"]["title"]["text"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
      indicatorPath.parameters["title"] = title;

    }
  }

  private extractXTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["chartDescription"]["xAxis"]["title"]) {
      title = obj["chartDescription"]["xAxis"]["title"]["text"];
      obj["chartDescription"]["xAxis"]["title"]["text"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
      indicatorPath.parameters["xAxisTitle"] = title;
    }
  }

  private extractYTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["chartDescription"]["yAxis"]["title"]) {
      title = obj["chartDescription"]["yAxis"]["title"]["text"];
      obj["chartDescription"]["yAxis"]["title"]["text"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
      indicatorPath.parameters["yAxisTitle"] = title;
    }
  }

  private addResultFilters(obj, indicatorPath: IndicatorPath) {
    let resultTypes = ["publication", "software", "dataset", "other"];
    let index = -1;
    for (let query of obj["chartDescription"]["queries"]) {
      if (!query["query"]["select"]) {
        return;
      }
      for (let select of query["query"]["select"]) {
        for (var i = 0; i < resultTypes.length; i++) {
          if (select.field.startsWith(resultTypes[i])) {
            index = i;
          }
        }
      }

    }
    if (index != -1) {
      indicatorPath.filters = IndicatorPath.createResultFilters(resultTypes[index]);
    }
  }


  private extractOldToolTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["title"]) {
      title = obj["title"];
      obj["title"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
      indicatorPath.parameters["title"] = title;

    }
  }

  private extractOldToolXTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["xaxistitle"]) {
      title = obj["xaxistitle"];
      obj["xaxistitle"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
      indicatorPath.parameters["xAxisTitle"] = title;
    }
  }

  private extractOldToolYTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["fieldsheaders"]) {
      title = Array.isArray(obj["fieldsheaders"])?obj["fieldsheaders"][0]:obj["fieldsheaders"];
      if(Array.isArray(obj["fieldsheaders"])){
        obj["fieldsheaders"][0] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
      }else {
        obj["fieldsheaders"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
      }
      indicatorPath.parameters["yAxisTitle"] = title;
    }
  }

}

/*
custom query
http://88.197.53.71:8080/stats-api/chart?json=%7B%22library%22%3A%22HighCharts%22%2C%22chartDescription%22%3A%7B%22queries%22%3A%5B%7B%22name%22%3A%22Publications%22%2C%22type%22%3A%22bar%22%2C%22query%22%3A%7B%22name%22%3A%22monitor.ec.publications.datasources%22%7D%7D%5D%2C%22chart%22%3A%7B%22backgroundColor%22%3A%22%23FFFFFFFF%22%2C%22borderColor%22%3A%22%23335cadff%22%2C%22borderRadius%22%3A0%2C%22borderWidth%22%3A0%2C%22plotBorderColor%22%3A%22%23ccccccff%22%2C%22plotBorderWidth%22%3A0%7D%2C%22title%22%3A%7B%22text%22%3A%22Publication%20content%20provider%22%7D%2C%22subtitle%22%3A%7B%7D%2C%22yAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Publications%22%7D%7D%2C%22xAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Content%20provider%22%7D%7D%2C%22lang%22%3A%7B%22noData%22%3A%22No%20Data%20available%20for%20the%20Query%22%7D%2C%22exporting%22%3A%7B%22enabled%22%3Atrue%7D%2C%22plotOptions%22%3A%7B%22series%22%3A%7B%22dataLabels%22%3A%7B%22enabled%22%3Afalse%7D%7D%7D%2C%22legend%22%3A%7B%22enabled%22%3Atrue%2C%22align%22%3A%22center%22%2C%22verticalAlign%22%3A%22bottom%22%2C%22layout%22%3A%22horizontal%22%7D%2C%22credits%22%3A%7B%22href%22%3Anull%2C%22enabled%22%3Atrue%2C%22text%22%3A%22Created%20by%20OpenAIRE%20via%20HighCharts%22%7D%7D%7D

query with year filters:
http://88.197.53.71:8080/stats-api/chart?json=%7B%22library%22%3A%22HighCharts%22%2C%22chartDescription%22%3A%7B%22queries%22%3A%5B%7B%22name%22%3A%22Publications%22%2C%22type%22%3A%22column%22%2C%22query%22%3A%7B%22select%22%3A%5B%7B%22field%22%3A%22publication%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22publication.year%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22publication.project.funder%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22European%20Commission%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%2C%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22publication.year%22%2C%22type%22%3A%22%3E%3D%22%2C%22values%22%3A%5B%222008%22%5D%7D%2C%7B%22field%22%3A%22publication.year%22%2C%22type%22%3A%22%3C%3D%22%2C%22values%22%3A%5B%222020%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%5D%2C%22entity%22%3A%22publication%22%2C%22profile%22%3A%22OpenAIRE%20All-inclusive%22%2C%22limit%22%3A%220%22%7D%7D%5D%2C%22chart%22%3A%7B%22backgroundColor%22%3A%22%23FFFFFFFF%22%2C%22borderColor%22%3A%22%23335cadff%22%2C%22borderRadius%22%3A0%2C%22borderWidth%22%3A0%2C%22plotBorderColor%22%3A%22%23ccccccff%22%2C%22plotBorderWidth%22%3A0%7D%2C%22title%22%3A%7B%22text%22%3A%22Publications%20timeline%22%7D%2C%22subtitle%22%3A%7B%7D%2C%22yAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Publications%22%7D%7D%2C%22xAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Year%22%7D%7D%2C%22lang%22%3A%7B%22noData%22%3A%22No%20Data%20available%20for%20the%20Query%22%7D%2C%22exporting%22%3A%7B%22enabled%22%3Atrue%7D%2C%22plotOptions%22%3A%7B%22series%22%3A%7B%22dataLabels%22%3A%7B%22enabled%22%3Afalse%7D%7D%7D%2C%22legend%22%3A%7B%22enabled%22%3Atrue%2C%22align%22%3A%22center%22%2C%22verticalAlign%22%3A%22bottom%22%2C%22layout%22%3A%22horizontal%22%7D%2C%22credits%22%3A%7B%22href%22%3Anull%2C%22enabled%22%3Atrue%2C%22text%22%3A%22Created%20by%20OpenAIRE%20via%20HighCharts%22%7D%7D%7D

double query
http://88.197.53.71:8080/stats-api/chart?json=%7B%22library%22%3A%22HighCharts%22%2C%22chartDescription%22%3A%7B%22colors%22%3A%5B%22%2342a5f5%22%2C%22%2326a69a%22%2C%22%2390ed7d%22%2C%22%23607d8b%22%2C%22%2300838f%22%2C%22%23689f38%22%2C%22%23e4d354%22%2C%22%232b908f%22%2C%22%23546e7a%22%2C%22%2301579%22%5D%2C%22queries%22%3A%5B%7B%22name%22%3A%22Gold%22%2C%22color%22%3A%22%23f8b500%22%2C%22type%22%3A%22column%22%2C%22query%22%3A%7B%22select%22%3A%5B%7B%22field%22%3A%22result%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22result.year%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.project.funder%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22European%20Commission%22%5D%7D%2C%7B%22field%22%3A%22result.project.funding%20level%200%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22H2020%22%5D%7D%2C%7B%22field%22%3A%22result.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22publication%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3E%3D%22%2C%22values%22%3A%5B%222014%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3C%3D%22%2C%22values%22%3A%5B%222019%22%5D%7D%2C%7B%22field%22%3A%22result.access%20mode%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Open%20Access%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%2C%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Journal%22%5D%7D%2C%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Journal%20Aggregator%2FPublisher%22%5D%7D%5D%2C%22op%22%3A%22OR%22%7D%5D%2C%22entity%22%3A%22result%22%2C%22profile%22%3A%22OpenAIRE%20original%22%2C%22limit%22%3A%220%22%7D%7D%2C%7B%22name%22%3A%22Green%22%2C%22color%22%3A%22%23239d60%22%2C%22type%22%3A%22column%22%2C%22query%22%3A%7B%22select%22%3A%5B%7B%22field%22%3A%22result%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22result.year%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.project.funder%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22European%20Commission%22%5D%7D%2C%7B%22field%22%3A%22result.project.funding%20level%200%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22H2020%22%5D%7D%2C%7B%22field%22%3A%22result.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22publication%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3E%3D%22%2C%22values%22%3A%5B%222014%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3C%3D%22%2C%22values%22%3A%5B%222019%22%5D%7D%2C%7B%22field%22%3A%22result.access%20mode%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Open%20Access%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%2C%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Institutional%20Repository%22%5D%7D%2C%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Thematic%20Repository%22%5D%7D%5D%2C%22op%22%3A%22OR%22%7D%5D%2C%22entity%22%3A%22result%22%2C%22profile%22%3A%22OpenAIRE%20original%22%2C%22limit%22%3A%220%22%7D%7D%5D%2C%22chart%22%3A%7B%22backgroundColor%22%3A%22%23FFFFFFFF%22%2C%22borderColor%22%3A%22%23335cadff%22%2C%22borderRadius%22%3A0%2C%22borderWidth%22%3A0%2C%22plotBorderColor%22%3A%22%23ccccccff%22%2C%22plotBorderWidth%22%3A0%7D%2C%22title%22%3A%7B%22text%22%3A%22H2020%20green%20and%20gold%20publications%22%7D%2C%22subtitle%22%3A%7B%22text%22%3A%22over%20time%22%7D%2C%22yAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Publications%22%7D%7D%2C%22xAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Year%22%7D%7D%2C%22lang%22%3A%7B%22noData%22%3A%22No%20Data%20available%20for%20the%20Query%22%7D%2C%22exporting%22%3A%7B%22enabled%22%3Atrue%7D%2C%22plotOptions%22%3A%7B%22series%22%3A%7B%22dataLabels%22%3A%7B%22enabled%22%3Afalse%7D%7D%7D%2C%22legend%22%3A%7B%22enabled%22%3Afalse%2C%22align%22%3A%22center%22%2C%22verticalAlign%22%3A%22bottom%22%2C%22layout%22%3A%22horizontal%22%7D%2C%22credits%22%3A%7B%22href%22%3Anull%2C%22enabled%22%3Atrue%2C%22text%22%3A%22Created%20by%20OpenAIRE%20via%20HighCharts%22%7D%7D%7D


//old tool
https://www.openaire.eu/stats/chart.php?com=query&data={%22table%22:%22result%22,%22fields%22:[{%22fld%22:%22number%22,%22agg%22:%22count%22,%22type%22:%22bar%22,%22yaxis%22:1,%22c%22:false}],%22xaxis%22:{%22name%22:%22result_datasources-datasource-name%22,%22agg%22:%22avg%22},%22group%22:%22%22,%22color%22:%22%22,%22type%22:%22chart%22,%22size%22:%2220%22,%22sort%22:%22count-number%22,%22yaxisheaders%22:[%22%22],%22fieldsheaders%22:[%22publications%22],%22in%22:[],%22filters%22:[{%22name%22:%22result_projects-project-funding_lvl0%22,%22values%22:[%22H2020%22],%22to%22:%22-1%22},{%22name%22:%22type%22,%22values%22:[%22publication%22],%22to%22:%22-1%22},{%22name%22:%22result_datasources-datasource-type%22,%22exvalues%22:[%22Publication%20Catalogue%22]}],%22having%22:[],%22xStyle%22:{%22r%22:%22-%22,%22s%22:%22-%22,%22l%22:%22-%22,%22ft%22:%22-%22,%22wt%22:%22-%22},%22title%22:%22H2020%20Publications%20by%20datasource%20%28top%2020%29%22,%22subtitle%22:%22%22,%22xaxistitle%22:%22datasource%22,%22order%22:%22d%22}&w=90%
 */
