-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-WeatherChartD3.js
executable file
·963 lines (873 loc) · 34.1 KB
/
MMM-WeatherChartD3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/**
* This MagicMirror² module displays a weather chart using any weather provider.
* It can display temperature, feels like temperature, precipitation, snow and weather icons.
* It uses the D3.js library.
* @module MMM-WeatherChartD3
* @class Module
* @see `README.md`
* @author Sébastien Mazzon
* @license MIT - @see `LICENCE.txt`
*/
"use strict";
Module.register("MMM-WeatherChartD3", {
/**
* Default properties of the module
* @see `module.defaults`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#defaults>
*/
defaults: {
updateInterval: 10 * 60 * 1000,
initialLoadDelay: 0, // 0 seconds delay
animationSpeed: 1000,
weatherProvider: "openweathermap",
weatherEndpoint: "/onecall",
type: "full", // Possible values: hourly, forecast (=daily) or specific value `full` which is a join of data from hourly+daily
apiKey: "",
lat: 0,
lon: 0,
lang: config.language,
units: config.units,
locale: config.locale,
d3jsVersion: "7", // can either be in format "7.4" or even "7.4.4"
height: 300,
width: 500,
iconSize: undefined, // in px or undefined to define automatically at first call
iconURLBase: "https://raw.githubusercontent.com/erikflowers/weather-icons/master/svg/",
hoursRatio: 0, // Ratio of fetched hours in graph (useful for openweathermap onecall that gives 48h with 1h precision) - 0 or undefined to ignore
showIcons: true,
showNights: true,
showTemperature: true,
showMinMaxTemperature: false,
showFeelsLikeTemp: true,
showPrecipitationAmount: true,
showPrecipitationProbability: true, // Only used when showPrecipitationAmount == true
showSnow: true, // if false: snow is included in precipitations
showPressure: true,
showHumidity: true,
showWind: true,
showAQI: true,
showUVI: true,
},
/**
* Number of calls to `updateAvailable` before triggering `updateDom` (set by `scheduleUpdate`)
*/
nbUpdateWait: 0,
/**
* Initializes module
* @see `module.start`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#start>
*/
start: function () {
// Initializes and starts the weather provider
this.weatherProvider = WeatherProvider.initialize(this.config.weatherProvider, this);
this.weatherProvider.start();
// Loads D3 locale
(async () => {
await d3.json(`https://unpkg.com/d3-time-format@4/locale/${this.config.locale}.json`).then(function (locale) {
d3.timeFormatDefaultLocale(locale);
});
})();
// Schedules the first update
this.scheduleUpdate(this.config.initialLoadDelay);
},
/**
* Returns the CSS files used by getDom
* @see `module.getStyles`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#getstyles>
* @returns {Array}
*/
getStyles: function () {
return [`${this.name}.css`];
},
/**
* Returns the scripts necessary for the chart and weather provider
* @see <https://docs.magicmirror.builders/development/core-module-file.html#getscripts>
* @returns {string[]} An array with filenames
*/
getScripts: function () {
const pathWeather = "modules/default/weather/";
return [
// Loads d3 from CDN
`https://cdn.jsdelivr.net/npm/d3@${this.config.d3jsVersion}/dist/d3.min.js`,
"suncalc.js",
`${pathWeather}providers/${this.config.weatherProvider.toLowerCase()}.js`,
];
},
/**
* Called when the provider has retrieved data
*/
updateAvailable: function () {
this.nbUpdateWait--;
if (this.nbUpdateWait <= 0) {
// No more waiting call - update DOM with all the available data and schedule next update
Log.log("New weather information available.");
this.updateDom(this.config.animationSpeed);
this.scheduleUpdate();
}
},
/**
* Called when the module is hidden
* @see `module.suspend`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#suspend>
*/
suspend: function () {
// Stop scheduled updates
if (this.timer) {
clearTimeout(this.timer);
}
},
/**
* Called when the module is shown
* @see `module.resume`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#resume>
*/
resume: function () {
// Instantly restart scheduled updates
this.scheduleUpdate(0);
},
/**
* Returns the title of the module
* @see <https://docs.magicmirror.builders/development/core-module-file.html#getheader>
* @returns {string}
*/
getHeader: function () {
return this.data.header || this.translate("TITLE");
},
/**
* Schedules next data retrieving
* @param {integer} delay Delay before updating - use config.updateInterval if null
*/
scheduleUpdate: function (delay = null) {
let nextLoad = this.config.updateInterval;
if (delay !== null && delay >= 0) {
nextLoad = delay;
}
this.timer = setTimeout(() => {
switch (this.config.type.toLowerCase()) {
case "hourly":
this.nbUpdateWait = 1;
this.weatherProvider.fetchWeatherHourly();
break;
case "daily":
case "forecast":
this.nbUpdateWait = 1;
this.weatherProvider.fetchWeatherForecast();
break;
case "full":
this.nbUpdateWait = 2;
if (this.config.showAQI && typeof this.weatherProvider.fetchCurrentPollution === "function") {
this.nbUpdateWait++;
this.weatherProvider.fetchPollutionForecast();
}
this.weatherProvider.fetchWeatherHourly();
this.weatherProvider.fetchWeatherForecast();
break;
default:
this.nbUpdateWait = 0;
Log.error(`Invalid type ${this.config.type} configured (must be one of 'hourly', 'daily', 'forecast' or 'full')`);
}
}, nextLoad);
},
/**
* Returns value or a fallback if value is not a number
* @param {*} value Value to check
* @param {*} fallback Fallback value
* @returns Fallback if value if is null or not a number, else: value
*/
ifNan: function (value, fallback) {
return (isNaN(value) || value === null) ? fallback : value;
},
/**
* Generates the DOM containing the chart
*
* @see `module.getDom`
* @see <https://docs.magicmirror.builders/development/core-module-file.html#getdom>
* @returns {HTMLElement|Promise} The DOM to display
*/
getDom: function () {
const promises = [];
const ifDef = (value, fallback) => (typeof (value) === "undefined" || value === null) ? fallback : value;
const dataHourly = ifDef(this.weatherProvider.weatherHourly(), []);
let dataDaily = ifDef(this.weatherProvider.weatherForecast(), []);
const dataPollution = ifDef(this.weatherProvider.pollutionForecast(), []);
if (dataHourly.length > 0 || dataDaily.length > 0) {
if (dataHourly.length > 0 && dataDaily.length > 0) {
// Remove current day and next day of dataDaily (provided by dataHourly)
const dateMaxHourly = d3.max(dataHourly, d => d.date);
dataDaily = dataDaily.filter(d => d.date.isAfter(dateMaxHourly));
}
// Merge and sort data
const sortedData = d3.sort([].concat(dataHourly).concat(dataDaily), d => d.date);
// Frame
const margins = { top: 0, right: 10, bottom: 30, left: 10 };
const legendBarWidth = 55;
const innerWidth = this.config.width - margins.left - margins.right - 2 * legendBarWidth;
// Define x scale
let xTime;
if (dataHourly.length > 0 && dataDaily.length > 0) {
let rangeX = [0, innerWidth];
let domainX = [d3.min(dataHourly, d => d.date), d3.max(dataDaily, d => d.date)];
if (this.ifNan(this.config.hoursRatio, 0) !== 0) {
rangeX = [0, innerWidth * (1 - this.config.hoursRatio), innerWidth];
domainX = [d3.min(dataHourly, d => d.date), d3.min(dataDaily, d => d.date), d3.max(dataDaily, d => d.date)];
}
xTime = d3.scaleTime()
.domain(domainX)
.range(rangeX);
} else {
xTime = d3.scaleTime()
.domain(d3.extent(sortedData, d => d.date))
.range([0, innerWidth]);
}
// Define icon size and gap between icons
if (this.config.iconSize === undefined) {
let minDelta = Infinity;
for (let i = 1; i < sortedData.length; i++) {
const delta = xTime(sortedData[i].date) - xTime(sortedData[i - 1].date)
if (minDelta > delta && delta > 0) {
minDelta = delta;
}
}
const magnifier = this.config.width / minDelta / 30; // Empiric value
this.config.iconSize = minDelta * magnifier;
}
// Frame
margins.top = this.config.iconSize;
const innerHeight = this.config.height - margins.top - margins.bottom - legendBarWidth;
// Remove existing svg
d3.select(`#${this.identifier} svg`).remove();
// Adds new svg
const svg = d3.select(`#${this.identifier}`)
.append("svg")
.attr("width", this.config.width)
.attr("height", this.config.height)
.append("g")
.attr("id", "grp")
.attr("transform", `translate(${margins.left + legendBarWidth}, ${margins.top})`);
// Adds Y axis (temperature)
const yTemp = d3.scaleLinear()
.domain([
d3.min(sortedData, d => Math.min(this.ifNan(d.temperature, 0), this.ifNan(d.minTemperature, 0), this.ifNan(d.feelsLikeTemp, 0)) - 1),
d3.max(sortedData, d => Math.max(this.ifNan(d.temperature, 40), this.ifNan(d.maxTemperature, 40), this.ifNan(d.feelsLikeTemp, 40)) + 1)
])
.range([innerHeight, 0]);
// Adds grids and axis
promises.push(this.addGridAndAxis(svg, dataHourly, dataDaily, xTime, innerHeight, legendBarWidth));
// Adds day/night
if (this.config.showNights && sortedData.length > 1) {
promises.push(this.svgAddDayNight(svg, sortedData, xTime, innerWidth, innerHeight, margins, legendBarWidth));
}
// Adds precipitation (rain/snow)
if (this.config.showPrecipitationAmount) {
promises.push(this.svgAddPrecipitation(svg, sortedData, xTime, innerWidth, innerHeight, margins));
}
// Adds pressure
if (this.config.showPressure) {
promises.push(this.svgAddPressure(svg, sortedData, xTime, innerWidth, innerHeight, margins));
}
// Adds temperature min/max
if (this.config.showMinMaxTemperature) {
promises.push(this.svgAddTemperatureMinMax(svg, sortedData, xTime, innerWidth, innerHeight, margins, yTemp));
}
// Adds temperature
if (this.config.showTemperature) {
promises.push(this.svgAddTemperature(svg, sortedData, xTime, innerWidth, innerHeight, margins, yTemp));
}
// Adds feels alike temperature
if (this.config.showFeelsLikeTemp) {
promises.push(this.svgAddFeelsAlikeTemperature(svg, sortedData, xTime, innerWidth, innerHeight, margins, yTemp));
}
// Adds weather icons
if (this.config.showIcons) {
promises.push(this.svgAddWeatherIcons(svg, sortedData, xTime, innerWidth, innerHeight, margins));
}
// Adds Humidity
if (this.config.showHumidity) {
promises.push(this.svgAddHumidity(svg, sortedData, xTime, innerWidth, innerHeight, margins));
}
// Adds Wind
if (this.config.showWind) {
promises.push(this.svgAddWind(svg, sortedData, xTime, innerWidth, innerHeight, margins));
}
// Adds UVI
if (this.config.showUVI) {
promises.push(this.svgAddUvi(svg, sortedData, xTime, innerWidth, innerHeight, margins));
}
// Adds AQI
if (this.config.showAQI) {
promises.push(this.svgAddAqi(svg, dataPollution, xTime, innerWidth, innerHeight, margins));
}
}
// SVG is directly added into div module
return Promise.all(promises).then(() => document.createElement("div"));
},
/**
* Returns an array without intermediate values (only local min and max values)
* @param {Array} data Array to filter
* @param {Function} fctGet Function to be called with an item of data to get value
* @param {Number} minDelta Minimum delta between 2 values to keep it - undefined to ignore
* @returns {Array} data with only local min/max values
*/
keepExtremes: function (data, fctGet, minDelta) {
let direction = data.length <= 1 || fctGet(data[1]) > fctGet(data[0]) ? -1 : 1;
const startIndexToDisplay = 2; // Don't keep first value to avoid display on left axis
const result = [];
let lastValue = Number.MAX_SAFE_INTEGER;
for (let i = startIndexToDisplay; i < data.length; i++) {
const d0 = fctGet(data[i - 1]);
const d1 = fctGet(data[i]);
if (i == data.length - 1 || (direction > 0 && d1 < d0) || (direction < 0 && d1 > d0)) {
direction *= -1;
if (minDelta === undefined || Math.abs(d0 - lastValue) > minDelta) {
lastValue = d0;
result.push(data[i - 1]);
}
}
}
return result;
},
/**
* Adds grids and axis to SVG
* @param {svg} svg SVG of the chart
* @param {Array} dataHourly Data of weatherHourly
* @param {Array} dataDaily Data of weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {integer} legendBarWidth Width of the legend (in pixels)
*/
addGridAndAxis: async function (svg, dataHourly, dataDaily, xTime, innerHeight, legendBarWidth) {
// X axis (date)
svg.append("g")
.attr("id", "x-axis-hours")
.attr("class", "x-axis")
.attr("transform", `translate(0, ${innerHeight})`)
.call(d3.axisBottom(xTime)
.tickValues(d3.timeHour.every(3).range(d3.min(dataHourly, d => d.date), d3.max(dataHourly, d => d.date))
.concat(d3.timeHour.every(6).range(d3.min(dataDaily, d => d.date), d3.max(dataDaily, d => d.date))))
.tickFormat(d3.timeFormat("%Hh"))
);
// Rotate hours legend
svg.selectAll("#x-axis-hours text")
.attr("text-anchor", "end")
.attr("dx", "-0.8em")
.attr("dy", "0.15em")
.attr("transform", "rotate(-65)");
// X gridline
svg.append("g")
.attr("id", "x-axis-days")
.attr("class", "x-axis-grid")
.attr("transform", `translate(0, ${innerHeight})`)
.call(d3.axisBottom(xTime)
.ticks(d3.timeDay.every(1))
.tickSize(-innerHeight, 0, 0).tickPadding(legendBarWidth)
.tickFormat(d3.timeFormat("%a %d")))
// Shift text to start of tick
.selectAll("text").attr("text-anchor", "start");
},
/**
* Adds day/night to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddDayNight: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins, legendBarWidth) {
let sunTimesData = [];
let iterd = sortedData[0].date;
while (iterd <= sortedData[sortedData.length - 1].date) {
sunTimesData.push(SunCalc.getTimes(iterd, this.config.lat, this.config.lon));
iterd = iterd.clone().add(1, "d");
}
const fctNightWidth = (d1, d2) => Math.max(0, Math.min(innerWidth, d2 ? xTime(d2.sunrise) : innerWidth) - Math.max(0, xTime(d1.sunset)));
// In graph
svg.selectAll("grp").append("g")
.data(sunTimesData).enter()
.append("rect")
.attr("class", "night")
.attr("x", d => Math.max(xTime(d.sunset), 0))
.attr("y", -this.config.iconSize)
.attr("width", (d, i) => fctNightWidth(d, sunTimesData[i + 1]))
.attr("height", innerHeight + this.config.iconSize);
// In axis
svg.selectAll("grp").append("g")
.data(sunTimesData).enter()
.append("rect")
.attr("class", "axis-night")
.attr("x", d => Math.max(xTime(d.sunset), 0))
.attr("y", innerHeight)
.attr("width", (d, i) => fctNightWidth(d, sunTimesData[i + 1]))
.attr("height", legendBarWidth);
},
/**
* Adds precipitation and snow to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddPrecipitation: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins) {
let data = sortedData.filter(d => d.precipitationAmount !== undefined);
// Add slot duration in ms
data.forEach((d, i) => d.period = Math.abs(d.date.diff(data[i + (i + 1 < data.length ? 1 : -1)].date)));
data = data.filter(d => d.precipitationAmount !== null);
const self = this;
const getHeightPrecipitation = function (d, withRain = true, withSnow = true) {
const deltaInHours = d.period / (60 * 60 * 1000); // ms to hours
const precipitations = (withRain ? d.rain ?? 0 : 0) + (withSnow ? d.snow ?? 0 : 0);
return parseFloat((precipitations / deltaInHours).toFixed(2));
}
/*
// Y axis
svg.append("g")
.attr("class", "y-axis")
.attr("transform", `translate(${innerWidth}, 0)`)
.call(d3.axisRight(yAxis));
*/
if (data.length > 0) {
const maxPrecipitations = d3.max(data, d => getHeightPrecipitation(d));
const minDelta = d3.min(data, d => d.period);
const yAxis = d3.scaleLinear()
.domain([0, Math.max(5, maxPrecipitations)]) // world record: ~300mm for an hour
.range([innerHeight, 0]);
// Y axis icon
svg.append("image")
.attr("class", "precipitation axis-icon")
.attr("x", -this.config.iconSize / 2)
.attr("y", yAxis(maxPrecipitations) - this.config.iconSize / 4)
.attr("xlink:href", `${this.config.iconURLBase}/wi-raindrop.svg`)
.attr("width", this.config.iconSize / 2);
// Y axis (rain) label
svg.append("text")
.attr("class", "rain axis-label")
.attr("x", innerWidth + margins.left)
.attr("y", yAxis(getHeightPrecipitation(data[data.length - 1])))
.attr("text-anchor", "start")
.text(this.config.units === "imperial" ? "in/h" : "mm/h");
// Rain/precipitations
svg.selectAll("grp").append("g")
.data(data).enter()
.append("rect")
.attr("class", "precipitation curve")
.attr("x", d => xTime(d.date))
.attr("y", yAxis(0))
.attr("transform", d => `translate(0, ${-yAxis(0) + yAxis(getHeightPrecipitation(d, true, !this.config.showSnow))})`)
.attr("width", d => Math.min(innerWidth, xTime(d.date + d.period)) - xTime(d.date))
.attr("height", d => yAxis(0) - yAxis(getHeightPrecipitation(d, true, !this.config.showSnow)));
if (this.config.showSnow) {
// Snow
svg.selectAll("grp").append("g")
.data(data.filter(d => d.snow && d.snow !== null)).enter()
.append("rect")
.attr("class", "snow curve")
.attr("x", d => xTime(d.date))
.attr("y", yAxis(0))
.attr("transform", d => `translate(0, ${-yAxis(0) + yAxis(getHeightPrecipitation(d, false, true))})`)
.attr("width", d => Math.min(innerWidth, xTime(d.date + d.period)) - xTime(d.date))
.attr("height", d => yAxis(0) - yAxis(getHeightPrecipitation(d, false, true)));
}
// Precipitation probability
let getProba = (d) => "";
if (this.config.showPrecipitationProbability) {
getProba = (d) => `(${d.precipitationProbability.toFixed(0)}%)`;
}
const dataExtremes = this.keepExtremes(data, d => getHeightPrecipitation(d), 0.5);
// Local min/max values as text
svg.selectAll("grp")
.data(dataExtremes).enter()
.append("text")
.attr("class", "precipitation curve-value")
.attr("text-anchor", "start")
.attr("x", d => xTime(d.date))
.attr("y", d => yAxis(getHeightPrecipitation(d)))
.text(d => `${(getHeightPrecipitation(d)).toFixed(1)} ${getProba(d)}`);
}
},
/**
* Adds min/max temperature to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
* @param {d3.scaleLinear} yTemp Y-axis scale (temperature)
*/
svgAddTemperatureMinMax: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins, yTemp) {
svg.append("path")
.datum(sortedData.filter(d => d.minTemperature && d.minTemperature !== null && d.minTemperature && d.minTemperature !== null))
.attr("class", "min-max-temperature curve")
.attr("d", d3.area().curve(d3.curveCardinal.tension(0.3))
.x(d => xTime(d.date))
.y0(d => yTemp(parseFloat(d.minTemperature.toFixed(1))))
.y1(d => yTemp(parseFloat(d.maxTemperature.toFixed(1))))
);
},
/**
* Adds temperature to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
* @param {d3.scaleLinear} yTemp Y-axis scale (temperature)
*/
svgAddTemperature: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins, yTemp) {
const data = sortedData.filter(d => d.temperature && d.temperature !== null);
const getValue = d => parseFloat(d.temperature.toFixed(1));
/*
// Y axis
svg.append("g")
.attr("class", "y-axis")
.call(d3.axisLeft(yTemp));
*/
// Y axis icon
svg.append("image")
.attr("class", "temperature axis-icon")
.attr("x", -this.config.iconSize / 2)
.attr("y", yTemp(getValue(data[0])) - this.config.iconSize / 4)
.attr("xlink:href", `${this.config.iconURLBase}/wi-thermometer.svg`)
.attr("width", this.config.iconSize / 2);
// Y axis label
svg.append("text")
.attr("class", "temperature axis-label")
.attr("text-anchor", "start")
.attr("x", innerWidth + margins.left)
.attr("y", yTemp(getValue(data[data.length - 1])))
.text(this.config.units === "imperial" ? "°F" : "°C");
// Curve
svg.append("path")
.datum(data)
.attr("class", "temperature curve")
.attr("d", d3.line().curve(d3.curveCardinal.tension(0.3))
.x(d => xTime(d.date))
.y(d => yTemp(getValue(d)))
);
const dataExtremes = this.keepExtremes(data, d => getValue(d), 1);
// Local min/max values as text
svg.selectAll("grp")
.data(dataExtremes).enter()
.append("text")
.attr("class", "temperature curve-value")
.attr("text-anchor", "middle")
.attr("x", d => xTime(d.date))
.attr("y", d => yTemp(getValue(d)))
.attr("dy", (d, i) => `${((i > 0 && getValue(d) > getValue(dataExtremes[i - 1])) || (i == 0 && getValue(d) > getValue(dataExtremes[i + 1]))) ? -0.75 : 1.5}em`)
.text(d => getValue(d));
},
/**
* Adds feels alike temperature to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
* @param {d3.scaleLinear} yTemp Y-axis scale (temperature)
*/
svgAddFeelsAlikeTemperature: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins, yTemp) {
const data = sortedData.filter(d => d.feelsLikeTemp && d.feelsLikeTemp !== null);
const getValue = d => parseFloat(d.feelsLikeTemp.toFixed(1));
// Curve
svg.append("path")
.datum(data)
.attr("class", "feelsLikeTemp curve")
.attr("d", d3.line().curve(d3.curveCardinal.tension(0.3))
.x(d => xTime(d.date))
.y(d => yTemp(getValue(d)))
);
},
/**
* Adds weather icons to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddWeatherIcons: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins,) {
let lastIcon = undefined; // static
function differentThanPrevious(d) {
const res = d.weatherType && lastIcon !== d.weatherType;
lastIcon = d.weatherType;
return res;
}
const dataIcons = sortedData.filter(differentThanPrevious) // Display only icons that are different from previous
let lastPosNb = 1;
let sumLastStack = 0;
// Un-align icons if previous is too close
function yAxis(d, i) {
let nb = 1;
if (i > 0) {
nb = (xTime(d.date) - xTime(dataIcons[i - 1].date)) / this.config.iconSize;
}
sumLastStack += nb * this.config.iconSize;
if (nb < 1 && sumLastStack < this.config.iconSize) {
return -this.config.iconSize + lastPosNb++ * this.config.iconSize / 1.5;
} else {
sumLastStack = 0;
lastPosNb = 1;
return -this.config.iconSize;
}
}
// Icons
svg.selectAll("grp").append("g")
.data(dataIcons).enter()
.append("image") // NB: Adding text with classes "wi weathericon" does not work, so: using svg instead
.attr("class", "weather curve")
.attr("xlink:href", d => `${this.config.iconURLBase}/wi-${d.weatherType}.svg`)
.attr("x", d => xTime(d.date))
.attr("y", yAxis.bind(this))
.attr("width", this.config.iconSize);
},
/**
* Adds pressure to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddPressure: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins) {
const data = sortedData.filter(d => d.pressure && d.pressure !== null);
const getValue = d => parseFloat(d.pressure.toFixed(0));
const yAxis = d3.scaleLinear()
.domain([Math.min(950, d3.min(data, d => getValue(d))), Math.max(1050, d3.max(data, d => getValue(d)))])
.range([innerHeight, 0]);
// Y axis icon
svg.append("image")
.attr("class", "pressure axis-icon")
.attr("x", -this.config.iconSize / 2)
.attr("y", yAxis(getValue(data[0])) - this.config.iconSize / 4)
.attr("xlink:href", `${this.config.iconURLBase}/wi-barometer.svg`)
.attr("width", this.config.iconSize / 2);
// Y axis label
svg.append("text")
.attr("class", "pressure axis-label")
.attr("text-anchor", "start")
.attr("x", innerWidth + margins.left)
.attr("y", yAxis(getValue(data[data.length - 1])))
.text("hPa");
// Curve
svg.append("path")
.datum(data)
.attr("class", "pressure curve")
.attr("d", d3.line().curve(d3.curveCardinal.tension(0.3))
.x(d => xTime(d.date))
.y(d => yAxis(getValue(d)))
);
const dataExtremes = this.keepExtremes(data, d => getValue(d), 1);
// Local min/max values as text
svg.selectAll("grp")
.data(dataExtremes).enter()
.append("text")
.attr("class", "pressure curve-value")
.attr("text-anchor", "middle")
.attr("x", d => xTime(d.date))
.attr("y", d => yAxis(getValue(d)))
.attr("dy", (d, i) => `${(i > 0 && getValue(d) > getValue(dataExtremes[i - 1])) ? -0.75 : 1}em`)
.text(d => getValue(d));
},
/**
* Adds humidity to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddHumidity: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins) {
const data = sortedData.filter(d => d.humidity && d.humidity !== null);
const getValue = d => parseFloat(d.humidity.toFixed(0));
const yAxis = d3.scaleLinear()
.domain([0, 100])
.range([innerHeight, 0]);
// Y axis icon
svg.append("image")
.attr("class", "humidity axis-icon")
.attr("x", -this.config.iconSize / 2)
.attr("y", yAxis(getValue(data[0])) - this.config.iconSize / 4)
.attr("xlink:href", `${this.config.iconURLBase}/wi-humidity.svg`)
.attr("width", this.config.iconSize / 2);
// Y axis label
svg.append("text")
.attr("class", "humidity axis-label")
.attr("text-anchor", "start")
.attr("x", innerWidth + margins.left)
.attr("y", yAxis(getValue(data[data.length - 1])))
.text("%");
// Curve
svg.append("path")
.datum(data)
.attr("class", "humidity curve")
.attr("d", d3.line().curve(d3.curveCardinal.tension(0.3))
.x(d => xTime(d.date))
.y(d => yAxis(getValue(d)))
);
const dataExtremes = this.keepExtremes(data, d => getValue(d), 4);
// Local min/max values as text
svg.selectAll("grp")
.data(dataExtremes).enter()
.append("text")
.attr("class", "humidity curve-value")
.attr("text-anchor", "middle")
.attr("x", d => xTime(d.date))
.attr("y", d => yAxis(getValue(d)))
.attr("dy", (d, i) => `${(i > 0 && getValue(d) > getValue(dataExtremes[i - 1])) ? -0.75 : 1}em`)
.text(d => getValue(d));
},
/**
* Adds wind to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddWind: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins) {
const data = sortedData.filter(d => d.windSpeed && d.windSpeed !== null);
const getValue = d => parseFloat(d.windSpeed.toFixed(0));
const yAxis = d3.scaleLinear()
.domain([0, Math.max(50, d3.max(data, d => getValue(d)))])
.range([innerHeight, 0]);
// Y axis icon
svg.append("image")
.attr("class", "wind axis-icon")
.attr("x", -this.config.iconSize / 2)
.attr("y", yAxis(getValue(data[0])) - this.config.iconSize / 4)
.attr("xlink:href", `${this.config.iconURLBase}/wi-strong-wind.svg`)
.attr("width", this.config.iconSize / 2);
// Y axis label
svg.append("text")
.attr("class", "wind axis-label")
.attr("text-anchor", "start")
.attr("x", innerWidth + margins.left)
.attr("y", yAxis(getValue(data[data.length - 1])))
.text(this.config.units === "imperial" ? "mi/h" : "km/h");
// Curve
svg.append("path")
.datum(data)
.attr("class", "wind curve")
.attr("d", d3.line().curve(d3.curveCardinal.tension(0.3))
.x(d => xTime(d.date))
.y(d => yAxis(getValue(d)))
);
const dataExtremes = this.keepExtremes(data, d => getValue(d), 1);
// Local min/max values as text
svg.selectAll("grp")
.data(dataExtremes).enter()
.append("text")
.attr("class", "wind curve-value")
.attr("text-anchor", "middle")
.attr("x", d => xTime(d.date))
.attr("y", d => yAxis(getValue(d)))
.attr("dy", (d, i) => `${(i > 0 && getValue(d) > getValue(dataExtremes[i - 1])) ? -0.75 : 1}em`)
.text(d => getValue(d));
},
/**
* Adds UVI to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData Concatenation of weatherHourly and weatherDaily
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddUvi: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins) {
const data = sortedData.filter(d => d.uv_index && d.uv_index !== null);
const getValue = d => parseFloat(d.uv_index.toFixed(1));
const yAxis = d3.scaleLinear()
.domain([0, Math.max(10, d3.max(data, d => getValue(d)))])
.range([innerHeight, 0]);
// Y axis icon
svg.append("image")
.attr("class", "uvi axis-icon")
.attr("x", -this.config.iconSize / 2)
.attr("y", yAxis(getValue(data[0])) - this.config.iconSize / 4)
.attr("xlink:href", `${this.config.iconURLBase}/wi-day-sunny.svg`)
.attr("width", this.config.iconSize / 2);
// Y axis label
svg.append("text")
.attr("class", "uvi axis-label")
.attr("text-anchor", "start")
.attr("x", innerWidth + margins.left)
.attr("y", yAxis(getValue(data[data.length - 1])))
.text("UV");
// Curve
svg.append("path")
.datum(data)
.attr("class", "uvi curve")
.attr("d", d3.line().curve(d3.curveCardinal.tension(0.3))
.x(d => xTime(d.date))
.y(d => yAxis(getValue(d)))
);
const dataExtremes = this.keepExtremes(data, d => getValue(d), 1);
// Local min/max values as text
svg.selectAll("grp")
.data(dataExtremes).enter()
.append("text")
.attr("class", "uvi curve-value")
.attr("text-anchor", "middle")
.attr("x", d => xTime(d.date))
.attr("y", d => yAxis(getValue(d)))
.attr("dy", (d, i) => `${(i > 0 && getValue(d) > getValue(dataExtremes[i - 1])) ? -0.75 : 1}em`)
.text(d => getValue(d));
},
/**
* Adds AQI to SVG
* @param {svg} svg SVG of the chart
* @param {Array} sortedData pollutionForecast
* @param {d3.scaleTime} xTime X-axis scale (time)
* @param {integer} innerWidth Width of the chart (in pixels)
* @param {integer} innerHeight Height of the chart (in pixels)
* @param {top, right, bottom, left} margins Margins of the chart (in pixels)
*/
svgAddAqi: async function (svg, sortedData, xTime, innerWidth, innerHeight, margins) {
const data = sortedData.filter(d => d.aqi && d.aqi !== null);
const getValue = d => parseFloat(d.aqi.toFixed(1));
const yAxis = d3.scaleLinear()
.domain([Math.max(5, d3.max(data, d => getValue(d))), Math.max(1, d3.min(data, d => getValue(d)))])
.range([innerHeight, 2 * this.config.iconSize]);
// Y axis icon
svg.append("image")
.attr("class", "aqi axis-icon")
.attr("x", -this.config.iconSize / 2)
.attr("y", yAxis(getValue(data[0])) - this.config.iconSize / 4)
.attr("xlink:href", `${this.config.iconURLBase}/wi-train.svg`)
.attr("width", this.config.iconSize / 2);
// Y axis label
svg.append("text")
.attr("class", "aqi axis-label")
.attr("text-anchor", "start")
.attr("x", innerWidth + margins.left)
.attr("y", yAxis(getValue(data[data.length - 1])))
.text("AQI");
// Curve
svg.append("path")
.datum(data)
.attr("class", "aqi curve")
.attr("d", d3.line().curve(d3.curveCardinal.tension(1))
.x(d => xTime(d.date))
.y(d => yAxis(getValue(d)))
);
const dataExtremes = this.keepExtremes(data, d => getValue(d), 0.5);
// Local min/max values as text
svg.selectAll("grp")
.data(dataExtremes).enter()
.append("text")
.attr("class", "aqi curve-value")
.attr("text-anchor", "middle")
.attr("x", d => xTime(d.date))
.attr("y", d => yAxis(getValue(d)))
.attr("dy", (d, i) => `${(i > 0 && getValue(d) > getValue(dataExtremes[i - 1])) ? 1 : -0.75}em`)
.text(d => getValue(d));
},
});