Echarts.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace common\widgets\echarts;
  3. use Yii;
  4. use yii\base\Widget;
  5. use common\helpers\ArrayHelper;
  6. use common\helpers\StringHelper;
  7. use common\widgets\echarts\assets\AppAsset;
  8. /**
  9. * Class Echarts
  10. * @package common\widgets\echarts
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class Echarts extends Widget
  14. {
  15. /**
  16. * @var array
  17. */
  18. public $config = [];
  19. /**
  20. * 默认主题
  21. *
  22. * @var string
  23. */
  24. public $theme = 'line-bar';
  25. /**
  26. * 模板主题
  27. *
  28. * @var string
  29. */
  30. public $themeJs = 'walden';
  31. /**
  32. * 默认主题配置
  33. *
  34. * @var array
  35. */
  36. public $themeConfig = [
  37. 'today' => '今天',
  38. 'yesterday' => '昨天',
  39. 'this7Day' => '近7天',
  40. 'this30Day' => '近30天',
  41. // 'thisWeek' => '本周',
  42. // 'thisMonth' => '本月',
  43. 'thisYear' => '今年',
  44. // 'lastYear' => '去年',
  45. 'customData' => '自定义区间'
  46. ];
  47. /**
  48. * 字段
  49. *
  50. * @var array
  51. */
  52. public $columns = [
  53. // [
  54. // 'name' => 'test',
  55. // 'value' => 2022,
  56. // 'type' => 'radioList',
  57. // 'items' => [
  58. // 2022 => '2022年',
  59. // 2023 => '2023年'
  60. // ],
  61. // 'col' => 4,
  62. // ]
  63. ];
  64. /**
  65. * 盒子ID
  66. *
  67. * @var
  68. */
  69. protected $boxId;
  70. /**
  71. * @throws \yii\base\InvalidConfigException
  72. * @throws \Exception
  73. */
  74. public function init()
  75. {
  76. parent::init();
  77. $this->boxId = StringHelper::uuid('uniqid');
  78. $this->config = ArrayHelper::merge([
  79. 'server' => '',
  80. 'height' => '500px'
  81. ], $this->config);
  82. $this->themeConfig = ArrayHelper::merge([
  83. ], $this->themeConfig);
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function run()
  89. {
  90. // 注册资源
  91. $this->registerClientScript();
  92. if (empty($this->theme)) {
  93. return false;
  94. }
  95. return $this->render($this->theme, [
  96. 'boxId' => $this->boxId,
  97. 'config' => $this->config,
  98. 'themeJs' => $this->themeJs,
  99. 'themeConfig' => $this->themeConfig,
  100. 'columns' => $this->columns,
  101. ]);
  102. }
  103. /**
  104. * 注册资源
  105. */
  106. protected function registerClientScript()
  107. {
  108. $view = $this->getView();
  109. AppAsset::register($view);
  110. if ($this->theme == 'bmap') {
  111. $view->registerJsFile('https://api.map.baidu.com/api?v=2.0&ak=' . Yii::$app->services->config->backendConfig('map_baidu_ak'));
  112. }
  113. }
  114. }
粤ICP备19079148号