Skip to content

Commit 904a60e

Browse files
committed
Fix - VueUiWaffle - Fix bad proportion calculations
1 parent ff1c8bc commit 904a60e

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/components/vue-ui-waffle.vue

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,23 @@ const absoluteRectDimensionY = computed(() => {
175175
})
176176
177177
function calculateProportions(numbers) {
178+
const totalSquares = FINAL_CONFIG.value.style.chart.layout.grid.size * FINAL_CONFIG.value.style.chart.layout.grid.size;
178179
const totalSum = numbers.reduce((a, b) => a + b, 0);
179-
const proportions = numbers.map(num => Math.round((num / totalSum) * 100) / 100);
180-
const roundedSum = proportions.reduce((a, b) => a + b, 0);
180+
const proportions = numbers.map(num => (num / totalSum) * totalSquares);
181181
182-
if (roundedSum !== 1) {
183-
const lastIndex = proportions.length - 1;
184-
proportions[lastIndex] += (1 - roundedSum);
185-
proportions[lastIndex] = Math.round(proportions[lastIndex] * 100) / 100;
182+
const intParts = proportions.map(Math.floor);
183+
const fractionalParts = proportions.map(num => num % 1);
184+
185+
let remainingSquares = totalSquares - intParts.reduce((a, b) => a + b, 0);
186+
187+
while (remainingSquares > 0) {
188+
let maxIndex = fractionalParts.indexOf(Math.max(...fractionalParts));
189+
intParts[maxIndex] += 1;
190+
fractionalParts[maxIndex] = 0;
191+
remainingSquares -= 1;
186192
}
187-
return proportions;
193+
194+
return intParts;
188195
}
189196
190197
const datasetCopyReference = computed(() => {
@@ -234,7 +241,7 @@ const waffleSet = computed(() => {
234241
color: serie.color,
235242
value: (serie.values || []).reduce((a,b) => a + b, 0),
236243
absoluteValues: serie.values || [],
237-
proportion: proportions.value[i] * Math.pow(FINAL_CONFIG.value.style.chart.layout.grid.size, 2)
244+
proportion: proportions.value[i]
238245
}
239246
})
240247
});
@@ -249,7 +256,7 @@ const immutableSet = computed(() => {
249256
color: serie.color,
250257
value: (serie.values || []).reduce((a,b) => a + b, 0),
251258
absoluteValues: serie.values || [],
252-
proportion: immutableProportions.value[i] * Math.pow(FINAL_CONFIG.value.style.chart.layout.grid.size, 2)
259+
proportion: immutableProportions.value[i]
253260
}
254261
})
255262
});
@@ -260,24 +267,30 @@ function getData() {
260267
name: ds.name,
261268
color: ds.color,
262269
value: ds.value,
263-
proportion: ds.proportion / (Math.pow(FINAL_CONFIG.value.style.chart.layout.grid.size, 2))
270+
proportion: ds.proportion
264271
}
265272
});
266273
}
267274
268275
const cumulatedSet = computed(() => {
276+
let cumulativeProportion = 0;
277+
269278
return waffleSet.value.map((serie, i) => {
270-
const start = i > 0 ? waffleSet.value.filter((_,j) => j < i).map(el => el.proportion).reduce((a,b) => a + b) + serie.proportion - waffleSet.value[i - 1].proportion: serie.proportion - serie.proportion;
271-
const end = start + serie.proportion;
279+
const start = cumulativeProportion;
280+
const end = start + serie.proportion;
281+
272282
const rects = [];
273-
for(let j = start; j <= end; j += 1) {
274-
rects.push(j)
283+
for (let j = Math.floor(start); j < Math.floor(end); j += 1) {
284+
rects.push(j);
275285
}
286+
287+
cumulativeProportion = end;
288+
276289
return {
277290
...serie,
278-
start: i > 0 ? waffleSet.value.filter((_,j) => j < i).map(el => el.proportion).reduce((a,b) => a + b) + serie.proportion - waffleSet.value[i - 1].proportion: serie.proportion - serie.proportion,
279-
rects
280-
}
291+
start,
292+
rects,
293+
};
281294
});
282295
});
283296

0 commit comments

Comments
 (0)