Skip to content

Commit

Permalink
style(AnswersChartData.php): improve code readability by using backsl…
Browse files Browse the repository at this point in the history
…ashes for global functions like in_array and is_array

refactor(AnswersChartData.php): simplify condition checks by using strict comparison operators and rearrange code for better clarity
perf(AnswersChartData.php): optimize code by reducing unnecessary function calls and improving variable naming for better understanding
  • Loading branch information
marco76tv committed Aug 20, 2024
1 parent feadcec commit 48cb6a4
Showing 1 changed file with 26 additions and 39 deletions.
65 changes: 26 additions & 39 deletions Datas/AnswersChartData.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getChartJsData(): array
// dddx($this->answers->toCollection());
// }

if (in_array($this->chart->type, ['pieAvg', 'pie1'], false)) {
if (\in_array($this->chart->type, ['pieAvg', 'pie1'], false)) {
$data = $this->answers->toCollection()->pluck('avg')->all();

if (isset($this->chart->max)) {
Expand All @@ -76,14 +76,14 @@ public function getChartJsData(): array
if ($other > 0.01) {
$data[] = $other;
$labels[] = $this->chart->answer_value_no_txt ?? 'answer_value_no_txt';
if (\count($labels) === 2 && \strlen($labels[0]) < 3) {
if (2 === \count($labels) && \strlen($labels[0]) < 3) {
$labels[0] = $this->chart->answer_value_txt;
}
}
}
}

if (isset($data[0]) && is_array($data[0])) { // questionario multiplo
if (isset($data[0]) && \is_array($data[0])) { // questionario multiplo
// dddx([$this->chart, $this->answers]);
$legends = array_keys($data[0]);
foreach ($legends as $key => $legend) {
Expand All @@ -108,13 +108,13 @@ public function getChartJsData(): array
$data[] = $other;
$labels[] = $this->chart->answer_value_no_txt ?? 'answer_value_no_txt';
Assert::notNull($labels[0], '['.__FILE__.']['.__LINE__.']');
if (\count($labels) === 2 && \strlen($labels[0]) < 3) {
if (2 === \count($labels) && \strlen($labels[0]) < 3) {
$labels[0] = $this->chart->answer_value_txt;
}
}
}

if (isset($this->answers->toCollection()->pluck('avg')[0]) && ! is_string($this->answers->toCollection()->pluck('avg')[0])) {
if (isset($this->answers->toCollection()->pluck('avg')[0]) && ! \is_string($this->answers->toCollection()->pluck('avg')[0])) {
$label = 'Media';
} else {
$label = 'Percentuale';
Expand Down Expand Up @@ -148,7 +148,7 @@ public function getChartJsOptionsArray(): array
{
$title = [];

if ($this->title !== 'no_set') {
if ('no_set' !== $this->title) {
$title = [
'display' => true,
'text' => $this->title,
Expand All @@ -158,7 +158,7 @@ public function getChartJsOptionsArray(): array
];
}

if ($this->footer !== 'no_set') {
if ('no_set' !== $this->footer) {
$title = [
'display' => true,
'text' => $this->footer,
Expand All @@ -170,7 +170,7 @@ public function getChartJsOptionsArray(): array
'title' => $title,
];

if ($this->chart->type === 'horizbar1') {
if ('horizbar1' === $this->chart->type) {
$options['indexAxis'] = 'y';
}

Expand All @@ -186,18 +186,18 @@ public function getChartJsBarOptionsJs(string $js): string
$indexAxis = 'x';
$value = '';

if ($this->chart->max === 100.0) {
if (100.0 === $this->chart->max) {
$value = ' %';
}
if ($this->chart->type === 'horizbar1') {
if ('horizbar1' === $this->chart->type) {
$indexAxis = 'y';
$value = ' %';
}

$title = '{}';

$labels = '{}';
if (count($this->getChartJsData()['datasets']) == 1 && $this->chart->type !== 'horizbar1') {
if (1 === \count($this->getChartJsData()['datasets']) && 'horizbar1' !== $this->chart->type) {
$labels = "{
name: {
align: 'center',
Expand Down Expand Up @@ -225,7 +225,7 @@ public function getChartJsBarOptionsJs(string $js): string

$title = '{}';
// dddx($this);
if ($this->title !== 'no_set' && $this->chart->type === 'horizbar1') {
if ('no_set' !== $this->title && 'horizbar1' === $this->chart->type) {
$title = "{
display: true,
text: '".$this->title."',
Expand All @@ -235,15 +235,15 @@ public function getChartJsBarOptionsJs(string $js): string
}";
}

if ($this->footer !== 'no_set') {
if ('no_set' !== $this->footer) {
$title = "{
display: true,
text: '".$this->footer."',
position: 'bottom',
}";
}
$tooltip = '{}';
if ($this->chart->type === 'bar2' && count($this->getChartJsData()['datasets']) == 1) {
if ('bar2' === $this->chart->type && 1 === \count($this->getChartJsData()['datasets'])) {
$tooltip = "{
callbacks: {
label: function(context) {
Expand Down Expand Up @@ -386,7 +386,7 @@ public function getChartJsBarOptionsJs(string $js): string
public function getChartJsDoughnutOptionsJs(string $js): string
{
$title = '{}';
if ($this->title !== 'no_set') {
if ('no_set' !== $this->title) {
$title = "{
display: true,
text: '".$this->title."',
Expand All @@ -397,9 +397,13 @@ public function getChartJsDoughnutOptionsJs(string $js): string
}
$first_answer = $this->answers->first();
$label = '--';
<<<<<<< HEAD
if ($first_answer != null) {
=======
if (null !== $first_answer) {
>>>>>>> ff702a8 (up)
Assert::isInstanceOf($first_answer, AnswerData::class, '['.__LINE__.']['.__FILE__.']');
$label = round(floatval($this->answers->first()->avg), 2);
$label = round((float) $this->answers->first()->avg, 2);
}
$js = <<<JS
scales: {
Expand Down Expand Up @@ -477,7 +481,7 @@ public function getChartJsDoughnutOptionsArray(array $options): array
];
Assert::isInstanceOf($this->answers->first(), AnswerData::class, '['.__LINE__.']['.__FILE__.']');
$options['plugins']['doughnutLabel'] = [
'label' => round(floatval($this->answers->first()->avg), 2),
'label' => round((float) $this->answers->first()->avg, 2),
];

return $options;
Expand All @@ -490,36 +494,19 @@ public function getChartJsOptionsJs(): RawJs
$method = 'getChartJs'.Str::of($chartjs_type)->studly()->toString().'OptionsJs';
$js = $this->{$method}($js);

// dddx(
// RawJs::make('{
// '.$js.'
// }')
// );
// return '{'.$js.'}';

return RawJs::make('{
'.$js.'
}');
/*
return RawJs::make(<<<JS
{
scales: {
y: {
ticks: {
callback: (value) => '€' + value,
},
},
},
}
JS);
*/
}

// funzione deprecata, utilizzata nella dashboard precedente
public function getChartJsOptions(): array
{
$title = [];

if ($this->title !== 'no_set') {
if ('no_set' !== $this->title) {
$title = [
'display' => true,
'text' => $this->title,
Expand All @@ -529,7 +516,7 @@ public function getChartJsOptions(): array
];
}

if ($this->footer !== 'no_set') {
if ('no_set' !== $this->footer) {
$title = [
'display' => true,
'text' => $this->footer,
Expand All @@ -541,7 +528,7 @@ public function getChartJsOptions(): array
'title' => $title,
];

if ($this->chart->type === 'horizbar1') {
if ('horizbar1' === $this->chart->type) {
$options['indexAxis'] = 'y';
}

Expand All @@ -551,4 +538,4 @@ public function getChartJsOptions(): array

return $options;
}
}
}

0 comments on commit 48cb6a4

Please sign in to comment.