芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.sksb.smartcon-survey.com/app/Livewire/SplitLineChart.php
topicId); $sh = new SurveyJSHelper; $sh->setSurveyJson($topic->surveyJson); $questions = $sh->getQuestions(); $baseFilter = [ ['topic_id', $this->topicId], ['is_test', 0], ]; if ($this->question != 'all') { $questions = array_values(array_filter($questions, fn ($item) => $item['name'] == $this->question)); } $this->data[] = [ 'label' => 'Total', 'data' => $this->getValues($questions, $baseFilter), ]; // add total without split // $data[] = $this->getValue($interviews); if ($this->currentTab == 'department') { $labels = Department::all(['id', 'name'])->toArray(); foreach ($labels as $k => $dep) { $id = $dep['id']; $data = $this->getValues($questions, array_merge($baseFilter, [['department_id', $id]])); $this->data[] = [ 'label' => $dep['name'], 'data' => array_values($data), ]; } } elseif ($this->currentTab == 'age') { $labels = AgeFilter::getFilters(); foreach ($labels as $ageGroup) { $min = $ageGroup['min']; $max = $ageGroup['max']; $data = $this->getValues($questions, array_merge($baseFilter, [ ['age', '>=', $min], ['age', '<=', $max], ])); $this->data[] = [ 'label' => $ageGroup['name'], 'data' => array_values($data), ]; } } else { $labels = PositionFilter::getFilters(); foreach ($labels as $position) { $id = $position['id']; $data = $this->getValues($questions, array_merge($baseFilter, [['leading_pos', $id]])); $this->data[] = [ 'label' => $position['name'], 'data' => array_values($data), ]; } } // Add total to labels: $labels = array_merge([['name' => 'Total']], [...$labels]); // $this->labels = array_map(fn ($item) => $item['name'], $labels); // get the labels $this->labels = array_keys($this->data[0]['data']); $this->data[0]['data'] = array_values($this->data[0]['data']); foreach ($this->labels as $key => $value) { [$quarter, $year] = explode(' ', $value); $quarter = (int) str_replace('Q', '', $quarter); $base = Interview::where($baseFilter)->whereRaw("YEAR(created_at) = $year AND QUARTER(created_at) = $quarter")->count(); $this->labels[$key] .= " (n=$base)"; } // turn data into chartjs format $this->data = collect($this->data)->map(function ($set) { $set['data'] = collect($set['data'])->map(fn ($item) => $item['value'])->toArray(); return $set; })->toArray(); return view('livewire.split-line-chart'); } public function getValues(array $questions, array $filter = []): array { $results = []; foreach ($questions as $question) { $top2 = Interview::where($filter) ->whereQuestion($question['name'], range(1, 2)) ->resultsByQuarter()->get()->toArray(); $total = Interview::where($filter) ->whereQuestion($question['name'], range(1, 5)) ->resultsByQuarter()->get()->toArray(); $results[] = array_map(function ($item, $total) { if ($item === null) { return [ 'label' => 'Q'.$total['quarter'].' '.$total['year'], 'count' => 0, 'total' => $total['count'] ?? 0, ]; } return [ 'label' => 'Q'.$item['quarter'].' '.$item['year'], 'count' => $item['count'], 'total' => $total['count'], ]; }, $top2, $total); } $indexResults = []; // merge all question into index foreach ($questions as $key => $question) { foreach ($results[$key] as $value) { if (isset($indexResults[$value['label']])) { $indexResults[$value['label']]['count'] += $value['count']; $indexResults[$value['label']]['total'] += $value['total']; } else { $indexResults[$value['label']] = [ 'label' => $value['label'], 'count' => $value['count'], 'total' => $value['total'], ]; } } } $indexResults = array_map(function ($item) { $item['value'] = round($item['count'] / $item['total'] * 100, 1); return $item; }, $indexResults); return $indexResults; } }