WeakKnowledgePointsChart.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Livewire\MistakeBook;
  3. use App\Services\MistakeBookService;
  4. use Filament\Widgets\ChartWidget;
  5. class WeakKnowledgePointsChart extends ChartWidget
  6. {
  7. public ?string $studentId;
  8. protected static ?int $sort = 3;
  9. public function mount(?string $studentId = null): void
  10. {
  11. $this->studentId = $studentId;
  12. }
  13. protected function getData(): array
  14. {
  15. if (!$this->studentId) {
  16. return [
  17. 'datasets' => [],
  18. 'labels' => [],
  19. ];
  20. }
  21. $service = app(MistakeBookService::class);
  22. $patterns = $service->getMistakePatterns($this->studentId);
  23. $knowledgePoints = array_slice($patterns['knowledge_points'] ?? [], 0, 10, true);
  24. $labels = array_keys($knowledgePoints);
  25. $data = array_values($knowledgePoints);
  26. return [
  27. 'datasets' => [
  28. [
  29. 'label' => '错误次数',
  30. 'data' => $data,
  31. 'backgroundColor' => 'rgba(239, 68, 68, 0.5)',
  32. 'borderColor' => 'rgb(239, 68, 68)',
  33. 'borderWidth' => 2,
  34. ],
  35. ],
  36. 'labels' => $labels,
  37. ];
  38. }
  39. protected function getType(): string
  40. {
  41. return 'bar';
  42. }
  43. protected function getOptions(): array
  44. {
  45. return [
  46. 'responsive' => true,
  47. 'maintainAspectRatio' => false,
  48. 'plugins' => [
  49. 'legend' => [
  50. 'display' => false,
  51. ],
  52. ],
  53. 'scales' => [
  54. 'y' => [
  55. 'beginAtZero' => true,
  56. 'ticks' => [
  57. 'stepSize' => 1,
  58. ],
  59. ],
  60. ],
  61. ];
  62. }
  63. }