app_mapbuilder/libs/math.js
2022-04-22 18:34:31 +05:00

14 lines
802 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default {
/**
* Создает данные дл круговой диаграммы из набора значений v0,v1,v2,v3 -> [{a0: 0, a1: v0}, {a0: v0, a1: v0+v1}, {a0: v0+v1, a1: v0+v1+v2},]
* @param { [Number] } values Значения долей
* @param { boolean } norm Нормировать ли значения диапазонов в отрезок 0-1
* @returns Массив с диапазонами долей 1,2,3,4 -> [{0,0.1},{0.1,0.3},{0.3,0.6},{0.6,1}]
*/
make_ranges(values, norm = true) {
let ranges = values.reduce((s, c, i) => [...s, {...c, a0: i && s[i - 1].a1, a1: c + (i && s[i - 1].a1)}], [])
let max = ranges[ranges.length - 1].a1
return norm ? ranges.map(x => ({a0: x.a0 / max, a1: x.a1 / max})) : ranges
},
};