jemoh

Laravel Excel Issue with Charts in Multiple Sheets

Aug 30th, 2022 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. //Parent class
  2. class MultipleUsersExport implements WithMultipleSheets
  3. {
  4. use Exportable;
  5.  
  6. public function sheets(): array
  7. {
  8. $sheets = [];
  9.  
  10. $query = DB::table('users')
  11. ->select('id', 'name')
  12. ->orderBy('updated_at')
  13. ->get();
  14.  
  15. foreach($query as $user) {
  16. $sheets[] = new ActiveUsersExport($user->name, $user->id);
  17. }
  18.  
  19. return $sheets;
  20. }
  21.  
  22. public function registerEvents(): array
  23. {
  24. return [
  25. BeforeWriting::class => function(BeforeWriting $event) {
  26. $event->writer->setActiveSheetIndexByName('3');
  27. },
  28. ];
  29. }
  30.  
  31. }
  32.  
  33. //Child class
  34. class ActiveUsersExport implements`
  35. ` FromQuery, ShouldAutoSize, WithMapping, WithHeadings, WithEvents, WithCharts, WithTitle
  36. {
  37.  
  38. ` use Exportable;`
  39.  
  40. public $name;
  41.  
  42. public $id;
  43.  
  44. public function __construct($name, $id) {
  45.  
  46. $this->name = $name;
  47.  
  48. $this->id = $id;
  49.  
  50. }
  51.  
  52. public function query()
  53. {
  54.  
  55. $query = DB::table('users')
  56. ->select('users.*')
  57. ->orderBy('updated_at');
  58.  
  59. return $query;
  60.  
  61. }
  62.  
  63. public function headings(): array
  64. {
  65.  
  66. return [
  67.  
  68. 'ID',
  69. 'Name',
  70. 'Email',
  71.  
  72. ];
  73.  
  74. }
  75.  
  76. public function charts()
  77. {
  78. $label = [new DataSeriesValues('String', $this->id.'!$B$1', null, 1), ];
  79. $categories = [new DataSeriesValues('String', $this->id.'!$B$2:$B$8', null, 2)];
  80. $values = [new DataSeriesValues('Number', $this->id.'!$A$2:$A8', null, 2)];
  81.  
  82. $series = new DataSeries(DataSeries::TYPE_BARCHART_3D, NULL,
  83. range(0, \count($values) - 1), $label, $categories, $values);
  84. $plot = new PlotArea(NULL, [$series]);
  85.  
  86. $legend = new Legend(Legend::POSITION_RIGHT, NULL, false);
  87. $chart = new Chart('Overview X', new Title('Overview'), $legend, $plot,true,'gap',NULL,NULL);
  88. $chart->setTopLeftPosition('E1');
  89. $chart->setBottomRightPosition('K20');
  90.  
  91. return $chart;
  92. }
  93.  
  94. public function registerEvents(): array {
  95.  
  96. return [
  97.  
  98. AfterSheet::class => function(AfterSheet $event) {
  99.  
  100. $chart = $this->charts();
  101. $event->sheet->getDelegate()->addChart($chart);
  102.  
  103. }
  104.  
  105. ];
  106.  
  107. }
  108.  
  109. public function map($user): array
  110. {
  111.  
  112. return [
  113.  
  114. [$user->id,
  115. $user->name,
  116. $user->email,],
  117.  
  118. ];
  119.  
  120. }
  121.  
  122. public function title(): string
  123. {
  124.  
  125. return $this->id;
  126.  
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment