region-graph.component.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {Component, Input, OnChanges, SimpleChanges} from '@angular/core';
  2. import abbreviationMap from '../../../assets/data/abbreviation-map.json';
  3. import {SdmDihService} from '../../sdm-dih.service';
  4. @Component({
  5. selector: 'region-graph',
  6. templateUrl: './region-graph.component.html',
  7. styleUrls: ['./region-graph.component.scss'],
  8. })
  9. export class RegionGraphComponent implements OnChanges {
  10. @Input() region: string;
  11. dataLoaded = false;
  12. data = [];
  13. plotlyLayout = {
  14. height: 180,
  15. cliponaxis: false,
  16. constraintext: 'none',
  17. yaxis: {
  18. automargin: true,
  19. },
  20. xaxis: {
  21. automargin: true,
  22. },
  23. margin: {
  24. l: 5,
  25. r: 5,
  26. b: 10,
  27. t: 10,
  28. pad: 4,
  29. },
  30. //width: '100%',
  31. };
  32. plotlyConfig = {
  33. staticPlot: true,
  34. };
  35. constructor(public sdmDihService: SdmDihService) {
  36. this.sdmDihService.dataLoads.subscribe((loaded) => {
  37. if (!loaded) {
  38. return;
  39. }
  40. this.processData();
  41. this.dataLoaded = true;
  42. });
  43. }
  44. ngOnChanges(changes: SimpleChanges): void {
  45. this.processData();
  46. this.dataLoaded = true;
  47. }
  48. processData() {
  49. const PARAM_TO_COMPARE = 'aggregated';
  50. const years = [];
  51. const factors = [];
  52. const colors = [];
  53. for (const yearData of Object.values<any[]>(this.sdmDihService.sdmData)) {
  54. for (const regionData of yearData) {
  55. //console.log(regionData, this.region);
  56. if (regionData['MODEL'] !== this.region) {
  57. continue;
  58. }
  59. for (const [factor, value] of Object.entries(regionData)) {
  60. if (
  61. ['TIME_STEP', 'MODEL', 'SCENARIO', 'DOMAIN', 'aggregated'].includes(
  62. factor
  63. )
  64. ) {
  65. continue;
  66. }
  67. years.push(regionData['TIME_STEP']);
  68. let factorBeautifulName =
  69. abbreviationMap[factor.split('/').pop()] ?? factor.split('/').pop();
  70. factorBeautifulName += ' index';
  71. factors.push(factorBeautifulName);
  72. colors.push(regionData[factor]?.index);
  73. }
  74. }
  75. }
  76. const trace1 = {
  77. x: years,
  78. y: factors,
  79. mode: 'markers',
  80. marker: {
  81. size: 10,
  82. color: colors,
  83. colorscale: [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1].map(
  84. (perc) => {
  85. return [perc, this.sdmDihService.perc2color(perc)];
  86. }
  87. ),
  88. },
  89. type: 'scatter',
  90. };
  91. this.data = [trace1];
  92. }
  93. }