. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Statistics\Google; use Fisharebest\Localization\Locale\LocaleInterface; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Module\ModuleThemeInterface; use Fisharebest\Webtrees\Statistics\Service\ColorService; use Psr\Http\Message\ServerRequestInterface; use function app; use function assert; use function count; /** * A chart showing the top given names. */ class ChartCommonGiven { /** * @var ModuleThemeInterface */ private $theme; /** * @var ColorService */ private $color_service; /** * Constructor. */ public function __construct() { $this->theme = app(ModuleThemeInterface::class); $this->color_service = new ColorService(); } /** * Create a chart of common given names. * * @param int $tot_indi The total number of individuals * @param array $given The list of common given names * @param string|null $color_from * @param string|null $color_to * * @return string */ public function chartCommonGiven( int $tot_indi, array $given, string $color_from = null, string $color_to = null ): string { $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values'); $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values'); $color_from = $color_from ?? $chart_color1; $color_to = $color_to ?? $chart_color2; $tot = 0; foreach ($given as $count) { $tot += $count; } $data = [ [ I18N::translate('Name'), I18N::translate('Total') ], ]; foreach ($given as $name => $count) { $data[] = [ $name, $count ]; } $data[] = [ I18N::translate('Other'), $tot_indi - $tot ]; $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1); $locale = app(ServerRequestInterface::class)->getAttribute('locale'); assert($locale instanceof LocaleInterface); return view('statistics/other/charts/pie', [ 'title' => null, 'data' => $data, 'colors' => $colors, 'language' => $locale->languageTag(), ]); } }