From c6c95f7091ecd407d51a7d28a8b8957752f869c7 Mon Sep 17 00:00:00 2001 From: CloudNiner Date: Mon, 20 Nov 2017 15:47:17 -0500 Subject: [PATCH 1/3] Create and use HISTORIC_RANGE_DEFAULT in extra params components Value of HISTORIC_RANGE_DEFAULT matches default historic_range param value of the Climate API --- src/lib/modules/api/index.ts | 2 +- src/lib/modules/api/models/historic-range.model.ts | 2 ++ .../charts/components/extra-params/historic.component.ts | 9 ++------- .../extra-params/percentile-historic.component.ts | 7 ++----- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/lib/modules/api/index.ts b/src/lib/modules/api/index.ts index 7801b2a..919188e 100644 --- a/src/lib/modules/api/index.ts +++ b/src/lib/modules/api/index.ts @@ -9,7 +9,7 @@ export { ClimateModel } from './models/climate-model.model'; export { DataPoint } from './models/data-point.model'; export { Dataset } from './models/dataset.model'; export { HistoricIndicatorQueryParams } from './models/historic-indicator-query-params.model'; -export { HistoricRange } from './models/historic-range.model'; +export { HistoricRange, HISTORIC_RANGE_DEFAULT } from './models/historic-range.model'; export { IndicatorParameter } from './models/indicator-parameter.model'; export { IndicatorQueryParams } from './models/indicator-query-params.model'; export { IndicatorRequestOpts } from './models/indicator-request-opts.model'; diff --git a/src/lib/modules/api/models/historic-range.model.ts b/src/lib/modules/api/models/historic-range.model.ts index 47ea621..510a69d 100644 --- a/src/lib/modules/api/models/historic-range.model.ts +++ b/src/lib/modules/api/models/historic-range.model.ts @@ -1,3 +1,5 @@ +export const HISTORIC_RANGE_DEFAULT = '1971'; + export interface HistoricRange { start_year: string; end_year: string; diff --git a/src/lib/modules/charts/components/extra-params/historic.component.ts b/src/lib/modules/charts/components/extra-params/historic.component.ts index ccd777a..55df88f 100644 --- a/src/lib/modules/charts/components/extra-params/historic.component.ts +++ b/src/lib/modules/charts/components/extra-params/historic.component.ts @@ -1,7 +1,7 @@ import { AfterViewInit, Component, EventEmitter, Input, Output, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; -import { HistoricRange } from '../../../api/models/historic-range.model'; +import { HistoricRange, HISTORIC_RANGE_DEFAULT } from '../../../api/models/historic-range.model'; import { HistoricIndicatorQueryParams } from '../../../api/models/historic-indicator-query-params.model'; import { HistoricRangeService } from '../../../api/services/historic-range.service'; import { Indicator } from '../../../api/models/indicator.model'; @@ -23,9 +23,6 @@ export class HistoricComponent implements AfterViewInit, OnInit { historicForm: FormGroup; public historicRangeOptions: string[] = []; - // default form values - private defaultHistoric = null; - constructor(private formBuilder: FormBuilder, private historicRangeService: HistoricRangeService) {} @@ -45,7 +42,7 @@ export class HistoricComponent implements AfterViewInit, OnInit { createForm() { this.historicForm = this.formBuilder.group({ - historicCtl: [this.extraParams.historic_range || this.defaultHistoric], + historicCtl: [this.extraParams.historic_range || HISTORIC_RANGE_DEFAULT], }); this.historicForm.valueChanges.debounceTime(700).subscribe(form => { @@ -58,8 +55,6 @@ export class HistoricComponent implements AfterViewInit, OnInit { getHistoricRanges() { this.historicRangeService.list().subscribe(data => { this.historicRangeOptions = data.map(h => h.start_year); - // add empty option, as this is not a required parameter - this.historicRangeOptions.unshift(''); }); } } diff --git a/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts b/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts index 8d59b45..c4c70ba 100644 --- a/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts +++ b/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts @@ -1,7 +1,7 @@ import { AfterViewInit, Component, EventEmitter, Input, Output, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; -import { HistoricRange } from '../../../api/models/historic-range.model'; +import { HistoricRange, HISTORIC_RANGE_DEFAULT } from '../../../api/models/historic-range.model'; import { PercentileHistoricIndicatorQueryParams } from '../../../api/models/percentile-historic-indicator-query-params.model'; import { HistoricRangeService } from '../../../api/services/historic-range.service'; import { Indicator } from '../../../api/models/indicator.model'; @@ -24,7 +24,6 @@ export class PercentileHistoricComponent implements AfterViewInit, OnInit { public historicRangeOptions: string[] = []; // default form values - private defaultHistoric = null; private defaultPercentile = 50; constructor(private formBuilder: FormBuilder, @@ -47,7 +46,7 @@ export class PercentileHistoricComponent implements AfterViewInit, OnInit { createForm() { this.percentileHistoricForm = this.formBuilder.group({ - historicCtl: [this.extraParams.historic_range || this.defaultHistoric], + historicCtl: [this.extraParams.historic_range || HISTORIC_RANGE_DEFAULT], percentileCtl: [this.extraParams.percentile || this.defaultPercentile, Validators.required] }); @@ -66,8 +65,6 @@ export class PercentileHistoricComponent implements AfterViewInit, OnInit { getHistoricRanges() { this.historicRangeService.list().subscribe(data => { this.historicRangeOptions = data.map(h => h.start_year); - // add empty option, as this is not a required parameter - this.historicRangeOptions.unshift(''); }); } } From 0968e481aa12f5a18b011e99f6432681854654b0 Mon Sep 17 00:00:00 2001 From: CloudNiner Date: Wed, 22 Nov 2017 09:43:42 -0500 Subject: [PATCH 2/3] Dynamically choose latest default historic range --- src/lib/modules/api/index.ts | 2 +- .../modules/api/models/historic-range.model.ts | 1 - .../components/extra-params/historic.component.ts | 12 ++++++++---- .../extra-params/percentile-historic.component.ts | 15 +++++++++++---- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/lib/modules/api/index.ts b/src/lib/modules/api/index.ts index 919188e..7801b2a 100644 --- a/src/lib/modules/api/index.ts +++ b/src/lib/modules/api/index.ts @@ -9,7 +9,7 @@ export { ClimateModel } from './models/climate-model.model'; export { DataPoint } from './models/data-point.model'; export { Dataset } from './models/dataset.model'; export { HistoricIndicatorQueryParams } from './models/historic-indicator-query-params.model'; -export { HistoricRange, HISTORIC_RANGE_DEFAULT } from './models/historic-range.model'; +export { HistoricRange } from './models/historic-range.model'; export { IndicatorParameter } from './models/indicator-parameter.model'; export { IndicatorQueryParams } from './models/indicator-query-params.model'; export { IndicatorRequestOpts } from './models/indicator-request-opts.model'; diff --git a/src/lib/modules/api/models/historic-range.model.ts b/src/lib/modules/api/models/historic-range.model.ts index 510a69d..f085fd6 100644 --- a/src/lib/modules/api/models/historic-range.model.ts +++ b/src/lib/modules/api/models/historic-range.model.ts @@ -1,4 +1,3 @@ -export const HISTORIC_RANGE_DEFAULT = '1971'; export interface HistoricRange { start_year: string; diff --git a/src/lib/modules/charts/components/extra-params/historic.component.ts b/src/lib/modules/charts/components/extra-params/historic.component.ts index 55df88f..b11a10a 100644 --- a/src/lib/modules/charts/components/extra-params/historic.component.ts +++ b/src/lib/modules/charts/components/extra-params/historic.component.ts @@ -1,7 +1,7 @@ import { AfterViewInit, Component, EventEmitter, Input, Output, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; -import { HistoricRange, HISTORIC_RANGE_DEFAULT } from '../../../api/models/historic-range.model'; +import { HistoricRange } from '../../../api/models/historic-range.model'; import { HistoricIndicatorQueryParams } from '../../../api/models/historic-indicator-query-params.model'; import { HistoricRangeService } from '../../../api/services/historic-range.service'; import { Indicator } from '../../../api/models/indicator.model'; @@ -21,7 +21,7 @@ export class HistoricComponent implements AfterViewInit, OnInit { @Output() historicParamSelected = new EventEmitter(); historicForm: FormGroup; - public historicRangeOptions: string[] = []; + public historicRangeOptions: number[] = []; constructor(private formBuilder: FormBuilder, private historicRangeService: HistoricRangeService) {} @@ -42,7 +42,7 @@ export class HistoricComponent implements AfterViewInit, OnInit { createForm() { this.historicForm = this.formBuilder.group({ - historicCtl: [this.extraParams.historic_range || HISTORIC_RANGE_DEFAULT], + historicCtl: [this.extraParams.historic_range], }); this.historicForm.valueChanges.debounceTime(700).subscribe(form => { @@ -54,7 +54,11 @@ export class HistoricComponent implements AfterViewInit, OnInit { getHistoricRanges() { this.historicRangeService.list().subscribe(data => { - this.historicRangeOptions = data.map(h => h.start_year); + this.historicRangeOptions = data.map(h => parseInt(h.start_year, 10)); + if (!this.extraParams.historic_range) { + const latestHistoricRange = Math.max(...this.historicRangeOptions); + this.historicForm.setValue({historicCtl: latestHistoricRange}); + } }); } } diff --git a/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts b/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts index c4c70ba..8dd3166 100644 --- a/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts +++ b/src/lib/modules/charts/components/extra-params/percentile-historic.component.ts @@ -1,7 +1,7 @@ import { AfterViewInit, Component, EventEmitter, Input, Output, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; -import { HistoricRange, HISTORIC_RANGE_DEFAULT } from '../../../api/models/historic-range.model'; +import { HistoricRange } from '../../../api/models/historic-range.model'; import { PercentileHistoricIndicatorQueryParams } from '../../../api/models/percentile-historic-indicator-query-params.model'; import { HistoricRangeService } from '../../../api/services/historic-range.service'; import { Indicator } from '../../../api/models/indicator.model'; @@ -21,7 +21,7 @@ export class PercentileHistoricComponent implements AfterViewInit, OnInit { @Output() percentileHistoricParamSelected = new EventEmitter(); percentileHistoricForm: FormGroup; - public historicRangeOptions: string[] = []; + public historicRangeOptions: number[] = []; // default form values private defaultPercentile = 50; @@ -46,7 +46,7 @@ export class PercentileHistoricComponent implements AfterViewInit, OnInit { createForm() { this.percentileHistoricForm = this.formBuilder.group({ - historicCtl: [this.extraParams.historic_range || HISTORIC_RANGE_DEFAULT], + historicCtl: [this.extraParams.historic_range], percentileCtl: [this.extraParams.percentile || this.defaultPercentile, Validators.required] }); @@ -64,7 +64,14 @@ export class PercentileHistoricComponent implements AfterViewInit, OnInit { getHistoricRanges() { this.historicRangeService.list().subscribe(data => { - this.historicRangeOptions = data.map(h => h.start_year); + this.historicRangeOptions = data.map(h => parseInt(h.start_year, 10)); + if (!this.extraParams.historic_range) { + const latestHistoricRange = Math.max(...this.historicRangeOptions); + this.percentileHistoricForm.setValue({ + historicCtl: latestHistoricRange, + percentileCtl: this.percentileHistoricForm.controls.percentileCtl.value + }); + } }); } } From b1e728245f27ec15f8d55ed0672af92a098ce678 Mon Sep 17 00:00:00 2001 From: CloudNiner Date: Wed, 22 Nov 2017 11:23:44 -0500 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a060d08..87c9cec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +- PercentileHistoricComponent and HistoricComponent now automatically choose a sensible dropdown + default for the historic baseline parameter ## [0.2.1] - 2017-11-07 ### Added