v1.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace zin;
  3. class echarts extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'theme?: string|array', // 主题
  10. 'width?: number|string="100%"', // 宽度
  11. 'height?: number|string=100', // 高度
  12. 'responsive?: bool', // 是否自适应
  13. 'exts?: string|array' // 插件
  14. );
  15. /**
  16. * 扩展定义。
  17. * Echart extensions map.
  18. *
  19. * @var array
  20. */
  21. public static $extMap = array
  22. (
  23. 'timeline' => 'timeline.min.js',
  24. 'liquidfill' => 'echarts-liquidfill.min.js'
  25. );
  26. /**
  27. * @param string|int $width
  28. * @param string|int $height
  29. */
  30. public function size($width, $height)
  31. {
  32. if(isDebug())
  33. {
  34. $this->triggerError('echarts::size(' . json_encode($width) . ', ' . json_encode($height) . ') is deprecated, use echarts(set::width(' . json_encode($width) . '), set::height(' . json_encode($height) . ')) in instead.', E_USER_WARNING);
  35. }
  36. $this->setProp('width', $width);
  37. $this->setProp('height', $height);
  38. return $this;
  39. }
  40. /**
  41. * @param string|mixed[] $value
  42. */
  43. public function theme($value)
  44. {
  45. $this->setProp('theme', $value);
  46. return $this;
  47. }
  48. /**
  49. * @param bool $value
  50. */
  51. public function responsive($value = true)
  52. {
  53. $this->setProp('responsive', $value);
  54. return $this;
  55. }
  56. protected function build()
  57. {
  58. global $app;
  59. list($exts, $width, $height, $responsive, $theme) = $this->prop(array('exts', 'width', 'height', 'responsive', 'theme'));
  60. $root = $app->getWebRoot() . 'js/echarts/';
  61. $files = array($root . 'echarts.common.min.js');
  62. if($exts)
  63. {
  64. $exts = is_array($exts) ? $exts : explode(',', $exts);
  65. foreach($exts as $ext)
  66. {
  67. if(isset(self::$extMap[$ext])) $ext = self::$extMap[$ext];
  68. if(!str_contains($ext, '.')) $ext = $ext . '.min.js';
  69. if(!str_starts_with($ext, '/')) $ext = $root . $ext;
  70. $files[] = $ext;
  71. }
  72. }
  73. return zui::echarts
  74. (
  75. set::_id('zin_echart_' . uniqid()),
  76. set::responsive($responsive),
  77. set::theme($theme),
  78. set::_style(array('width' => is_int($width) ? "{$width}px" : $width, 'height' => is_int($height) ? "{$height}px" : $height)),
  79. set('$lib', array('check' => 'echarts', 'src' => $files, 'root' => false)),
  80. set($this->getRestProps()),
  81. $this->children()
  82. );
  83. }
  84. }