yaoyueChart.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view class="content">
  3. <!-- #ifdef APP-PLUS || H5 -->
  4. <view class="title"
  5. :style="`background-color: ${isYellow ? fuzhuColor50 : themeColor50};`">
  6. {{title}}
  7. </view>
  8. <view class="count">
  9. {{count}}
  10. </view>
  11. <view :prop="option" :change:prop="echarts.updateEcharts" id="yaoyueChart" class="echarts" v-show="fuzhuColor50!=null && dataList.length > 0"></view>
  12. <!-- #endif -->
  13. <!-- #ifndef APP-PLUS || H5 -->
  14. <view>非 APP、H5 环境不支持</view>
  15. <!-- #endif -->
  16. </view>
  17. </template>
  18. <script>
  19. import echarts from '../../../static/echarts.js'
  20. export default {
  21. props: {
  22. title: String,
  23. count: Number,
  24. dataList: {
  25. type: Array,
  26. default: () => {
  27. return []
  28. }
  29. },
  30. isYellow: Boolean,
  31. themeColor50: String,
  32. fuzhuColor50: String,
  33. },
  34. data() {
  35. return {
  36. option: {
  37. grid: {
  38. left: '10%',
  39. right: '10%',
  40. top: '0%',
  41. bottom: '10%',
  42. containLabel: false
  43. },
  44. xAxis: {
  45. show: false,
  46. type: 'category',
  47. boundaryGap: ['20%', '20%']
  48. },
  49. yAxis: {
  50. show: false,
  51. type: 'value',
  52. boundaryGap: ['70%', '70%']
  53. },
  54. series: [
  55. {
  56. type: 'line',
  57. smooth: 0.5,
  58. symbol: 'none',
  59. lineStyle: {
  60. color: null,
  61. width: 3
  62. },
  63. data: []
  64. }
  65. ]
  66. }
  67. }
  68. },
  69. created() {
  70. // this.option.series[0].data = []
  71. },
  72. methods: {
  73. getDataAction() {
  74. this.option.series[0].data = this.dataList
  75. this.option.series[0].lineStyle.color = this.isYellow ? this.fuzhuColor50 : this.themeColor50
  76. // console.log('数据刷新', this.option.series)
  77. },
  78. },
  79. watch: {
  80. dataList: {
  81. handler(e1, e2) {
  82. var e1Str = JSON.stringify(e1)
  83. var e2Str = JSON.stringify(e2)
  84. if (e1Str && e1Str != e2Str) {
  85. this.getDataAction()
  86. }
  87. },
  88. deep: true,
  89. immediate: true
  90. },
  91. themeColor50: {
  92. handler(e) {
  93. if (e) {
  94. this.option.series[0].lineStyle.color = this.isYellow ? this.fuzhuColor50 : this.themeColor50
  95. }
  96. },
  97. immediate: true
  98. }
  99. }
  100. }
  101. </script>
  102. <script module="echarts" lang="renderjs">
  103. let myChart
  104. export default {
  105. mounted() {
  106. this.initAction()
  107. },
  108. methods: {
  109. initAction() {
  110. if (typeof window.echarts === 'function') {
  111. this.initEcharts()
  112. } else {
  113. // 动态引入较大类库避免影响页面展示
  114. const script = document.createElement('script')
  115. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  116. script.src = 'static/echarts.js'
  117. script.onload = this.initEcharts.bind(this)
  118. document.head.appendChild(script)
  119. }
  120. },
  121. initEcharts() {
  122. myChart = echarts.init(document.getElementById("yaoyueChart"))
  123. // 观测更新的数据在 view 层可以直接访问到
  124. myChart.setOption(this.option)
  125. },
  126. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  127. if (myChart) {
  128. // 监听 service 层数据变更
  129. myChart.setOption(newValue)
  130. }
  131. else {
  132. // console.log("无chart11111 -> this.$refs", this.$refs.chartId.$el.id)
  133. this.initAction()
  134. }
  135. },
  136. onClick(event, ownerInstance) {
  137. // 调用 service 层的方法
  138. ownerInstance.callMethod('onViewClick', {
  139. test: 'test'
  140. })
  141. }
  142. }
  143. }
  144. </script>
  145. <style>
  146. .content {
  147. width: 100%;
  148. height: 100%;
  149. }
  150. .title {
  151. font-size: 32rpx;
  152. font-family: Verdana;
  153. text-align: center;
  154. width: 176rpx;
  155. margin-left: calc((100% - 176rpx) / 2);
  156. line-height: 46rpx;
  157. /* background-color: #E1F4F4; */
  158. color: rgba(0, 0, 0, 0.65);
  159. margin-top: 28rpx;
  160. margin-left: 20rpx;
  161. border-radius: 8rpx;
  162. }
  163. .count {
  164. width: 100%;
  165. margin-top: 24rpx;
  166. height: 48rpx;
  167. line-height: 48rpx;
  168. text-align: center;
  169. font-family: Verdana;
  170. font-size: 40rpx;
  171. color: rgba(32, 38, 56, 1);
  172. }
  173. .echarts {
  174. margin-top: 10rpx;
  175. width: 100%;
  176. height: 80rpx;
  177. }
  178. </style>