|
|
@@ -2,6 +2,7 @@ import {Component, OnInit} from '@angular/core';
|
|
|
import {interpolate, quantize, select} from 'd3';
|
|
|
|
|
|
import {SdmDihService} from '../../sdm-dih.service';
|
|
|
+import {YearGraphService} from '../year-graph.service';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'year-graph',
|
|
|
@@ -9,16 +10,16 @@ import {SdmDihService} from '../../sdm-dih.service';
|
|
|
styleUrls: ['./year-graph.component.scss'],
|
|
|
})
|
|
|
export class YearGraphComponent implements OnInit {
|
|
|
- selectedYear = '';
|
|
|
-
|
|
|
- constructor(public sdmDihService: SdmDihService) {}
|
|
|
+ constructor(
|
|
|
+ public sdmDihService: SdmDihService,
|
|
|
+ public yearGraphService: YearGraphService
|
|
|
+ ) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
this.sdmDihService.dataLoads.subscribe((loaded) => {
|
|
|
if (!loaded) {
|
|
|
return;
|
|
|
}
|
|
|
- this.selectedYear = this.sdmDihService.firstYear;
|
|
|
this.redrawGraphs();
|
|
|
this.drawLegend();
|
|
|
});
|
|
|
@@ -29,7 +30,7 @@ export class YearGraphComponent implements OnInit {
|
|
|
let i = 1;
|
|
|
for (const year of this.sdmDihService.years) {
|
|
|
setTimeout(() => {
|
|
|
- this.selectedYear = year;
|
|
|
+ this.yearGraphService.selectedYear = year;
|
|
|
this.redrawGraphs();
|
|
|
}, MILLISECONDS_TO_ANIMATE * i);
|
|
|
i++;
|
|
|
@@ -37,9 +38,9 @@ export class YearGraphComponent implements OnInit {
|
|
|
}
|
|
|
|
|
|
drawGraph(region: string) {
|
|
|
- const year = this.selectedYear.toString().includes('.')
|
|
|
- ? this.selectedYear
|
|
|
- : this.selectedYear + '.0';
|
|
|
+ const year = this.yearGraphService.selectedYear.toString().includes('.')
|
|
|
+ ? this.yearGraphService.selectedYear
|
|
|
+ : this.yearGraphService.selectedYear + '.0';
|
|
|
const regionData = this.sdmDihService.sdmData[year].find(
|
|
|
(row) => row['MODEL'] === region
|
|
|
);
|
|
|
@@ -109,13 +110,6 @@ export class YearGraphComponent implements OnInit {
|
|
|
.text((d) => `${(Number.parseFloat(d['aggregated']) * 100).toFixed(2)} %`)
|
|
|
.style('text-anchor', 'middle');
|
|
|
svgContent
|
|
|
- .append('text')
|
|
|
- .attr('x', 0)
|
|
|
- .attr('y', 60)
|
|
|
- .attr('dy', '.35em')
|
|
|
- .text((d) => `${(Number.parseFloat(d['aggregated']) * 100).toFixed(2)} %`)
|
|
|
- .style('text-anchor', 'middle');
|
|
|
- svgContent
|
|
|
.append('use')
|
|
|
.attr('xlink:href', '#arrow')
|
|
|
.attr('x', -30)
|
|
|
@@ -123,7 +117,8 @@ export class YearGraphComponent implements OnInit {
|
|
|
.attr(
|
|
|
'transform',
|
|
|
// rotation is defined clockwise, hence -45 deg will turn the arrow up
|
|
|
- (d) => `rotate(${-45 * this.getRegionProgress(d)}, 0, 0)`
|
|
|
+ (d) =>
|
|
|
+ `rotate(${-45 * this.yearGraphService.getRegionProgress(d)}, 0, 0)`
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -160,23 +155,29 @@ export class YearGraphComponent implements OnInit {
|
|
|
|
|
|
nextYear() {
|
|
|
const selectedYearIndex = this.sdmDihService.years.findIndex(
|
|
|
- (val) => val == this.selectedYear
|
|
|
+ (val) => val == this.yearGraphService.selectedYear
|
|
|
);
|
|
|
- this.selectedYear =
|
|
|
- this.sdmDihService.years[selectedYearIndex + 1] ?? this.selectedYear;
|
|
|
+ this.yearGraphService.selectedYear =
|
|
|
+ this.sdmDihService.years[selectedYearIndex + 1] ??
|
|
|
+ this.yearGraphService.selectedYear;
|
|
|
this.redrawGraphs();
|
|
|
}
|
|
|
|
|
|
prevYear() {
|
|
|
const selectedYearIndex = this.sdmDihService.years.findIndex(
|
|
|
- (val) => val == this.selectedYear
|
|
|
+ (val) => val == this.yearGraphService.selectedYear
|
|
|
);
|
|
|
- this.selectedYear =
|
|
|
- this.sdmDihService.years[selectedYearIndex - 1] ?? this.selectedYear;
|
|
|
+ this.yearGraphService.selectedYear =
|
|
|
+ this.sdmDihService.years[selectedYearIndex - 1] ??
|
|
|
+ this.yearGraphService.selectedYear;
|
|
|
this.redrawGraphs();
|
|
|
}
|
|
|
|
|
|
redrawGraphs() {
|
|
|
+ // Data not loaded yet
|
|
|
+ if (!this.sdmDihService.sdmData) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
const width = 200;
|
|
|
const height = 200;
|
|
|
select('#year-graph-place').selectAll('svg')?.remove();
|
|
|
@@ -184,33 +185,4 @@ export class YearGraphComponent implements OnInit {
|
|
|
this.drawGraph(region);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- getRegionProgress(regionData): -1 | 0 | 1 {
|
|
|
- //TODO: parametrize the PARAM_TO_COMPARE
|
|
|
- //TODO: use i-2, i-1 comparison based on 2 previous steps
|
|
|
- const PARAM_TO_COMPARE = 'aggregated';
|
|
|
- const region = regionData['MODEL'];
|
|
|
- const year = regionData['TIME_STEP'];
|
|
|
- const thisYearValue = +Number.parseFloat(
|
|
|
- regionData[PARAM_TO_COMPARE]
|
|
|
- ).toFixed(3);
|
|
|
- let pastYear = Number.parseFloat(year) - 0.25 + '';
|
|
|
- pastYear = pastYear.includes('.') ? pastYear : pastYear + '.0';
|
|
|
- const pastYearData = this.sdmDihService.sdmData[pastYear];
|
|
|
- if (!pastYearData) {
|
|
|
- // We are in the first time step, so there is nothing to compare
|
|
|
- return 0;
|
|
|
- }
|
|
|
- let pastYearValue = pastYearData.find(
|
|
|
- (regionPastYear) => regionPastYear['MODEL'] === region
|
|
|
- )?.[PARAM_TO_COMPARE];
|
|
|
- pastYearValue = +Number.parseFloat(pastYearValue).toFixed(3);
|
|
|
- if (pastYearValue < thisYearValue) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- if (pastYearValue > thisYearValue) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
}
|