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

113 lines
2.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Admin\Extensions\Chart\Bar;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Widgets\ApexCharts\Chart;
  5. use Dcat\Admin\Widgets\Metrics\Bar;
  6. class OrderBar extends Chart
  7. {
  8. protected $_data;
  9. public function __construct($data=[])
  10. {
  11. parent::__construct($containerSelector=null, $options=[]);
  12. $this->_data = $data;
  13. $this->setUpOptions();
  14. }
  15. /**
  16. * 初始化图表配置
  17. */
  18. protected function setUpOptions()
  19. {
  20. $color = Admin::color();
  21. $colors = [$color->primary(), $color->primaryDarker()];
  22. $this->options([
  23. 'colors' => $colors,
  24. 'chart' => [
  25. 'type' => 'bar',
  26. 'height' => 430,
  27. ],
  28. 'plotOptions' => [
  29. 'bar' => [
  30. 'horizontal' => false,
  31. 'dataLabels' => [
  32. 'position' => 'top',
  33. ],
  34. ]
  35. ],
  36. 'dataLabels' => [
  37. 'enabled' => true,
  38. 'offsetX' => -6,
  39. 'style' => [
  40. 'fontSize' => '12px',
  41. 'colors' => ['#fff']
  42. ]
  43. ],
  44. 'stroke' => [
  45. 'show' => true,
  46. 'width' => 1,
  47. 'colors' => ['#fff']
  48. ],
  49. 'xaxis' => [
  50. 'categories' => [],
  51. ],
  52. ]);
  53. }
  54. /**
  55. * 处理图表数据
  56. */
  57. protected function buildData()
  58. {
  59. $orderType = request()->input('order_type')??'amount';
  60. // 执行你的数据查询逻辑
  61. $data = [
  62. [
  63. 'name'=>'订单金额',
  64. 'data' =>$orderType=='num'? $this->_data['num']:$this->_data['amount']
  65. ],
  66. ];
  67. $categories = $this->_data['time'];
  68. $this->withData($data);
  69. $this->withCategories($categories);
  70. }
  71. /**
  72. * 设置图表数据
  73. *
  74. * @param array $data
  75. *
  76. * @return $this
  77. */
  78. public function withData(array $data)
  79. {
  80. return $this->option('series', $data);
  81. }
  82. /**
  83. * 设置图表类别.
  84. *
  85. * @param array $data
  86. *
  87. * @return $this
  88. */
  89. public function withCategories(array $data)
  90. {
  91. return $this->option('xaxis.categories', $data);
  92. }
  93. /**
  94. * 渲染图表
  95. *
  96. * @return string
  97. */
  98. public function render()
  99. {
  100. $this->buildData();
  101. return parent::render();
  102. }
  103. }