@@ -175,16 +175,23 @@ const absoluteRectDimensionY = computed(() => {
175
175
})
176
176
177
177
function calculateProportions (numbers ) {
178
+ const totalSquares = FINAL_CONFIG .value .style .chart .layout .grid .size * FINAL_CONFIG .value .style .chart .layout .grid .size ;
178
179
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);
181
181
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 ;
186
192
}
187
- return proportions;
193
+
194
+ return intParts;
188
195
}
189
196
190
197
const datasetCopyReference = computed (() => {
@@ -234,7 +241,7 @@ const waffleSet = computed(() => {
234
241
color: serie .color ,
235
242
value: (serie .values || []).reduce ((a ,b ) => a + b, 0 ),
236
243
absoluteValues: serie .values || [],
237
- proportion: proportions .value [i] * Math . pow ( FINAL_CONFIG . value . style . chart . layout . grid . size , 2 )
244
+ proportion: proportions .value [i]
238
245
}
239
246
})
240
247
});
@@ -249,7 +256,7 @@ const immutableSet = computed(() => {
249
256
color: serie .color ,
250
257
value: (serie .values || []).reduce ((a ,b ) => a + b, 0 ),
251
258
absoluteValues: serie .values || [],
252
- proportion: immutableProportions .value [i] * Math . pow ( FINAL_CONFIG . value . style . chart . layout . grid . size , 2 )
259
+ proportion: immutableProportions .value [i]
253
260
}
254
261
})
255
262
});
@@ -260,24 +267,30 @@ function getData() {
260
267
name: ds .name ,
261
268
color: ds .color ,
262
269
value: ds .value ,
263
- proportion: ds .proportion / ( Math . pow ( FINAL_CONFIG . value . style . chart . layout . grid . size , 2 ))
270
+ proportion: ds .proportion
264
271
}
265
272
});
266
273
}
267
274
268
275
const cumulatedSet = computed (() => {
276
+ let cumulativeProportion = 0 ;
277
+
269
278
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
+
272
282
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);
275
285
}
286
+
287
+ cumulativeProportion = end;
288
+
276
289
return {
277
290
... 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
+ };
281
294
});
282
295
});
283
296
0 commit comments