function calcMaxValueForDataset(data, questionKey, responseKey, weights) {
const weightsLookup = Object.fromEntries(weights);
return d3
.groups(
data,
(d) => d[questionKey],
(d) => d[responseKey]
)
.reduce((max, question, i, qs) => {
let neutralsVal =
question[1]
.filter((r) => weightsLookup[r[0]] === 0)
.reduce((length, arr) => (length += arr[1].length), 0) / 2 || 0;
let largestNegVal =
question[1]
.filter((r) => weightsLookup[r[0]] < 0)
.reduce((length, arr) => (length += arr[1].length), 0) +
neutralsVal || 0;
let largestPosVal =
question[1]
.filter((r) => weightsLookup[r[0]] < 0)
.reduce((length, arr) => (length += arr[1].length), 0) +
neutralsVal || 0;
if (largestNegVal > max) max = largestNegVal;
if (largestPosVal > max) max = largestPosVal;
return max;
}, 0);
}