链街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.

193 lines
5.3 KiB

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