图表

Dcat Admin引入了apexcharts图表功能,通过Dcat\Admin\Widgets\ApexCharts\Chart这个类可以帮助开发者快速渲染图表。

简单用法

如果你需要构建一个图表,可以参考下面的方式

{tip} 更多类型的图表,请参考apexcharts官方文档

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

使用

  1. <?php
  2. use App\Admin\Widgets\Charts\MyBar;
  3. use Dcat\Admin\Widgets\Card;
  4. use Dcat\Admin\Layout\Content;
  5. class MyController
  6. {
  7. public function index(Content $content)
  8. {
  9. return $content->body(
  10. Card::make('我的图表', MyBar::make())
  11. );
  12. }
  13. }

效果

图表 - 图1

图表与后端API交互

如果你的图表需要与后端API交互,可以参考以下方式

{tip} 为了方便阅读,这里的示例代码直接继承前面定义的MyBar类。

  1. <?php
  2. namespace App\Admin\Widgets\Charts;
  3. use Illuminate\Http\Request;
  4. class MyAjaxBar extends MyBar
  5. {
  6. protected $id;
  7. protected $username;
  8. // 这里的参数一定要设置默认值
  9. public function __construct($id = null, $username = null)
  10. {
  11. parent::__construct();
  12. $this->id = $id;
  13. $this->username = $username;
  14. }
  15. /**
  16. * 处理请求
  17. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  18. *
  19. * @param Request $request
  20. * @return mixed|void
  21. */
  22. public function handle(Request $request)
  23. {
  24. // 获取 parameters 方法设置的自定义参数
  25. $id = $request->get('id');
  26. $username = $request->get('username');
  27. switch ((int) $request->get('option')) {
  28. case 30:
  29. // 你的数据查询逻辑
  30. $data = [
  31. [
  32. 'data' => [44, 55, 41, 64, 22, 43, 21]
  33. ],
  34. [
  35. 'data' => [53, 32, 33, 52, 13, 44, 32]
  36. ]
  37. ];
  38. $categories = [2001, 2002, 2003, 2004, 2005, 2006, 2007];
  39. break;
  40. case 28:
  41. // 你的数据查询逻辑
  42. $data = [
  43. [
  44. 'data' => [44, 55, 41, 64, 22, 43, 21]
  45. ],
  46. [
  47. 'data' => [53, 32, 33, 52, 13, 44, 32]
  48. ]
  49. ];
  50. $categories = [2001, 2002, 2003, 2004, 2005, 2006, 2007];
  51. break;
  52. case 7:
  53. default:
  54. // 你的数据查询逻辑
  55. $data = [
  56. [
  57. 'data' => [44, 55, 41, 64, 22, 43, 21]
  58. ],
  59. [
  60. 'data' => [53, 32, 33, 52, 13, 44, 32]
  61. ]
  62. ];
  63. $categories = [2001, 2002, 2003, 2004, 2005, 2006, 2007];
  64. break;
  65. }
  66. $this->withData($data);
  67. $this->withCategories($categories);
  68. }
  69. /**
  70. * 这里返回需要异步传递到 handler 方法的参数
  71. *
  72. * @return array
  73. */
  74. public function parameters(): array
  75. {
  76. return [
  77. 'id' => $this->id,
  78. 'username' => $this->username,
  79. ];
  80. }
  81. /**
  82. * 这里覆写父类的方法,不再查询数据
  83. */
  84. protected function buildData()
  85. {
  86. }
  87. }

用户点击构建下拉菜单加载不同的图表数据

  1. <?php
  2. use App\Admin\Widgets\Charts\MyAjaxBar;
  3. use Dcat\Admin\Widgets\Dropdown;
  4. use Dcat\Admin\Widgets\Box;
  5. use Dcat\Admin\Layout\Row;
  6. use Dcat\Admin\Layout\Content;
  7. class MyController
  8. {
  9. public function index(Content $content)
  10. {
  11. return $content->body(function (Row $row) {
  12. // 构建下拉菜单,当点击菜单时发起请求获取数据重新渲染图表
  13. $menu = [
  14. '7' => '最近7天',
  15. '28' => '最近28天',
  16. '30' => '最近30天',
  17. ];
  18. $dropdown = Dropdown::make($menu)
  19. ->button(current($menu))
  20. ->click()
  21. ->map(function ($v, $k) {
  22. // 此处设置的 data-xxx 属性会作为post数据发送到后端api
  23. return "<a class='switch-bar' data-option='{$k}'>{$v}</a>";
  24. });
  25. // 传递自定义参数
  26. $id = ...;
  27. $username = ...;
  28. $bar = MyAjaxBar::make($id, $username)
  29. ->fetching('$("#my-box").loading()') // 设置loading效果
  30. ->fetched('$("#my-box").loading(false)') // 移除loading效果
  31. ->click('.switch-bar'); // 设置图表点击菜单则重新发起请求,且被点击的目标元素上的 data-xxx 属性会被作为post数据发送到后端API
  32. $box = Box::make('我的图表2', $bar)
  33. ->id('my-box') // 设置盒子的ID
  34. ->tool($dropdown); // 设置下拉菜单按钮
  35. $row->column(12, $box);
  36. });
  37. }
  38. }

效果

图表 - 图2

设置图表配置为可执行JS代码

如果你需要在图表配置加入可执行的JS代码,可参考以下方式

  1. use use Dcat\Admin\Support\JavaScript;
  2. use Dcat\Admin\Admin;
  3. use Dcat\Admin\Widgets\ApexCharts\Chart;
  4. class MyBar extends Chart
  5. {
  6. public function __construct($containerSelector = null, $options = [])
  7. {
  8. parent::__construct($containerSelector, $options);
  9. $this->setUpOptions();
  10. }
  11. /**
  12. * 初始化图表配置
  13. */
  14. protected function setUpOptions()
  15. {
  16. $number = 20;
  17. $this->option(
  18. 'plotOptions.radialBar.dataLabels.total.formatter',
  19. // 这个值最后段代码会作为JS代码执行
  20. JavaScript::make("function () { return {$number}; }")
  21. );
  22. ...
  23. }
  24. ...
  25. }