Skip to content

Commit

Permalink
Merge c0cd341 into 39b853a
Browse files Browse the repository at this point in the history
  • Loading branch information
magicsunday committed Feb 12, 2019
2 parents 39b853a + c0cd341 commit 387fa55
Show file tree
Hide file tree
Showing 41 changed files with 1,290 additions and 1,234 deletions.
53 changes: 23 additions & 30 deletions app/Statistics.php
Expand Up @@ -404,11 +404,10 @@ public function totalIndisWithSources(): string
* @inheritDoc
*/
public function chartIndisWithSources(
string $size = null,
string $color_from = null,
string $color_to = null
): string {
return $this->individualRepository->chartIndisWithSources($size, $color_from, $color_to);
return $this->individualRepository->chartIndisWithSources($color_from, $color_to);
}

/**
Expand Down Expand Up @@ -447,11 +446,10 @@ public function totalFamsWithSources(): string
* @inheritDoc
*/
public function chartFamsWithSources(
string $size = null,
string $color_from = null,
string $color_to = null
): string {
return $this->individualRepository->chartFamsWithSources($size, $color_from, $color_to);
return $this->individualRepository->chartFamsWithSources($color_from, $color_to);
}

/**
Expand Down Expand Up @@ -650,12 +648,11 @@ public function totalSexUnknownPercentage(): string
* @inheritDoc
*/
public function chartSex(
string $size = null,
string $color_female = null,
string $color_male = null,
string $color_unknown = null
): string {
return $this->individualRepository->chartSex($size, $color_female, $color_male, $color_unknown);
return $this->individualRepository->chartSex($color_female, $color_male, $color_unknown);
}

/**
Expand Down Expand Up @@ -693,9 +690,9 @@ public function totalDeceasedPercentage(): string
/**
* @inheritDoc
*/
public function chartMortality(string $size = null, string $color_living = null, string $color_dead = null): string
public function chartMortality(string $color_living = null, string $color_dead = null): string
{
return $this->individualRepository->chartMortality($size, $color_living, $color_dead);
return $this->individualRepository->chartMortality($color_living, $color_dead);
}

/**
Expand Down Expand Up @@ -861,9 +858,9 @@ public function totalMediaUnknown(): string
/**
* @inheritDoc
*/
public function chartMedia(string $size = null, string $color_from = null, string $color_to = null): string
public function chartMedia(string $color_from = null, string $color_to = null): string
{
return $this->mediaRepository->chartMedia($size, $color_from, $color_to);
return $this->mediaRepository->chartMedia($color_from, $color_to);
}

/**
Expand Down Expand Up @@ -1000,9 +997,9 @@ public function statsBirthQuery(bool $sex = false, int $year1 = -1, int $year2 =
/**
* @inheritDoc
*/
public function statsBirth(string $size = null, string $color_from = null, string $color_to = null): string
public function statsBirth(string $color_from = null, string $color_to = null): string
{
return $this->individualRepository->statsBirth($size, $color_from, $color_to);
return $this->individualRepository->statsBirth($color_from, $color_to);
}

/**
Expand Down Expand Up @@ -1080,9 +1077,9 @@ public function statsDeathQuery(bool $sex = false, int $year1 = -1, int $year2 =
/**
* @inheritDoc
*/
public function statsDeath(string $size = null, string $color_from = null, string $color_to = null): string
public function statsDeath(string $color_from = null, string $color_to = null): string
{
return $this->individualRepository->statsDeath($size, $color_from, $color_to);
return $this->individualRepository->statsDeath($color_from, $color_to);
}

/**
Expand All @@ -1096,9 +1093,9 @@ public function statsAgeQuery(string $related = 'BIRT', string $sex = 'BOTH', in
/**
* @inheritDoc
*/
public function statsAge(string $size = '230x250'): string
public function statsAge(): string
{
return $this->individualRepository->statsAge($size);
return $this->individualRepository->statsAge();
}

/**
Expand Down Expand Up @@ -1448,9 +1445,9 @@ public function statsMarrQuery(bool $first = false, int $year1 = -1, int $year2
/**
* @inheritDoc
*/
public function statsMarr(string $size = null, string $color_from = null, string $color_to = null): string
public function statsMarr(string $color_from = null, string $color_to = null): string
{
return $this->familyRepository->statsMarr($size, $color_from, $color_to);
return $this->familyRepository->statsMarr($color_from, $color_to);
}

/**
Expand Down Expand Up @@ -1520,9 +1517,9 @@ public function lastDivorcePlace(): string
/**
* @inheritDoc
*/
public function statsDiv(string $size = null, string $color_from = null, string $color_to = null): string
public function statsDiv(string $color_from = null, string $color_to = null): string
{
return $this->familyRepository->statsDiv($size, $color_from, $color_to);
return $this->familyRepository->statsDiv($color_from, $color_to);
}

/**
Expand Down Expand Up @@ -1632,9 +1629,9 @@ public function statsMarrAgeQuery(string $sex = 'M', int $year1 = -1, int $year2
/**
* @inheritDoc
*/
public function statsMarrAge(string $size = '200x250'): string
public function statsMarrAge(): string
{
return $this->familyRepository->statsMarrAge($size);
return $this->familyRepository->statsMarrAge();
}

/**
Expand Down Expand Up @@ -1897,12 +1894,11 @@ public function topTenLargestFamilyList(string $total = '10'): string
* @inheritDoc
*/
public function chartLargestFamilies(
string $size = null,
string $color_from = null,
string $color_to = null,
string $total = '10'
): string {
return $this->familyRepository->chartLargestFamilies($size, $color_from, $color_to, (int) $total);
return $this->familyRepository->chartLargestFamilies($color_from, $color_to, (int) $total);
}

/**
Expand Down Expand Up @@ -1989,11 +1985,10 @@ public function noChildrenFamiliesList(string $type = 'list'): string
* @inheritDoc
*/
public function chartNoChildrenFamilies(
string $size = '220x200',
string $year1 = '-1',
string $year2 = '-1'
): string {
return $this->familyRepository->chartNoChildrenFamilies($size, (int) $year1, (int) $year2);
return $this->familyRepository->chartNoChildrenFamilies((int) $year1, (int) $year2);
}

/**
Expand Down Expand Up @@ -2069,13 +2064,12 @@ public function commonSurnamesListTotals(
* @inheritDoc
*/
public function chartCommonSurnames(
string $size = null,
string $color_from = null,
string $color_to = null,
string $number_of_surnames = '10'
): string {
return $this->individualRepository
->chartCommonSurnames($size, $color_from, $color_to, (int) $number_of_surnames);
->chartCommonSurnames($color_from, $color_to, (int) $number_of_surnames);
}

/**
Expand Down Expand Up @@ -2242,12 +2236,11 @@ public function commonGivenUnknownTable(string $threshold = '1', string $maxtosh
* @inheritDoc
*/
public function chartCommonGiven(
string $size = null,
string $color_from = null,
string $color_to = null,
string $maxtoshow = '7'
): string {
return $this->individualRepository->chartCommonGiven($size, $color_from, $color_to, (int) $maxtoshow);
return $this->individualRepository->chartCommonGiven($color_from, $color_to, (int) $maxtoshow);
}

/**
Expand Down
101 changes: 64 additions & 37 deletions app/Statistics/AbstractGoogle.php
Expand Up @@ -17,75 +17,102 @@

namespace Fisharebest\Webtrees\Statistics;

use Fisharebest\Webtrees\Module\ModuleThemeInterface;
use Fisharebest\Webtrees\Statistics\Helper\Sql;
use Fisharebest\Webtrees\Tree;

/**
* Base class for all google charts.
*
* @deprecated The pie chart API is outdated and should be replaced
* by the newer version https://developers.google.com/chart/ or
* an open source one like chart.js
*
* @see https://developers.google.com/chart/image/docs/gallery/pie_charts
*/
abstract class AbstractGoogle
{
// Used in Google charts
public const GOOGLE_CHART_ENCODING = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
/**
* @var Tree
*/
protected $tree;

/**
* Convert numbers to Google's custom encoding.
* @var ModuleThemeInterface
*/
protected $theme;

/**
* Constructor.
*
* @link http://bendodson.com/news/google-extended-encoding-made-easy
* @param Tree $tree
*/
public function __construct(Tree $tree)
{
$this->tree = $tree;
$this->theme = app()->make(ModuleThemeInterface::class);
}

/**
* Run an SQL query and cache the result.
*
* @param int[] $a
* @param string $sql
*
* @return string
* @return \stdClass[]
*/
protected function arrayToExtendedEncoding(array $a): string
protected function runSql(string $sql): array
{
$xencoding = self::GOOGLE_CHART_ENCODING;
$encoding = '';
return Sql::runSql($sql);
}

foreach ($a as $value) {
if ($value < 0) {
$value = 0;
}
/**
* Interpolates the number of color steps between a given start and end color.
*
* @param string $startColor The start color
* @param string $endColor The end color
* @param int $steps The number of steps to interpolate
*
* @return array
*/
public function interpolateRgb(string $startColor, string $endColor, int $steps): array
{
$s = $this->hexToRgb($startColor);
$e = $this->hexToRgb($endColor);
$colors = [];
$factorR = ($e[0] - $s[0]) / $steps;
$factorG = ($e[1] - $s[1]) / $steps;
$factorB = ($e[2] - $s[2]) / $steps;

$first = intdiv($value, 64);
$second = $value % 64;
$encoding .= $xencoding[$first] . $xencoding[$second];
for ($x = 1; $x < $steps; ++$x) {
$colors[] = $this->rgbToHex(
(int) round($s[0] + ($factorR * $x)),
(int) round($s[1] + ($factorG * $x)),
(int) round($s[2] + ($factorB * $x))
);
}

return $encoding;
$colors[] = $this->rgbToHex($e[0], $e[1], $e[2]);

return $colors;
}

/**
* Returns the three-dimensional pie chart url.
* Converts the color values to the HTML hex representation.
*
* @param string $data
* @param string $size
* @param array $colors
* @param string $labels
* @param int $r The red color value
* @param int $g The green color value
* @param int $b The blue color value
*
* @return string
*/
protected function getPieChartUrl(string $data, string $size, array $colors, string $labels): string
private function rgbToHex(int $r, int $g, int $b): string
{
return 'https://chart.googleapis.com/chart?cht=p3&chd=e:' . $data
. '&chs=' . $size . '&chco=' . implode(',', $colors) . '&chf=bg,s,ffffff00&chl='
. $labels;
return sprintf('#%02x%02x%02x', $r, $g, $b);
}

/**
* Run an SQL query and cache the result.
* Converts the HTML color hex representation to an array of color values.
*
* @param string $sql
* @param string $hex The HTML hex color code
*
* @return \stdClass[]
* @return array
*/
protected function runSql(string $sql): array
private function hexToRgb(string $hex): array
{
return Sql::runSql($sql);
return array_map('hexdec', str_split(ltrim($hex, '#'), 2));
}
}

0 comments on commit 387fa55

Please sign in to comment.