当前位置:   article > 正文

uniapp 使用renderjs引入echarts_renderjs运行echart

renderjs运行echart

效果图

1.1renderjs引入echarts 

组件zmui-echarts.vue:

  1. <template>
  2. <view class="zmui-echarts" :prop="option" :change:prop="echarts.delay"></view>
  3. </template>
  4. <script>
  5. export default {
  6. name: 'zmuiEcharts',
  7. props: {
  8. option: {
  9. type: Object,
  10. required: true
  11. }
  12. }
  13. }
  14. </script>
  15. //使用renderjs 就让app拥有类似有.HTML文件的环境 能直接挂载.js的能力
  16. <script module="echarts" lang="renderjs">
  17. export default {
  18. data() {
  19. return {
  20. timeoutId: null,
  21. chart: null
  22. }
  23. },
  24. mounted() {
  25. if (typeof window.echarts === 'object') {
  26. this.init()
  27. } else {
  28. // 动态引入类库
  29. const script = document.createElement('script') //创建个标签
  30. script.src = './static/echarts.min.js' //引入echarts.min.js
  31. script.onload = this.init
  32. document.head.appendChild(script) //全局挂载echarts.min.js
  33. }
  34. },
  35. methods: {
  36. /**
  37. * 初始化echarts
  38. */
  39. init() {
  40. // 根据id初始化图表
  41. this.chart = echarts.init(this.$el)
  42. this.update(this.option)
  43. },
  44. /**
  45. * 防抖函数,500毫秒内只运行最后一次的方法
  46. * @param {Object} option
  47. */
  48. delay(option) {
  49. if (this.timeoutId) {
  50. clearTimeout(this.timeoutId)
  51. this.timeoutId = null
  52. }
  53. this.timeoutId = setTimeout(() => {
  54. this.update(option)
  55. }, 500)
  56. },
  57. /**
  58. * 监测数据更新
  59. * @param {Object} option
  60. */
  61. update(option) {
  62. console.log('option', option)
  63. if (this.chart) {
  64. // 因App端,回调函数无法从renderjs外传递,故在此自定义设置相关回调函数
  65. if (option) {
  66. // tooltip
  67. if (option.tooltip) {
  68. // 判断是否设置tooltip的位置
  69. if (option.tooltip.positionStatus) {
  70. option.tooltip.position = this.tooltipPosition()
  71. }
  72. // 判断是否格式化tooltip
  73. if (option.tooltip.formatterStatus) {
  74. option.tooltip.formatter = this.tooltipFormatter(option.tooltip.formatterUnit, option.tooltip.formatFloat2,
  75. option.tooltip.formatThousands)
  76. }
  77. }
  78. // legend
  79. if (option.legend) {
  80. console.log('option.legend', option.legend)
  81. if (Object.prototype.toString.call(option.legend) == '[object Array]') {
  82. for (let i in option.legend) {
  83. if (option.legend[i].formatterStatus) {
  84. option.legend[i].formatter = this.legendFormatter(option.legend[i].formatterType, option.legend[i].formatterArr)
  85. }
  86. }
  87. console.log(12)
  88. } else {
  89. if (option.legend.formatterStatus) {
  90. console.log(56)
  91. // option.legend.formatter = this.legendFormatter(option.legend.formatterType, option.legend.formatterArr)
  92. // option.legend.formatter = function(value) {
  93. // return '1'
  94. // }
  95. }
  96. console.log(34)
  97. }
  98. }
  99. // y轴
  100. if (option.yAxis) {
  101. if (option.yAxis.axisLabel) {
  102. if (option.yAxis.axisLabel.formatterStatus) {
  103. option.yAxis.axisLabel.formatter = this.yAxisFormatter(option.yAxis.axisLabel.formatterData)
  104. }
  105. }
  106. if (option.yAxis[0].axisLabel.water) {
  107. let currentChooseName = option.yAxis[0].axisLabel.water.currentChooseName
  108. let name = option.yAxis[0].axisLabel.water.name
  109. console.log('name========>', name,currentChooseName)
  110. option.yAxis[0].axisLabel.formatter = function(value) {
  111. let level = null
  112. if (currentChooseName == '西铝饮用水' || currentChooseName == '和尚山') {
  113. if (name == 'CODMn') {
  114. switch (value) {
  115. case 2:
  116. level = 2.0;
  117. break;
  118. case 4:
  119. level = 4.0;
  120. break;
  121. case 6:
  122. level = 6.0;
  123. break;
  124. case 10:
  125. level = 10.0;
  126. break;
  127. }
  128. return level;
  129. }else if (name == 'TP') {
  130. switch (value) {
  131. case 0.02:
  132. level = 0.02;
  133. break;
  134. case 0.1:
  135. level = 0.1;
  136. break;
  137. case 0.2:
  138. level = 0.2;
  139. break;
  140. case 0.3:
  141. level = 0.3;
  142. break;
  143. }
  144. return level;
  145. }else if (name == 'DO') {
  146. switch (value) {
  147. case 7.5:
  148. level = 7.5;
  149. break;
  150. case 6:
  151. level = 6.0;
  152. break;
  153. case 5:
  154. level = 5.0;
  155. break;
  156. case 3:
  157. level = 3.0;
  158. break;
  159. }
  160. return level;
  161. }else if (name == 'TN') {
  162. switch (value) {
  163. case 0.2:
  164. level = 0.2;
  165. break;
  166. case 0.5:
  167. level = 0.5;
  168. break;
  169. case 1:
  170. level = 1.0;
  171. break;
  172. case 1.5:
  173. level = 1.5;
  174. break;
  175. }
  176. return level;
  177. } else if (name == 'NH3-N') {
  178. switch (value) {
  179. case 0.15:
  180. level = 0.15;
  181. break;
  182. case 0.5:
  183. level = 0.5;
  184. break;
  185. case 1:
  186. level = 1.0;
  187. break;
  188. case 1.5:
  189. level = 1.5;
  190. break;
  191. }
  192. return level;
  193. }
  194. }else if(currentChooseName !== '西铝饮用水' || currentChooseName !== '和尚山') {
  195. if (name == 'CODMn') {
  196. switch (value) {
  197. case 4:
  198. level = 4.0;
  199. break;
  200. case 6:
  201. level = 6.0;
  202. break;
  203. case 10:
  204. level = 10.0;
  205. break;
  206. case 15:
  207. level = 15.0;
  208. break;
  209. }
  210. return level;
  211. }else if (name == 'TP') {
  212. switch (value) {
  213. case 0.1:
  214. level = 0.1;
  215. break;
  216. case 0.2:
  217. level = 0.2;
  218. break;
  219. case 0.3:
  220. level = 0.3;
  221. break;
  222. case 0.4:
  223. level = 0.4;
  224. break;
  225. }
  226. return level;
  227. } else if (name == 'DO') {
  228. switch (value) {
  229. case 6:
  230. level = 6.0;
  231. break;
  232. case 5:
  233. level = 5.0;
  234. break;
  235. case 3:
  236. level = 3.0;
  237. break;
  238. case 2:
  239. level = 2.0;
  240. break;
  241. }
  242. return level;
  243. }else if (name == 'TN') {
  244. switch (value) {
  245. case 0.5:
  246. level = 0.5;
  247. break;
  248. case 1:
  249. level = 1.0;
  250. break;
  251. case 1.5:
  252. level = 1.5;
  253. break;
  254. case 2:
  255. level = 2.0;
  256. break;
  257. }
  258. return level;
  259. } else if (name == 'NH3-N') {
  260. switch (value) {
  261. case 0.5:
  262. level = 0.5;
  263. break;
  264. case 1:
  265. level = 1.0;
  266. break;
  267. case 1.5:
  268. level = 1.5;
  269. break;
  270. case 2:
  271. level = 2.0;
  272. break;
  273. }
  274. return level;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. // 颜色渐变
  281. if (option.series) {
  282. for (let i in option.series) {
  283. let linearGradient = option.series[i].linearGradient
  284. if (option.series[i].axisLabel && option.series[i].axisLabel.formatterText) {
  285. option.series[i].axisLabel.formatter = function(value) {
  286. if (value === 0) {
  287. return '0 \n 健康';
  288. } else if (value === 1) {
  289. return '50 \n 优';
  290. } else if (value === 2) {
  291. return '100 \n 良';
  292. } else if (value === 3) {
  293. return '150 \n 轻度';
  294. } else if (value === 4) {
  295. return '200 \n 中度';
  296. } else if (value === 5) {
  297. return '300 \n 重度';
  298. } else if (value === 6) {
  299. return '500 \n 严重';
  300. }
  301. }
  302. }
  303. if (linearGradient) {
  304. option.series[i].color = new echarts.graphic.LinearGradient(linearGradient[0], linearGradient[1],
  305. linearGradient[2], linearGradient[3], linearGradient[4])
  306. }
  307. if (option.series[i].itemStyle) {
  308. if (option.series[i].itemStyle.normal) {
  309. if (option.series[i].itemStyle.normal.formatterColor) {
  310. option.series[i].itemStyle.normal.color = this.colorFormatter(option.series[i].itemStyle.normal.formatterType,option.series[i])
  311. }
  312. }
  313. }
  314. if (option.series[i].markLine) {
  315. if (option.series[i].markLine.label) {
  316. if (option.series[i].markLine.label.normal) {
  317. if (option.series[i].markLine.label.normal.formatterLabel) {
  318. option.series[i].markLine.label.normal.formatter = this.labelFormatter(option.series[i].markLine.label.normal
  319. .formatterType, option.series[i].markLine.label.normal.formatterValue)
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. console.log(option)
  328. this.chart.clear() // 这个不要删掉哟,不然切换不同图形的时候会有bug
  329. // 设置新的option
  330. this.chart.setOption(option, option.notMerge)
  331. }
  332. },
  333. waterFormatterYAxis (value) {
  334. /* let name = null
  335. switch (value) {
  336. case 2:
  337. name = 2.0;
  338. break;
  339. case 4:
  340. name = 4.0;
  341. break;
  342. case 6:
  343. name = 6.0;
  344. break;
  345. case 10:
  346. name = 10.0;
  347. break;
  348. } */
  349. console.log('value', value)
  350. // return name;
  351. },
  352. /**
  353. * 设置tooltip的位置,防止超出画布
  354. */
  355. tooltipPosition() {
  356. return (point, params, dom, rect, size) => {
  357. // 其中point为当前鼠标的位置,size中有两个属性:viewSize和contentSize,分别为外层div和tooltip提示框的大小
  358. let x = point[0]
  359. let y = point[1]
  360. let viewWidth = size.viewSize[0]
  361. let viewHeight = size.viewSize[1]
  362. let boxWidth = size.contentSize[0]
  363. let boxHeight = size.contentSize[1]
  364. let posX = 0 // x坐标位置
  365. let posY = 0 // y坐标位置
  366. if (x >= boxWidth) { // 左边放的下
  367. posX = x - boxWidth - 1
  368. }
  369. if (y >= boxHeight) { // 上边放的下
  370. posY = y - boxHeight - 1
  371. }
  372. return [posX, posY]
  373. }
  374. },
  375. /**
  376. * tooltip格式化
  377. * @param {Object} unit 数值后的单位
  378. * @param {Object} formatFloat2 是否保留两位小数
  379. * @param {Object} formatThousands 是否添加千分位
  380. */
  381. tooltipFormatter(unit, formatFloat2, formatThousands) {
  382. return params => {
  383. if (Array.isArray(params)) {
  384. var params = params[0]
  385. // #ifdef H5
  386. var result = params.data.fullDate + '/n' + params.seriesName + ":" + params.data.value
  387. // #endif
  388. // #ifdef APP-PLUS
  389. var result = params.data.fullDate + '</br>' + params.seriesName + ":" + params.data.value
  390. // #endif
  391. } else {
  392. // #ifdef H5
  393. var result = params.data.fullDate + '/n' + params.seriesName + ":" + params.data.value
  394. // #endif
  395. // #ifdef APP-PLUS
  396. var result = params.data.fullDate + '</br>' + params.seriesName + ":" + params.data.value
  397. // #endif
  398. }
  399. return result
  400. }
  401. },
  402. /**
  403. * 保留两位小数
  404. * @param {Object} value
  405. */
  406. formatFloat2(value) {
  407. let temp = Math.round(parseFloat(value) * 100) / 100
  408. let xsd = temp.toString().split('.')
  409. if (xsd.length === 1) {
  410. temp = (isNaN(temp) ? '0' : temp.toString()) + '.00'
  411. return temp
  412. }
  413. if (xsd.length > 1) {
  414. if (xsd[1].length < 2) {
  415. temp = temp.toString() + '0'
  416. }
  417. return temp
  418. }
  419. },
  420. /**
  421. * 添加千分位
  422. * @param {Object} value
  423. */
  424. formatThousands(value) {
  425. if (value === undefined || value === null) {
  426. value = ''
  427. }
  428. if (!isNaN(value)) {
  429. value = value + ''
  430. }
  431. let re = /\d{1,3}(?=(\d{3})+$)/g
  432. let n1 = value.replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) {
  433. return s1.replace(re, '$&,') + s2
  434. })
  435. return n1
  436. },
  437. /**
  438. * Y轴格式化
  439. * @param {Object} data
  440. */
  441. yAxisFormatter(data) {
  442. return params => {
  443. let result = []
  444. for (let i in data) {
  445. if (params == i) {
  446. result.push(data[i])
  447. }
  448. }
  449. return result
  450. }
  451. },
  452. /**
  453. * legend 格式
  454. * @param {Object} Type
  455. * @param {Object} Arr
  456. */
  457. legendFormatter(Type, Arr) {
  458. console.log('Arr', Arr)
  459. return params => {
  460. let result = ""
  461. var total = 0;
  462. for (var i = 0, l = Arr.length; i < l; i++) {
  463. total += Arr[i].value;
  464. }
  465. let res = Arr.filter(v => v.name === params)
  466. let val = res[0].value
  467. let precent=((val/total)*100).toFixed(2) + '%'
  468. // #ifdef H5
  469. result = params +"("+ val+"件)"
  470. // #endif
  471. // #ifdef APP-PLUS
  472. result = params +"("+ val+"件)"
  473. // #endif
  474. return result
  475. }
  476. },
  477. /**
  478. * 颜色设置
  479. * @param {Object} Type
  480. */
  481. colorFormatter(Type,series) {
  482. let value =0;
  483. return params => {
  484. //如果series 里面包含aqiData 数据 按AQI 的值来渲染颜色
  485. let value =params.value;
  486. if(series['aqiData']!=null){
  487. value =series['aqiData'][params.dataIndex];
  488. }
  489. let result = ""
  490. if (Type === 'one') {
  491. result = "rgb(126,0,35)"
  492. if (value > 0 && value < 51) {
  493. result = "#00e400"
  494. } else if (value >= 51 && value < 101) {
  495. result = "#ffff00"
  496. } else if (value>= 101 && value < 151) {
  497. result = "#ff7e00"
  498. } else if (value >= 151 && value < 201) {
  499. result = "#ff0000"
  500. } else if (value >= 201 && value < 301) {
  501. result = "#7e0023"
  502. }
  503. }
  504. return result
  505. }
  506. },
  507. /**
  508. * 标记线
  509. * @param {Object} Type
  510. * @param {Object} Value
  511. */
  512. labelFormatter(Type, Value) {
  513. return params => {
  514. let result = ""
  515. if (Type === 'one') {
  516. result = Value
  517. }
  518. return result
  519. }
  520. },
  521. }
  522. }
  523. </script>
  524. <style scoped>
  525. .zmui-echarts {
  526. width: 100%;
  527. height: 100%;
  528. }
  529. </style>

2.使用<zmuiEcharts  :option="echartOptions"></zmuiEcharts>

  1. import zmuiEcharts from '@/components/zmui-echarts/zmui-echarts.vue'
  2. components: {
  3. zmuiEcharts
  4. },
  5. <zmuiEcharts :option="echartOptions"></zmuiEcharts>
  6. export default {
  7. data(){
  8. return{
  9. echartOptions: {
  10. backgroundColor: '#ffffff',
  11. title: {
  12. text: 'mg/L',
  13. textStyle: {
  14. fontSize: 12,
  15. fontWeight: 'normal',
  16. color: '#858585', //标题颜色
  17. },
  18. top: 0,
  19. left: '4%',
  20. },
  21. tooltip: {
  22. show: false
  23. // axisPointer: {
  24. // type: "axis",
  25. // textStyle: {
  26. // color: "#000"
  27. // }
  28. // },
  29. // formatter: (data) => {
  30. // // 可以自定义文字和样式,用行内样式,数据包含在data参数中
  31. // let text = ` ${data.name}:${data.value}`
  32. // return text
  33. // }
  34. },
  35. grid: {
  36. top: "5%",
  37. right: "3%",
  38. bottom: '13%',
  39. },
  40. xAxis: [{
  41. data: [],
  42. margin: 20,
  43. interval: 0,
  44. rotate: 40,
  45. color: '#858585',
  46. textStyle: {
  47. fontSize: 12
  48. },
  49. axisLine: {
  50. lineStyle: {
  51. color: '#858585',
  52. }
  53. },
  54. axisTick: {
  55. show: true
  56. },
  57. }],
  58. axisLine: {
  59. show: false,
  60. lineStyle: {
  61. color: '#858585', //y轴颜色
  62. }
  63. },
  64. axisTick: {
  65. show: false
  66. },
  67. splitLine: {
  68. show: false
  69. },
  70. dataZoom: [{
  71. "show": true,
  72. "height": 8,
  73. "xAxisIndex": [
  74. 0
  75. ],
  76. bottom: '0%',
  77. start: 0,
  78. end: 6,
  79. handleIcon: "M-292,322.2c-3.2,0-6.4-0.6-9.3-1.9c-2.9-1.2-5.4-2.9-7.6-5.1s-3.9-4.8-5.1-7.6c-1.3-3-1.9-6.1-1.9-9.3c0-3.2,0.6-6.4,1.9-9.3c1.2-2.9,2.9-5.4,5.1-7.6s4.8-3.9,7.6-5.1c3-1.3,6.1-1.9,9.3-1.9c3.2,0,6.4,0.6,9.3,1.9c2.9,1.2,5.4,2.9,7.6,5.1s3.9,4.8,5.1,7.6c1.3,3,1.9,6.1,1.9,9.3c0,3.2-0.6,6.4-1.9,9.3c-1.2,2.9-2.9,5.4-5.1,7.6s-4.8,3.9-7.6,5.1C-285.6,321.5-288.8,322.2-292,322.2z",
  80. handleSize: "130%",
  81. handleStyle: {
  82. color: "#fff",
  83. shadowColor: "#B1B5B9",
  84. shadowBlur: 5,
  85. },
  86. backgroundColor: "#DEE3F4",
  87. fillerColor: "#008efe",
  88. endValue: 5,
  89. },
  90. {
  91. "type": "inside",
  92. "show": true,
  93. "height": 15,
  94. "start": 1,
  95. "end": 35
  96. }
  97. ],
  98. yAxis: [{
  99. type: 'value',
  100. min: 0,
  101. max: 0,
  102. interval: 0.01,
  103. name: 'nj',
  104. nameTextStyle: {
  105. color: '#ffffff'
  106. },
  107. axisLine: {
  108. show: true
  109. },
  110. axisTick: {
  111. show: false
  112. },
  113. splitLine: {
  114. show: false,
  115. },
  116. axisLabel: {
  117. color: '#858585',
  118. textStyle: {
  119. fontSize: 13
  120. },
  121. formatter: function(value1) {
  122. return value1;
  123. },
  124. water: {
  125. name: 'water',
  126. code: 'code'
  127. },
  128. },
  129. }],
  130. series: [{
  131. label: {
  132. normal: {
  133. show: true,
  134. color: '#333',
  135. position: 'top',
  136. }
  137. },
  138. name: 'nj',
  139. type: 'bar',
  140. data: [],
  141. barWidth: '8px',
  142. itemStyle: {
  143. normal: {
  144. formatterColor: true,
  145. formatterType: "one",
  146. }
  147. },
  148. }]
  149. }
  150. }
  151. }
  152. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/123972?site
推荐阅读