lime-echart.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <view>
  3. <view style="height: 750rpx; position: relative">
  4. <l-echart ref="chart" @finished="init"></l-echart>
  5. <view
  6. class="customTooltips"
  7. :style="{ left: position[0] + 'px', top: position[1] + 'px' }"
  8. v-if="params.length && position.length && showTip"
  9. >
  10. <view>这是个自定的tooltips</view>
  11. <view>{{ params[0]['axisValue'] }}</view>
  12. <view v-for="item in params">
  13. <view>
  14. <text>{{ item.seriesName }}</text>
  15. <text>{{ item.value }}</text>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. // nvue 不需要引入
  24. // #ifdef VUE2
  25. import * as echarts from '@/uni_modules/lime-echart/static/echarts.min'
  26. // #endif
  27. // #ifdef VUE3
  28. // #ifdef MP
  29. // 由于vue3 使用vite 不支持umd格式的包,小程序依然可以使用,但需要使用require
  30. const echarts = require('../../static/echarts.min')
  31. // #endif
  32. // #ifndef MP
  33. // 由于 vue3 使用vite 不支持umd格式的包,故引入npm的包
  34. import * as echarts from 'echarts/dist/echarts.esm'
  35. // #endif
  36. // #endif
  37. export default {
  38. data() {
  39. return {
  40. showTip: false,
  41. position: [],
  42. params: [],
  43. option: {
  44. tooltip: {
  45. trigger: 'axis',
  46. // shadowBlur: 0,
  47. textStyle: {
  48. textShadowBlur: 0,
  49. },
  50. renderMode: 'richText',
  51. position: (point, params, dom, rect, size) => {
  52. // 假设自定义的tooltips尺寸
  53. const box = [170, 170]
  54. // 偏移
  55. const offsetX = point[0] < size.viewSize[0] / 2 ? 20 : -box[0] - 20
  56. const offsetY = point[1] < size.viewSize[1] / 2 ? 20 : -box[1] - 20
  57. const x = point[0] + offsetX
  58. const y = point[1] + offsetY
  59. this.position = [x, y]
  60. this.params = params
  61. },
  62. formatter: (params, ticket, callback) => {},
  63. },
  64. legend: {
  65. data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎'],
  66. },
  67. grid: {
  68. left: '3%',
  69. right: '4%',
  70. bottom: '3%',
  71. containLabel: true,
  72. },
  73. xAxis: {
  74. type: 'category',
  75. boundaryGap: false,
  76. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  77. },
  78. yAxis: {
  79. type: 'value',
  80. },
  81. series: [
  82. {
  83. name: '邮件营销',
  84. type: 'line',
  85. stack: '总量',
  86. data: [120, 132, 101, 134, 90, 230, 210],
  87. },
  88. {
  89. name: '联盟广告',
  90. type: 'line',
  91. stack: '总量',
  92. data: [220, 182, 191, 234, 290, 330, 310],
  93. },
  94. {
  95. name: '视频广告',
  96. type: 'line',
  97. stack: '总量',
  98. data: [150, 232, 201, 154, 190, 330, 410],
  99. },
  100. {
  101. name: '直接访问',
  102. type: 'line',
  103. stack: '总量',
  104. data: [320, 332, 301, 334, 390, 330, 320],
  105. },
  106. {
  107. name: '搜索引擎',
  108. type: 'line',
  109. stack: '总量',
  110. data: [820, 932, 901, 934, 1290, 1330, 1320],
  111. },
  112. ],
  113. },
  114. }
  115. },
  116. methods: {
  117. init() {
  118. // init(echarts, theme?:string, opts?:{}, chart => {})
  119. // echarts 必填, 非nvue必填,nvue不用填
  120. // theme 可选,应用的主题,目前只支持名称,如:'dark'
  121. // opts = { // 可选
  122. // locale?: string // 从 `5.0.0` 开始支持
  123. // }
  124. // chart => {} , callback 返回图表实例
  125. // setTimeout(()=>{
  126. // this.$refs.chart.init(echarts, chart => {
  127. // chart.setOption(this.option);
  128. // });
  129. // },300)
  130. this.$refs.chart.init(echarts, (chart) => {
  131. chart.setOption(this.option)
  132. // 监听tooltip显示事件
  133. chart.on('showTip', (params) => {
  134. this.showTip = true
  135. console.log('showTip::')
  136. })
  137. chart.on('hideTip', (params) => {
  138. setTimeout(() => {
  139. this.showTip = false
  140. }, 300)
  141. })
  142. setTimeout(() => {
  143. const option = {
  144. tooltip: {
  145. trigger: 'axis',
  146. // shadowBlur: 0,
  147. textStyle: {
  148. textShadowBlur: 0,
  149. },
  150. renderMode: 'richText',
  151. },
  152. legend: {
  153. data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎'],
  154. },
  155. grid: {
  156. left: '3%',
  157. right: '4%',
  158. bottom: '3%',
  159. containLabel: true,
  160. },
  161. xAxis: {
  162. type: 'category',
  163. boundaryGap: false,
  164. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  165. },
  166. yAxis: {
  167. type: 'value',
  168. },
  169. series: [
  170. {
  171. name: '邮件营销',
  172. type: 'line',
  173. stack: '总量',
  174. data: [1120, 132, 101, 134, 90, 230, 210],
  175. },
  176. {
  177. name: '联盟广告',
  178. type: 'line',
  179. stack: '总量',
  180. data: [220, 182, 191, 234, 290, 330, 310],
  181. },
  182. {
  183. name: '视频广告',
  184. type: 'line',
  185. stack: '总量',
  186. data: [150, 632, 201, 154, 190, 330, 410],
  187. },
  188. {
  189. name: '直接访问',
  190. type: 'line',
  191. stack: '总量',
  192. data: [820, 332, 301, 334, 390, 330, 320],
  193. },
  194. {
  195. name: '搜索引擎',
  196. type: 'line',
  197. stack: '总量',
  198. data: [820, 932, 901, 934, 1290, 1330, 1320],
  199. },
  200. ],
  201. }
  202. chart.setOption(option)
  203. }, 1000)
  204. })
  205. },
  206. save() {
  207. this.$refs.chart.canvasToTempFilePath({
  208. success(res) {
  209. console.log('res::::', res)
  210. },
  211. })
  212. },
  213. },
  214. }
  215. </script>
  216. <style>
  217. .customTooltips {
  218. position: absolute;
  219. background-color: rgba(255, 255, 255, 0.8);
  220. padding: 20rpx;
  221. }
  222. </style>