l-echart.uvue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <!-- #ifdef APP -->
  3. <web-view class="lime-echart" ref="chartRef" @load="loaded" :style="[customStyle]"
  4. :webview-styles="[webviewStyles]" src="/uni_modules/lime-echart/static/uvue.html?v=10112">
  5. </web-view>
  6. <!-- #endif -->
  7. <!-- #ifdef H5 -->
  8. <div class="lime-echart" ref="chartRef"></div>
  9. <!-- #endif -->
  10. </template>
  11. <script lang="uts" setup>
  12. // @ts-nocheck
  13. import { Echarts } from './uvue';
  14. type EchartsResolve = (value : Echarts) => void
  15. defineOptions({
  16. name: 'l-echart'
  17. })
  18. const emits = defineEmits(['finished'])
  19. const props = defineProps({
  20. // #ifdef APP
  21. webviewStyles: {
  22. type: Object
  23. },
  24. customStyle: {
  25. type: Object
  26. },
  27. // #endif
  28. // #ifndef APP
  29. webviewStyles: {
  30. type: Object
  31. },
  32. customStyle: {
  33. type: [String, Object]
  34. },
  35. // #endif
  36. isDisableScroll: {
  37. type: Boolean,
  38. default: false
  39. },
  40. isClickable: {
  41. type: Boolean,
  42. default: true
  43. },
  44. enableHover: {
  45. type: Boolean,
  46. default: false
  47. },
  48. beforeDelay: {
  49. type: Number,
  50. default: 30
  51. }
  52. })
  53. const finished = ref(false)
  54. const map = [] as EchartsResolve[]
  55. const callbackMap = [] as EchartsResolve[]
  56. // let context = null as UniWebViewElement | null
  57. let chart = null as Echarts | null
  58. let chartRef = ref<UniWebViewElement | null>(null)
  59. const trigger = () => {
  60. // #ifdef APP
  61. if (finished.value) {
  62. if (chart == null) {
  63. chart = new Echarts(chartRef.value!)
  64. }
  65. while (map.length > 0) {
  66. const resolve = map.pop() as EchartsResolve
  67. resolve(chart!)
  68. }
  69. }
  70. // #endif
  71. // #ifdef H5
  72. while (map.length > 0) {
  73. if(chart != null){
  74. const resolve = map.pop() as EchartsResolve
  75. resolve(chart!)
  76. }
  77. }
  78. // #endif
  79. if(chart != null){
  80. while(callbackMap.length > 0){
  81. const callback = callbackMap.pop() as EchartsResolve
  82. callback(chart!)
  83. }
  84. }
  85. }
  86. // #ifdef APP
  87. const loaded = (event : UniWebViewLoadEvent) => {
  88. event.stopPropagation()
  89. event.preventDefault()
  90. finished.value = true
  91. trigger()
  92. emits('finished')
  93. }
  94. // #endif
  95. const _next = () : boolean => {
  96. if (chart == null) {
  97. console.warn(`组件还未初始化,请先使用 init`)
  98. return true
  99. }
  100. return false
  101. }
  102. const setOption = (option : UTSJSONObject) => {
  103. if (_next()) return
  104. chart!.setOption(option);
  105. }
  106. const showLoading = () => {
  107. if (_next()) return
  108. chart!.showLoading();
  109. }
  110. const hideLoading = () => {
  111. if (_next()) return
  112. chart!.hideLoading();
  113. }
  114. const clear = () => {
  115. if (_next()) return
  116. chart!.clear();
  117. }
  118. const dispose = () => {
  119. if (_next()) return
  120. chart!.dispose();
  121. }
  122. const resize = (size : UTSJSONObject) => {
  123. if (_next()) return
  124. chart!.resize(size);
  125. }
  126. const canvasToTempFilePath = (opt : UTSJSONObject) => {
  127. if (_next()) return
  128. chart!.canvasToTempFilePath(opt);
  129. }
  130. // function init() : Promise<Echarts> {
  131. // return new Promise((resolve) => {
  132. // map.push(resolve)
  133. // trigger()
  134. // })
  135. // }
  136. // #ifdef APP
  137. function init(callback : ((chart : Echarts) => void) | null) : Promise<Echarts> {
  138. // if (chart !== null && callback != null) {
  139. // callback(chart!)
  140. // } else {
  141. // console.warn('echarts 未加载完成,您可以延时一下')
  142. // }
  143. if(callback!=null){
  144. callbackMap.push(callback)
  145. }
  146. return new Promise<Echarts>((resolve) => {
  147. map.push(resolve)
  148. trigger()
  149. })
  150. }
  151. // #endif
  152. // #ifdef H5
  153. const touchstart = (e) => {
  154. if(chart == null) return
  155. const handler = chart.getZr().handler;
  156. const rect = chart.getZr().dom.getBoundingClientRect()
  157. handler.dispatch('mousedown', {
  158. zrX: e.touches[0].clientX - rect.left,
  159. zrY: e.touches[0].clientY - rect.top
  160. })
  161. handler.dispatch('click', {
  162. zrX: e.touches[0].clientX - rect.left,
  163. zrY: e.touches[0].clientY - rect.top
  164. })
  165. }
  166. const touchmove = (e) => {
  167. if(chart == null) return
  168. const handler = chart.getZr().handler;
  169. const rect = chart.getZr().dom.getBoundingClientRect()
  170. handler.dispatch('mousemove', {
  171. zrX: e.touches[0].clientX - rect.left,
  172. zrY: e.touches[0].clientY - rect.top
  173. })
  174. }
  175. const mouseup = (e) => {
  176. if(chart == null) return
  177. const handler = chart.getZr().handler;
  178. handler.dispatch('mousemove', {
  179. zrX: 999999999,
  180. zrY: 999999999
  181. })
  182. handler.dispatch('mouseup', {
  183. zrX: 999999999,
  184. zrY: 999999999
  185. })
  186. }
  187. function init(echarts: any, ...args: any[]): Promise<Echarts>{
  188. if(echarts == null){
  189. console.error('请确保已经引入了 ECharts 库');
  190. return Promise.reject('请确保已经引入了 ECharts 库');
  191. }
  192. let theme:string|null=null
  193. let opts={}
  194. let callback:Function|null=null;
  195. args.forEach(item =>{
  196. if(typeof item === 'function') {
  197. callback = item
  198. } else if(['string'].includes(typeof item)){
  199. theme = item
  200. } else if(typeof item === 'object'){
  201. opts = item
  202. }
  203. })
  204. chart = echarts.init(chartRef.value, theme, opts)
  205. window.addEventListener('touchstart', touchstart)
  206. window.addEventListener('touchmove', touchmove)
  207. window.addEventListener('touchend', mouseup)
  208. if(callback!=null && typeof callback == 'function'){
  209. callbackMap.push(callback)
  210. }
  211. return new Promise<Echarts>((resolve) => {
  212. map.push(resolve)
  213. trigger()
  214. })
  215. }
  216. onMounted(()=>{
  217. finished.value = true
  218. trigger()
  219. emits('finished')
  220. })
  221. onUnmounted(()=>{
  222. window.removeEventListener('touchstart', touchstart)
  223. window.removeEventListener('touchmove', touchmove)
  224. window.removeEventListener('touchend', mouseup)
  225. })
  226. // #endif
  227. defineExpose({
  228. init,
  229. setOption,
  230. showLoading,
  231. hideLoading,
  232. clear,
  233. dispose,
  234. resize,
  235. canvasToTempFilePath
  236. })
  237. </script>
  238. <style lang="scss">
  239. .lime-echart {
  240. flex: 1;
  241. }
  242. </style>