doubleLineChart.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="content">
  3. <!-- #ifdef APP-PLUS || H5 -->
  4. <view class="top-Section">
  5. <view class="title">{{title}}</view>
  6. <view class="rightBtn" @click="yesterdayCompare" v-if="isShowYesterDay">
  7. <view class="rightName">昨日环比</view>
  8. <view class="rightIcon" :style="`border-color: ${fuzhuColor};`" v-if="isCompare"></view>
  9. <view class="rightIcon" style="background-color: #F4F7F7; border-color: #F4F7F7;" v-else></view>
  10. </view>
  11. </view>
  12. <view v-if="themeColor!=null" @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" ref="chartId"
  13. :id="chartId" class="echarts"></view>
  14. <!-- #endif -->
  15. </view>
  16. </template>
  17. <script>
  18. import echarts from '../../static/echarts.js'
  19. export default {
  20. props: {
  21. themeColor: String,
  22. fuzhuColor: String,
  23. themeColor50: String,
  24. themeColor25: String,
  25. fuzhuColor50: String,
  26. chartId: String,
  27. title: String,
  28. dataList: Array,
  29. data1: Array,
  30. data2: Array,
  31. isShowYesterDay: {
  32. type: Boolean,
  33. default: false
  34. },
  35. },
  36. data() {
  37. return {
  38. // dataList: [],
  39. // data1: [],
  40. // data2: [],
  41. option: {
  42. tooltip: {
  43. trigger: 'axis',
  44. axisPointer: {
  45. type: 'line',
  46. lineStyle: {
  47. color: '#F1F5F9',
  48. // color: '#3CD9D9',
  49. type: 'dotted',
  50. width: 2
  51. },
  52. label: {
  53. backgroundColor: '#6a7985'
  54. },
  55. },
  56. extraCssText: 'z-index:50'
  57. },
  58. grid: {
  59. left: '2%',
  60. right: '5%',
  61. bottom: 0,
  62. top: '10%',
  63. padding: 0,
  64. containLabel: true,
  65. },
  66. // dataZoom: {
  67. // type: 'inside',
  68. // start: 0,
  69. // end: 30,
  70. // zoomLock: true,
  71. // },
  72. xAxis: {
  73. type: 'category',
  74. boundaryGap: false,
  75. data: [],
  76. axisLabel: { //坐标轴刻度标签的相关设置。
  77. margin: 10,
  78. textStyle: {
  79. color: '#616F88',
  80. fontFamily: 'Verdana',
  81. fontSize: 10
  82. },
  83. },
  84. axisTick: { //坐标轴刻度相关设置。
  85. show: false,
  86. },
  87. axisLine: { //坐标轴轴线相关设置
  88. show: false
  89. },
  90. },
  91. yAxis: {
  92. // show: false,
  93. type: 'value',
  94. splitNumber: 4,
  95. boundaryGap: false,
  96. axisTick: {
  97. show: false
  98. },
  99. axisLine: { //坐标轴轴线相关设置
  100. // show: false
  101. lineStyle: {
  102. color: '#707070',
  103. opacity: 0
  104. }
  105. },
  106. minInterval: 1,
  107. splitLine: {
  108. show: false
  109. }
  110. },
  111. series: [{
  112. name: '昨日',
  113. type: 'line',
  114. // stack: '总量',
  115. symbol: 'circle',
  116. symbolSize: 6,
  117. itemStyle: {
  118. opacity: 0,
  119. normal: {
  120. borderRadius: 3,
  121. color: this.fuzhuColor,
  122. borderWidth: 2,
  123. borderColor: '#FFF'
  124. },
  125. },
  126. areaStyle: {
  127. color: ''
  128. },
  129. smooth: true,
  130. lineStyle: {
  131. color: ''
  132. },
  133. data: []
  134. },
  135. {
  136. name: '当前',
  137. type: 'line',
  138. // stack: '总量',
  139. symbol: 'circle',
  140. symbolSize: 6,
  141. itemStyle: {
  142. opacity: 0,
  143. normal: {
  144. borderRadius: 3,
  145. color: '',
  146. borderWidth: 2,
  147. borderColor: '#FFF'
  148. },
  149. },
  150. areaStyle: {
  151. // color: '#56CBCB'
  152. color: ''
  153. },
  154. smooth: true,
  155. lineStyle: {
  156. // color: '#fff',
  157. // type: 'dotted'
  158. },
  159. data: [],
  160. }
  161. ]
  162. },
  163. isCompare: false
  164. }
  165. },
  166. methods: {
  167. getDataAction() {
  168. // var data1 = []
  169. // var data2 = []
  170. // var dataList = []
  171. // for (var i = 0; i < 24; i++) {
  172. // var num1 = Math.ceil(Math.random() * 100)
  173. // var num2 = Math.ceil(Math.random() * 100)
  174. // data1.push(num1)
  175. // data2.push(num2)
  176. // dataList.push(i)
  177. // }
  178. // if (this.dataList.length <= 10) {
  179. // this.option.dataZoom.end = 100
  180. // } else if (this.dataList.length > 10 && this.dataList.length <= 20) {
  181. // this.option.dataZoom.end = 70
  182. // } else if (this.dataList.length > 20 && this.dataList.length <= 30) {
  183. // this.option.dataZoom.end = 20
  184. // } else if (this.dataList.length > 30 && this.dataList.length <= 40) {
  185. // this.option.dataZoom.end = 15
  186. // } else {
  187. // this.option.dataZoom.end = 10
  188. // }
  189. this.option.series[1].data = this.data1 || []
  190. if (this.isCompare) {
  191. this.option.series[0].data = this.data2
  192. } else {
  193. this.option.series[0].data = []
  194. }
  195. this.option.xAxis.data = this.dataList || []
  196. // // console.log('数据刷新', this.option)
  197. // console.log('数据刷新Double', this.option)
  198. },
  199. onViewClick(options) {
  200. // console.log(options)
  201. },
  202. reloadColor() {
  203. // console.log('what???:', this.option.series)
  204. this.option.series[1].areaStyle.color = new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  205. offset: 0,
  206. color: this.themeColor
  207. }, {
  208. offset: 1,
  209. color: this.themeColor25
  210. }])
  211. this.option.series[0].areaStyle.color = new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  212. offset: 0,
  213. color: this.fuzhuColor50
  214. }, {
  215. offset: 1,
  216. color: '#ffffff'
  217. }])
  218. this.option.tooltip.axisPointer.lineStyle.color = this.themeColor
  219. this.option.series[0].lineStyle.color = this.fuzhuColor
  220. this.option.series[1].itemStyle.normal.color = this.themeColor
  221. this.option.series[0].itemStyle.normal.color = this.fuzhuColor
  222. },
  223. yesterdayCompare() {
  224. this.isCompare = !this.isCompare
  225. if (this.isCompare) {
  226. this.option.series[0].data = this.data2
  227. } else {
  228. this.option.series[0].data = []
  229. }
  230. }
  231. },
  232. watch: {
  233. chartId: {
  234. handler(e) {
  235. // // console.log('走没走!!!!', e)
  236. this.getDataAction()
  237. },
  238. immediate: true
  239. },
  240. themeColor: {
  241. handler(e) {
  242. // console.log('走没走!!!!', e)
  243. this.reloadColor()
  244. },
  245. immediate: true
  246. },
  247. data2: {
  248. handler(e) {
  249. this.getDataAction()
  250. },
  251. immediate: true
  252. }
  253. }
  254. }
  255. </script>
  256. <script module="echarts" lang="renderjs">
  257. let myChart
  258. export default {
  259. mounted() {
  260. this.initAction()
  261. },
  262. methods: {
  263. initAction() {
  264. if (typeof window.echarts === 'function') {
  265. this.initEcharts()
  266. } else {
  267. // 动态引入较大类库避免影响页面展示
  268. const script = document.createElement('script')
  269. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  270. script.src = 'static/echarts.js'
  271. script.onload = this.initEcharts.bind(this)
  272. document.head.appendChild(script)
  273. }
  274. },
  275. initEcharts() {
  276. // console.log("initEcharts -> this.$refs", this.$refs.chartId.$el.id)
  277. myChart = echarts.init(document.getElementById(this.$refs.chartId.$el.id))
  278. // 观测更新的数据在 view 层可以直接访问到
  279. if (myChart) {
  280. myChart.clear()
  281. myChart.setOption(this.option)
  282. }
  283. },
  284. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  285. // 监听 service 层数据变更
  286. if (myChart) {
  287. myChart.clear()
  288. myChart.setOption(newValue)
  289. }
  290. },
  291. onClick(event, ownerInstance) {
  292. // 调用 service 层的方法
  293. ownerInstance.callMethod('onViewClick', {
  294. test: 'test'
  295. })
  296. }
  297. }
  298. }
  299. </script>
  300. <style scoped lang="scss">
  301. .content {
  302. width: 100%;
  303. height: 100%;
  304. // background-color: #FFFFFF;
  305. overflow: hidden;
  306. margin-bottom: 80rpx;
  307. .top-Section {
  308. padding: 0 20rpx;
  309. display: flex;
  310. flex-direction: row;
  311. justify-content: space-between;
  312. font-family: Verdana;
  313. .title {
  314. font-size: 32rpx;
  315. font-weight: bold;
  316. color: #383838;
  317. }
  318. .rightBtn {
  319. font-size: 24rpx;
  320. color: #546074;
  321. margin-top: 5rpx;
  322. display: flex;
  323. flex-direction: row;
  324. justify-content: flex-end;
  325. .rightName {
  326. margin-right: 12rpx;
  327. margin-top: 5rpx;
  328. }
  329. .rightIcon {
  330. width: 20rpx;
  331. height: 20rpx;
  332. border-radius: 50%;
  333. border: 10rpx solid;
  334. }
  335. }
  336. }
  337. .echarts {
  338. width: 100%;
  339. margin-top: 0;
  340. height: 350rpx;
  341. }
  342. }
  343. </style>