Skip to content

Commit

Permalink
Merge pull request #100 from digio-ch/issue/hc-235
Browse files Browse the repository at this point in the history
various improvements
  • Loading branch information
SebastianStorz authored Oct 6, 2023
2 parents 8d96886 + 7c507b8 commit 92a93e8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
34 changes: 33 additions & 1 deletion src/DTO/Mapper/CensusMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static function mapToLineChart(StatisticGroup $statisticGroup, array $cen
public static function filterCensusGroup(CensusGroup $group, CensusRequestData $censusRequestData)
{
if (self::isFiltered('biber', $censusRequestData->getRoles()) || !$censusRequestData->isFilterMales()) {
$group->setPtaMCount(0);
$group->setBiberMCount(0);
}
if (self::isFiltered('biber', $censusRequestData->getRoles()) || !$censusRequestData->isFilterFemales()) {
$group->setBiberFCount(0);
Expand Down Expand Up @@ -174,4 +174,36 @@ public static function getColorForId($id): string
{
return '#' . substr(md5($id), 0, 6);
}

/**
* Retuns a hex color string where each color (R,G,B) is withing 100-230, so that text is always readable on this color.
* @param int $id
* @return string
*/
public static function getLightColorForId(int $id): string
{
$color = self::getColorForId($id);
$r = hexdec(substr($color, 1, 2));
$g = hexdec(substr($color, 3, 2));
$b = hexdec(substr($color, 5, 2));
if ($r < 100) {
$r += 100;
}
if ($r > 230) {
$r -= 25;
}
if ($g < 100) {
$g += 100;
}
if ($g > 230) {
$g -= 25;
}
if ($b < 100) {
$b += 100;
}
if ($g > 230) {
$g -= 25;
}
return "#" . dechex($r) . dechex($g) . dechex($b);
}
}
35 changes: 21 additions & 14 deletions src/Service/DataProvider/CensusDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,33 +152,38 @@ public function findHighestRelevantRegion(StatisticGroup $group)
if ($parentGroup->getGroupType()->getGroupType() === GroupType::REGION) {
return $this->findHighestRelevantRegion($parentGroup);
} else {
if ($group->getGroupType()->getGroupType() === GroupType::DEPARTMENT && $parentGroup->getGroupType()->getGroupType() === GroupType::CANTON) {
return $this->findHighestRelevantRegion($parentGroup);
}
return $group;
}
}


/**
* @param TableDTO[] $dtos
* @return void
* @param StatisticGroup[] $groups
* @return StatisticGroup[]
*/
public function sortDTOs(array $dtos)
public function sortGroups(array $groups)
{
$regions = array_filter($dtos, function ($dto) {
return $dto->getType() === GroupType::REGION;
$regions = array_filter($groups, function ($group) {
return $group->getGroupType()->getGroupType() === GroupType::REGION;
});
$departments = array_filter($dtos, function ($dto) {
return $dto->getType() === GroupType::DEPARTMENT;
$departments = array_filter($groups, function ($group) {
return $group->getGroupType()->getGroupType() === GroupType::DEPARTMENT;
});
usort($regions, function (TableDTO $a, TableDTO $b) {
usort($regions, function (StatisticGroup $a, StatisticGroup $b) {
return strcmp($a->getName(), $b->getName());
});
usort($departments, function (TableDTO $a, TableDTO $b) {
usort($departments, function (StatisticGroup $a, StatisticGroup $b) {
return strcmp($a->getName(), $b->getName());
});

$return = [];
foreach ($regions as $region) {
$return[] = $region;
foreach ($departments as $department) {
if ($department->getParentId() === $region->getId()) {
if ($department->getParentGroup()->getId() === $region->getId()) {
$return[] = $department;
}
}
Expand All @@ -204,22 +209,23 @@ public function getRelevantGroups(Group $group)
public function getTableData(Group $group, CensusRequestData $censusRequestData)
{
$flattenedGroups = $this->getRelevantGroups($group);

$flattenedGroups = $this->sortGroups($flattenedGroups);
$dataTransferObjects = [];
$relevantYears = range(date('Y') - 5, date('Y'));
foreach ($flattenedGroups as $flattenedGroup) {
$dataTransferObjects[] = CensusMapper::mapToCensusTable($flattenedGroup, $this->censusGroupRepository->findBy(['group_id' => $flattenedGroup->getId()]), $relevantYears, $censusRequestData);
}
return [
'years' => $relevantYears,
'data' => $this->sortDTOs($dataTransferObjects),
'data' => $dataTransferObjects,
];
}

public function getDevelopmentData(Group $group, CensusRequestData $censusRequestData)
{
$relevantGroups = $this->getRelevantGroups($group);
$relevantGroups = $this->filterGroups($relevantGroups, $censusRequestData);
$relevantGroups = $this->sortGroups($relevantGroups);

$absolute = [];
$relative = [];
Expand All @@ -244,6 +250,7 @@ public function getMembersData(Group $group, CensusRequestData $censusRequestDat
{
$relevantGroups = $this->getRelevantGroups($group);
$relevantGroups = $this->filterGroups($relevantGroups, $censusRequestData);
$relevantGroups = $this->sortGroups($relevantGroups);

$rawResults = [];
foreach ($relevantGroups as $relevantGroup) {
Expand All @@ -263,7 +270,7 @@ public function getMembersData(Group $group, CensusRequestData $censusRequestDat
$rawResults[4][] = new StackedBarElementDTO($rover, $data[0]->getName(), '#1DA650');
$rawResults[3][] = new StackedBarElementDTO($pio, $data[0]->getName(), '#DD1F19');
$rawResults[5][] = new StackedBarElementDTO($pta, $data[0]->getName(), '#d9b826');
$rawResults[6][] = new StackedBarElementDTO($leaders, $data[0]->getName(), '#929292');
$rawResults[6][] = new StackedBarElementDTO($leaders, $data[0]->getName(), '#005716');
}
}
$return = [];
Expand All @@ -290,7 +297,7 @@ public function getTreemapData(Group $group, CensusRequestData $censusRequestDat
$parentName = $relevantGroup->getParentGroup()->getName();
$dto->setRegion($parentName);
$dto->setValue($data[0]->getCalculatedTotal());
$dto->setColor(CensusMapper::getColorForId($relevantGroup->getId()));
$dto->setColor(CensusMapper::getLightColorForId($relevantGroup->getParentGroup()->getId()));
$return[] = $dto;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Service/DataProvider/WidgetDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class WidgetDataProvider
'Group::Pio' => '#DD1F19',
'Group::AbteilungsRover' => '#1DA650',
'Group::Pta' => '#d9b826',
'Group::Abteilung' => '#929292',
'leaders' => '#929292'
'Group::Abteilung' => '#005716',
'leaders' => '#005716'
];

/** @var string[] */
Expand Down

0 comments on commit 92a93e8

Please sign in to comment.