-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundLevelChart.js
90 lines (86 loc) · 2.4 KB
/
SoundLevelChart.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
import { recordingCoverageWithMeta } from "../utils/helpers";
import withChartData from "../hoc/withChartData";
import { backfillData } from "../utils/data";
import SoundLevelChartLayout from "./SoundLevelChartLayout";
function restructureSoundLevelData(
exceedanceData,
noiseData,
fromDate,
toDate
) {
// Group in to a nested structure
const result = [];
exceedanceData.map(({ band, date, threshold, value }) => {
const bandIndex = result.findIndex((row) => row.band === band);
if (bandIndex === -1) {
result.push({
band,
exceedance: [
{
threshold,
rows: [{ date, value }],
},
],
});
} else {
const thresholdIndex = result[bandIndex].exceedance.findIndex(
(row) => row.threshold === threshold
);
if (thresholdIndex === -1) {
result[bandIndex].exceedance.push({
threshold,
rows: [{ date, value }],
});
} else {
result[bandIndex].exceedance[thresholdIndex].rows.push({
date,
value,
});
}
}
});
noiseData.map(({ band, date, value }) => {
const bandIndex = result.findIndex((row) => row.band === band);
if (bandIndex === -1) {
result.push({
band,
noise: [{ date, value }],
});
} else if (result[bandIndex].noise) {
result[bandIndex].noise.push({
date,
value,
});
} else {
result[bandIndex].noise = [{ date, value }];
}
});
// Add any missing values for the date range
result.forEach((bandRow) => {
bandRow.exceedance.forEach((thresholdRow) => {
thresholdRow.rows = backfillData(thresholdRow.rows, fromDate, toDate);
});
bandRow.noise = backfillData(bandRow.noise, fromDate, toDate);
});
return result;
}
const SoundLevelChart = withChartData(SoundLevelChartLayout, {
dataPointTypes: [`recordingCoverage`, `exceedance`, `noise`],
chartDataFromResponses: (responses, { fromDate, toDate } = {}) => {
const [recordingCoverageResult, exceedanceResult, noiseResult] = responses;
return {
soundLevelData: restructureSoundLevelData(
exceedanceResult.data,
noiseResult.data,
fromDate,
toDate
),
recordingCoverageData: recordingCoverageWithMeta(
recordingCoverageResult.data,
fromDate,
toDate
),
};
},
});
export default SoundLevelChart;