海南旅游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.

177 lines
3.3 KiB

4 years ago
  1. <?php
  2. namespace App\Admin\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. ])
  53. ->orderByDesc('count_id')
  54. ->groupBy('agent_product_id')
  55. ->limit(5)
  56. ->get();
  57. //$warehouse = [];
  58. $data = $categories = [];
  59. foreach ($warehouse as $v) {
  60. array_push($this->labels, $v->title);
  61. array_push($data, $v->count_id);
  62. $categories[] = [$v->title];
  63. }
  64. $this->withChart($data, $categories);
  65. //$this->chartLabels($this->labels);
  66. $count = Order::query()
  67. ->where([
  68. 'status' => OrderStatus::SUCCESS,
  69. ])
  70. ->count();
  71. //$count = 0;
  72. if ($count > 0) {
  73. array_unshift($data, $count);
  74. array_unshift($this->labels, '总销售量');
  75. $color = Admin::color();
  76. array_unshift($this->colors, $color->primary());
  77. }
  78. $this->withContent($data);
  79. }
  80. /**
  81. * 设置图表数据.
  82. *
  83. * @param array $data
  84. *
  85. * @return $this
  86. */
  87. public function withChart($data, $categories)
  88. {
  89. return $this->chart([
  90. 'series' => [[
  91. 'name' => '销量',
  92. 'data' => $data
  93. ]],
  94. 'chart' => [
  95. //'width' => '180%',
  96. 'type' => 'bar',
  97. 'events' => [
  98. ],
  99. 'toolbar' => ['show' => false],
  100. ],
  101. 'colors' => $this->colors,
  102. 'plotOptions' => [
  103. 'bar' => [
  104. //'columnWidth' => '45%',
  105. 'distributed' => true,
  106. ]
  107. ],
  108. 'dataLabels' => [
  109. 'enabled' => false
  110. ],
  111. 'legend' => [
  112. 'show' => false
  113. ],
  114. 'xaxis' => [
  115. 'categories' =>
  116. $categories
  117. ,
  118. 'labels' => [
  119. 'show' => false,
  120. 'style' => [
  121. 'colors' => $this->colors,
  122. 'fontSize' => '12px'
  123. ]
  124. ],
  125. ],
  126. 'yaxis' => [
  127. 'show' => false
  128. ],
  129. 'tooltip' => [
  130. 'x' => ['show' => true],
  131. ],
  132. ]);
  133. }
  134. /**
  135. * 设置卡片头部内容.
  136. *
  137. * @param mixed $desktop
  138. * @param mixed $mobile
  139. *
  140. * @return $this
  141. */
  142. protected function withContent($data)
  143. {
  144. $content = '';
  145. foreach ($data as $k => $v) {
  146. $content .= '
  147. <div class="d-flex pl-1 mt-2">
  148. <div style="width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
  149. <i class="fa fa-circle" style="color:' . $this->colors[$k] . '"></i> ' . $this->labels[$k] . '
  150. <div style="display:inline-block">' . $v . '</div>
  151. </div>
  152. </div>
  153. ';
  154. }
  155. return $this->content(
  156. <<<HTML
  157. $content
  158. HTML
  159. );
  160. }
  161. }