homeChartItem.vue 5.0 KB

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