<template> <view class="content"> <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" ref="chartId" :id="chartId" class="echarts"></view> <view style="display: flex;justify-content: space-between; position: absolute;bottom: 20rpx; width: 542rpx; border-top:1rpx solid rgba(200,202,210,0.3); margin-left: 135rpx;"> <text style="font-size: 18rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: right;color: #868da4;">冷</text> <text style="font-size: 18rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: right;color: #868da4;">热</text> </view> </view> </template> <script> import echarts from '../../static/echarts.js' export default { props: { chartId: String, title: { type: String, default: '总访客量' }, rightTitle: { type: String, default: '占比' }, dataList: { type: Array, default:() => { return [] } } }, data() { return { dateArray: [], data1: [], themeColor: "", themeColor25: "", option: { grid: { left: '3%', right: '4%', bottom: '3%', top: '3%', containLabel: true }, xAxis: { type: 'value', show: false }, yAxis: { type: 'category', data: [], axisLabel: { textStyle: { color: '#666666', fontStyle: 'normal', fontFamily: '微软雅黑', fontSize: 12, } }, axisTick: { show: false }, axisLine: { //坐标轴轴线相关设置 lineStyle: { color: '#C8CAD2', opacity: 0.2, } }, }, series: [{ type: 'bar', data: [], barWidth: 12, itemStyle: { // color: '#56CBCB' color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{ offset: 0, color: "#3CD9D9" }, { offset: 1, color: '#CEF6F6' }]) } }] } } }, methods: { getDataAction() { // this.dataList = [] // this.data1 = [] // for (var i = 0; i < 3; i++) { // var num1 = Math.ceil(Math.random() * 50) // this.data1.push(num1) // this.dataList.push(num1) // } this.data1 = [] this.dateArray = [] var self = this this.dataList.forEach((item, index) => { self.data1.push(item.count) self.dateArray.push(item.title) }) this.option.series[0].data = this.data1.length > 6 ? this.data1.slice(0, 6) : this.data1 this.option.yAxis.data = this.dateArray.length > 6 ? this.dateArray.slice(0, 6) : this.dateArray // console.log('关注项目数据刷新', this.option) }, onViewClick(options) { // console.log(options) } }, watch: { // chartId: { // handler(e) { // // console.log('走没走!!!!', e) // this.getDataAction() // }, // immediate: true // }, dataList: { handler(e) { this.getDataAction() }, immediate: true } }, mounted() { let app = getApp() let globalData = app.globalData; this.themeColor = globalData.themeColor this.themeColor25 = globalData.themeColor25 let that = this; this.option.series[0].itemStyle.color = new echarts.graphic.LinearGradient(1, 0, 0, 0, [{ offset: 0, color: that.themeColor }, { offset: 1, color: that.themeColor25 }]) } } </script> <script module="echarts" lang="renderjs"> let myChart export default { mounted() { this.initAction() }, methods: { initAction() { if (typeof window.echarts === 'function') { this.initEcharts() } else { // 动态引入较大类库避免影响页面展示 const script = document.createElement('script') // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算 script.src = 'static/echarts.js' script.onload = this.initEcharts.bind(this) document.head.appendChild(script) } }, initEcharts() { // console.log("initEcharts -> this.$refs", this.$refs.chartId.$el.id) myChart = echarts.init(document.getElementById(this.$refs.chartId.$el.id)) // 观测更新的数据在 view 层可以直接访问到 if (myChart) { myChart.setOption(this.option) } }, updateEcharts(newValue, oldValue, ownerInstance, instance) { // 监听 service 层数据变更 if (myChart) { myChart.setOption(newValue) } }, onClick(event, ownerInstance) { // 调用 service 层的方法 ownerInstance.callMethod('onViewClick', { test: 'test' }) } } } </script> <style scoped lang="scss"> .content { width: 100%; height: 100%; position: relative; .top-Section { padding: 0 20rpx; display: flex; flex-direction: row; justify-content: space-between; font-family: Verdana; .title { font-size: 32rpx; font-weight: bold; color: #383838; } .rightBtn { font-size: 24rpx; color: #546074; margin-top: 5rpx; } } .echarts { width: 100%; margin-top: 0; height: 400rpx; } } </style>