链街Dcat后台
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.

185 lines
5.1 KiB

  1. <?php
  2. namespace App\Admin\Widgets\Charts;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Widgets\ApexCharts\Chart;
  5. use Illuminate\Http\Request;
  6. class OrderGoodsActivityColumnChart extends Chart
  7. {
  8. protected $categories = [
  9. '商品1', '商品2', '商品3', '商品4', '商品5', '商品6', '商品7', '商品8', '商品9', '商品10'
  10. // ,'商品11', '商品12', '商品13', '商品14', '商品15', '商品16', '商品17', '商品18', '商品19', '商品20'
  11. ];
  12. protected $data = [];
  13. protected $id;
  14. protected $option;
  15. public function __construct($data = [], $containerSelector = null, $options = [])
  16. {
  17. $this->option = $this->id = 7;
  18. $this->data = $data;
  19. parent::__construct($containerSelector, $options);
  20. $this->setUpOptions();
  21. }
  22. /**
  23. * 初始化图表配置
  24. */
  25. protected function setUpOptions()
  26. {
  27. $color = Admin::color();
  28. $this->options([
  29. 'chart' => [
  30. 'type' => 'bar',
  31. 'height'=> 350,
  32. ],
  33. 'colors' => [
  34. '#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e', '#f48024', '#69d2e7'
  35. ,'#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e', '#f48024', '#69d2e7'
  36. ],
  37. 'plotOptions'=> [
  38. 'bar'=> [
  39. 'distributed' =>false, // 柱状图颜色
  40. 'horizontal' => false, // 竖直
  41. 'columnWidth' => '50%', // 柱状宽
  42. 'dataLabels' => [
  43. 'position' => 'bottom'
  44. ]
  45. ]
  46. ],
  47. 'dataLabels' => [
  48. 'enabled'=> true,
  49. ],
  50. 'xaxis' => [
  51. 'categories' => $this->categories,
  52. ],
  53. 'yaxis' => [
  54. 'axisBorder' => [
  55. 'show' => true,
  56. 'color' => $color->primary()
  57. ],
  58. 'labels' => [
  59. 'show' => true,
  60. 'style' => [
  61. 'colors' => $color->primary()
  62. ]
  63. ],
  64. 'title' => [
  65. 'text' => "销量",
  66. 'align' => 'center',
  67. 'style' => [
  68. 'color' => $color->primary()
  69. ]
  70. ]
  71. ]
  72. ]);
  73. $number = 10;
  74. // $this->chartOption(
  75. // 'dataLabels.formatter',
  76. // // 这个值最后段代码会作为JS代码执行
  77. // JavaScript::make("function () { return {$number}; }")
  78. // );
  79. }
  80. /**
  81. * 处理请求
  82. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  83. *
  84. * @param Request $request
  85. * @return mixed|void
  86. */
  87. public function handle(Request $request)
  88. {
  89. switch ((int) $request->get('option')) {
  90. case 30:
  91. // 你的数据查询逻辑
  92. $data = [
  93. [
  94. 'name' => '活动商品',
  95. 'data' => [54, 45, 41, 64, 22, 46, 52, 10, 9, 11, 54, 45, 41, 64, 22, 46, 52, 10, 9, 11]
  96. ]
  97. ];
  98. break;
  99. case 28:
  100. // 你的数据查询逻辑
  101. $data = [
  102. [
  103. 'name' => '活动商品',
  104. 'data' => [44, 55, 41, 64, 22, 56, 52, 12, 52, 12, 44, 55, 41, 64, 22, 56, 52, 12, 52, 12]
  105. ]
  106. ];
  107. break;
  108. case 7:
  109. default:
  110. // 你的数据查询逻辑
  111. $data = [
  112. [
  113. 'name' => '活动商品',
  114. 'data' => [54, 45, 41, 64, 22, 46, 52, 10, 9, 11]
  115. ]
  116. ];
  117. break;
  118. }
  119. $this->withData($data);
  120. }
  121. /**
  122. * 处理图表数据
  123. */
  124. protected function buildData()
  125. {
  126. // 执行你的数据查询逻辑
  127. $data = [
  128. [
  129. 'name' => '活动商品',
  130. 'data' => [44, 55, 41, 64, 22, 56, 52, 12, 52, 12]
  131. ]
  132. ];
  133. $categories = $this->categories;
  134. $this->withData($data);
  135. $this->withCategories($categories);
  136. }
  137. /**
  138. * 这里返回需要异步传递到 handler 方法的参数
  139. *
  140. * @return array
  141. */
  142. public function parameters(): array
  143. {
  144. return [
  145. 'id' => $this->id,
  146. 'option' => $this->option,
  147. ];
  148. }
  149. /**
  150. * 设置图表数据
  151. *
  152. * @param array $data
  153. *
  154. * @return $this
  155. */
  156. public function withData(array $data)
  157. {
  158. return $this->option('series', $data);
  159. }
  160. /**
  161. * 渲染图表
  162. *
  163. * @return string
  164. */
  165. public function render()
  166. {
  167. $this->buildData();
  168. return parent::render();
  169. }
  170. }