海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
3.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Metrics\Examples;
  3. use App\Common\OrderStatus;
  4. use App\Models\Order;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Widgets\Metrics\Bar;
  7. use Illuminate\Support\Facades\DB;
  8. class SalesExamples extends Bar
  9. {
  10. protected $labels = [];
  11. protected $colors;
  12. /**
  13. * 初始化卡片内容
  14. */
  15. protected function init()
  16. {
  17. parent::init();
  18. $color = Admin::color();
  19. $this->colors = [$color->green(), $color->red(), $color->yellow(), $color->orange1(), $color->dark90(), $color->dark70(), $color->custom()];
  20. $this->contentWidth(3, 9);
  21. $this->title('销售排行');
  22. //$this->chartPullRight = false;
  23. //$this->subTitle('Last 30 days');
  24. $this->height = 280;
  25. $this->chartHeight = 250;
  26. // 设置图表颜色
  27. $this->chartColors($this->colors);
  28. }
  29. /**
  30. * 渲染模板
  31. *
  32. * @return string
  33. */
  34. public function render()
  35. {
  36. $this->fill();
  37. return parent::render();
  38. }
  39. /**
  40. * 写入数据.
  41. *
  42. * @return void
  43. */
  44. public function fill()
  45. {
  46. $warehouse = Order::query()
  47. ->with('agentProduct')
  48. ->select('*')
  49. ->addSelect(DB::raw('count(id) as count_id'))
  50. ->where([
  51. 'status' => OrderStatus::SUCCESS,
  52. 'agent_id' => Admin::user()->id,
  53. ])
  54. ->orderByDesc('count_id')
  55. ->groupBy('agent_product_id')
  56. ->limit(5)
  57. ->get();
  58. //$warehouse = [];
  59. $data = $categories = [];
  60. foreach ($warehouse as $v) {
  61. array_push($this->labels, $v->title);
  62. array_push($data, $v->count_id);
  63. $categories[] = [$v->title];
  64. }
  65. $this->withChart($data, $categories);
  66. //$this->chartLabels($this->labels);
  67. $count = Order::query()
  68. ->where([
  69. 'status' => OrderStatus::SUCCESS,
  70. 'agent_id' => Admin::user()->id,
  71. ])
  72. ->count();
  73. //$count = 0;
  74. if ($count > 0) {
  75. array_unshift($data, $count);
  76. array_unshift($this->labels, '总销售量');
  77. $color = Admin::color();
  78. array_unshift($this->colors, $color->primary());
  79. }
  80. $this->withContent($data);
  81. }
  82. /**
  83. * 设置图表数据.
  84. *
  85. * @param array $data
  86. *
  87. * @return $this
  88. */
  89. public function withChart($data, $categories)
  90. {
  91. return $this->chart([
  92. 'series' => [[
  93. 'name' => '销量',
  94. 'data' => $data
  95. ]],
  96. 'chart' => [
  97. //'width' => '180%',
  98. 'type' => 'bar',
  99. 'events' => [
  100. ],
  101. 'toolbar' => ['show' => false],
  102. ],
  103. 'colors' => $this->colors,
  104. 'plotOptions' => [
  105. 'bar' => [
  106. //'columnWidth' => '45%',
  107. 'distributed' => true,
  108. ]
  109. ],
  110. 'dataLabels' => [
  111. 'enabled' => false
  112. ],
  113. 'legend' => [
  114. 'show' => false
  115. ],
  116. 'xaxis' => [
  117. 'categories' =>
  118. $categories
  119. ,
  120. 'labels' => [
  121. 'show' => false,
  122. 'style' => [
  123. 'colors' => $this->colors,
  124. 'fontSize' => '12px'
  125. ]
  126. ],
  127. ],
  128. 'yaxis' => [
  129. 'show' => false
  130. ],
  131. 'tooltip' => [
  132. 'x' => ['show' => true],
  133. ],
  134. ]);
  135. }
  136. /**
  137. * 设置卡片头部内容.
  138. *
  139. * @param mixed $desktop
  140. * @param mixed $mobile
  141. *
  142. * @return $this
  143. */
  144. protected function withContent($data)
  145. {
  146. $content = '';
  147. foreach ($data as $k => $v) {
  148. $content .= '
  149. <div class="d-flex pl-1 mt-2">
  150. <div style="width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
  151. <i class="fa fa-circle" style="color:' . $this->colors[$k] . '"></i> ' . $this->labels[$k] . '
  152. <div style="display:inline-block">' . $v . '</div>
  153. </div>
  154. </div>
  155. ';
  156. }
  157. return $this->content(
  158. <<<HTML
  159. $content
  160. HTML
  161. );
  162. }
  163. }