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

115 lines
2.4 KiB

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. public function __construct($containerSelector = null, $options = [])
  9. {
  10. parent::__construct($containerSelector, $options);
  11. $this->setUpOptions();
  12. }
  13. /**
  14. * 初始化图表配置
  15. */
  16. protected function setUpOptions()
  17. {
  18. $color = Admin::color();
  19. $colors = [$color->primary(), $color->primaryDarker()];
  20. $this->options([
  21. 'colors' => $colors,
  22. 'chart' => [
  23. 'type' => 'bar',
  24. 'height' => 430
  25. ],
  26. 'plotOptions' => [
  27. 'bar' => [
  28. 'horizontal' => false,
  29. 'dataLabels' => [
  30. 'position' => 'top',
  31. ],
  32. ]
  33. ],
  34. 'dataLabels' => [
  35. 'enabled' => true,
  36. 'offsetX' => -6,
  37. 'style' => [
  38. 'fontSize' => '12px',
  39. 'colors' => ['#fff']
  40. ]
  41. ],
  42. 'stroke' => [
  43. 'show' => true,
  44. 'width' => 1,
  45. 'colors' => ['#fff']
  46. ],
  47. 'xaxis' => [
  48. 'categories' => [],
  49. ],
  50. ]);
  51. }
  52. /**
  53. * 处理图表数据
  54. */
  55. protected function buildData()
  56. {
  57. // 执行你的数据查询逻辑
  58. $data = [
  59. [
  60. 'data' => [44, 55, 41, 64, 22, 43, 21]
  61. ],
  62. [
  63. 'data' => [53, 32, 33, 52, 13, 44, 32]
  64. ]
  65. ];
  66. $categories = [2001, 2002, 2003, 2004, 2005, 2006, 2007];
  67. $this->withData($data);
  68. $this->withCategories($categories);
  69. }
  70. /**
  71. * 设置图表数据
  72. *
  73. * @param array $data
  74. *
  75. * @return $this
  76. */
  77. public function withData(array $data)
  78. {
  79. return $this->option('series', $data);
  80. }
  81. /**
  82. * 设置图表类别.
  83. *
  84. * @param array $data
  85. *
  86. * @return $this
  87. */
  88. public function withCategories(array $data)
  89. {
  90. return $this->option('xaxis.categories', $data);
  91. }
  92. /**
  93. * 渲染图表
  94. *
  95. * @return string
  96. */
  97. public function render()
  98. {
  99. $this->buildData();
  100. return parent::render();
  101. }
  102. }