UNPKG

@figliolia/rn-donut-chart

Version:
50 lines (49 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Controller = void 0; class Controller { constructor(data) { this.renderPhase = "INITIAL"; this.data = data; this.total = this.sum(); this.percentages = this.toStrokeLengths(); this.angles = this.createAngles(); } update(data) { if (data === this.data) { return; } this.renderPhase = "UPDATE"; this.data = data; this.total = this.sum(); this.percentages = this.toStrokeLengths(); this.angles = this.createAngles(); } sum() { return this.data.reduce((acc, next) => (acc += next.value), 0); } toStrokeLengths() { return this.data.map((d) => d.value / this.total); } createAngles() { const { length } = this.data; const angles = new Array(this.data.length).fill(0); for (let i = 1; i < length; i++) { angles[i] = angles[i - 1] + this.percentages[i - 1] * 360; } return angles; } static parseStroke(label, stroke) { if (Array.isArray(stroke)) { if (stroke.length === 1) { return stroke[0]; } return `url(#${label})`; } return stroke; } static renderGradient(stroke) { return Array.isArray(stroke) && stroke.length > 1; } } exports.Controller = Controller;