hengxiang.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="content">
  3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" ref="chartId" :id="chartId" class="echarts"></view>
  4. <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;">
  5. <text style="font-size: 18rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: right;color: #868da4;">冷</text>
  6. <text style="font-size: 18rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: right;color: #868da4;">热</text>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. import echarts from '../../static/echarts.js'
  12. export default {
  13. props: {
  14. chartId: String,
  15. title: {
  16. type: String,
  17. default: '总访客量'
  18. },
  19. rightTitle: {
  20. type: String,
  21. default: '占比'
  22. },
  23. dataList: {
  24. type: Array,
  25. default:() => {
  26. return []
  27. }
  28. }
  29. },
  30. data() {
  31. return {
  32. dateArray: [],
  33. data1: [],
  34. themeColor: "",
  35. themeColor25: "",
  36. option: {
  37. grid: {
  38. left: '3%',
  39. right: '4%',
  40. bottom: '3%',
  41. top: '3%',
  42. containLabel: true
  43. },
  44. xAxis: {
  45. type: 'value',
  46. show: false
  47. },
  48. yAxis: {
  49. type: 'category',
  50. data: [],
  51. axisLabel: {
  52. textStyle: {
  53. color: '#666666',
  54. fontStyle: 'normal',
  55. fontFamily: '微软雅黑',
  56. fontSize: 12,
  57. }
  58. },
  59. axisTick: {
  60. show: false
  61. },
  62. axisLine: { //坐标轴轴线相关设置
  63. lineStyle: {
  64. color: '#C8CAD2',
  65. opacity: 0.2,
  66. }
  67. },
  68. },
  69. series: [{
  70. type: 'bar',
  71. data: [],
  72. barWidth: 12,
  73. itemStyle: {
  74. // color: '#56CBCB'
  75. color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  76. offset: 0,
  77. color: "#3CD9D9"
  78. }, {
  79. offset: 1,
  80. color: '#CEF6F6'
  81. }])
  82. }
  83. }]
  84. }
  85. }
  86. },
  87. methods: {
  88. getDataAction() {
  89. // this.dataList = []
  90. // this.data1 = []
  91. // for (var i = 0; i < 3; i++) {
  92. // var num1 = Math.ceil(Math.random() * 50)
  93. // this.data1.push(num1)
  94. // this.dataList.push(num1)
  95. // }
  96. this.data1 = []
  97. this.dateArray = []
  98. var self = this
  99. this.dataList.forEach((item, index) => {
  100. self.data1.push(item.count)
  101. self.dateArray.push(item.title)
  102. })
  103. this.option.series[0].data = this.data1.length > 6 ? this.data1.slice(0, 6) : this.data1
  104. this.option.yAxis.data = this.dateArray.length > 6 ? this.dateArray.slice(0, 6) : this.dateArray
  105. // console.log('关注项目数据刷新', this.option)
  106. },
  107. onViewClick(options) {
  108. // console.log(options)
  109. }
  110. },
  111. watch: {
  112. // chartId: {
  113. // handler(e) {
  114. // // console.log('走没走!!!!', e)
  115. // this.getDataAction()
  116. // },
  117. // immediate: true
  118. // },
  119. dataList: {
  120. handler(e) {
  121. this.getDataAction()
  122. },
  123. immediate: true
  124. }
  125. },
  126. mounted() {
  127. let app = getApp()
  128. let globalData = app.globalData;
  129. this.themeColor = globalData.themeColor
  130. this.themeColor25 = globalData.themeColor25
  131. let that = this;
  132. this.option.series[0].itemStyle.color = new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  133. offset: 0,
  134. color: that.themeColor
  135. }, {
  136. offset: 1,
  137. color: that.themeColor25
  138. }])
  139. }
  140. }
  141. </script>
  142. <script module="echarts" lang="renderjs">
  143. let myChart
  144. export default {
  145. mounted() {
  146. this.initAction()
  147. },
  148. methods: {
  149. initAction() {
  150. if (typeof window.echarts === 'function') {
  151. this.initEcharts()
  152. } else {
  153. // 动态引入较大类库避免影响页面展示
  154. const script = document.createElement('script')
  155. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  156. script.src = 'static/echarts.js'
  157. script.onload = this.initEcharts.bind(this)
  158. document.head.appendChild(script)
  159. }
  160. },
  161. initEcharts() {
  162. // console.log("initEcharts -> this.$refs", this.$refs.chartId.$el.id)
  163. myChart = echarts.init(document.getElementById(this.$refs.chartId.$el.id))
  164. // 观测更新的数据在 view 层可以直接访问到
  165. if (myChart) {
  166. myChart.setOption(this.option)
  167. }
  168. },
  169. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  170. // 监听 service 层数据变更
  171. if (myChart) {
  172. myChart.setOption(newValue)
  173. }
  174. },
  175. onClick(event, ownerInstance) {
  176. // 调用 service 层的方法
  177. ownerInstance.callMethod('onViewClick', {
  178. test: 'test'
  179. })
  180. }
  181. }
  182. }
  183. </script>
  184. <style scoped lang="scss">
  185. .content {
  186. width: 100%;
  187. height: 100%;
  188. position: relative;
  189. .top-Section {
  190. padding: 0 20rpx;
  191. display: flex;
  192. flex-direction: row;
  193. justify-content: space-between;
  194. font-family: Verdana;
  195. .title {
  196. font-size: 32rpx;
  197. font-weight: bold;
  198. color: #383838;
  199. }
  200. .rightBtn {
  201. font-size: 24rpx;
  202. color: #546074;
  203. margin-top: 5rpx;
  204. }
  205. }
  206. .echarts {
  207. width: 100%;
  208. margin-top: 0;
  209. height: 400rpx;
  210. }
  211. }
  212. </style>