lime-echart.vue 5.7 KB

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