cyChart2.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <template>
  2. <view class="container">
  3. <div class="moreTime">
  4. <div v-for="(item,index) in range" :key="index" @click="change(index)" :class="choiceTime===index?'bod':''">
  5. {{item}}
  6. </div>
  7. </div>
  8. <div class="childTime">
  9. <div v-for="(item,index) in childTime" v-show="choiceTime===index">
  10. <template>
  11. <template v-for="(o,i) in item">
  12. <div @click="getTime(i,o.value)" :class="choiceChildTime===i?'box':''">{{o.text}}</div>
  13. </template>
  14. </template>
  15. </div>
  16. </div>
  17. <view>
  18. <div id="echart_demo" style="height: 400px;">
  19. </div>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. nowDate,
  26. timeToDate
  27. } from '../../utils/date.js'
  28. import echart from '../echarts/echarts-uniapp.vue';
  29. import * as echarts from 'echarts';
  30. export default {
  31. components: {
  32. echart,
  33. },
  34. props: {
  35. //图表类型 bar,line,pie,table
  36. chartType: {
  37. type: String,
  38. default: 'bar'
  39. },
  40. //图表高度
  41. height: {
  42. type: String,
  43. default: '400px'
  44. },
  45. //后台定义的图表id
  46. chartId: {
  47. type: String,
  48. default: null
  49. }
  50. },
  51. data() {
  52. return {
  53. chartData: {},
  54. option: {},
  55. range: ["分", "时", "日", "周"],
  56. childTime: [
  57. [{
  58. value: 900000,
  59. text: "15分"
  60. },
  61. {
  62. value: 1800000,
  63. text: "30分"
  64. },
  65. {
  66. value: 2700000,
  67. text: "45分"
  68. }
  69. ],
  70. [{
  71. value: 3600000,
  72. text: "1小时"
  73. },
  74. {
  75. value: 21600000,
  76. text: "6小时"
  77. },
  78. {
  79. value: 43200000,
  80. text: "12小时"
  81. },
  82. {
  83. value: 64800000,
  84. text: "18小时"
  85. }
  86. ],
  87. [{
  88. value: 86400000,
  89. text: "1日"
  90. },
  91. {
  92. value: 259200000,
  93. text: "3日"
  94. },
  95. {
  96. value: 432000000,
  97. text: "5日"
  98. }
  99. ],
  100. [{
  101. value: 604800000,
  102. text: "1周"
  103. },
  104. {
  105. value: 1209600000,
  106. text: "2周"
  107. },
  108. {
  109. value: 1814400000,
  110. text: "3周"
  111. },
  112. {
  113. value: 2419200000,
  114. text: "4周"
  115. }
  116. ],
  117. ],
  118. startTime: null,
  119. choiceTime: 0,
  120. choiceChildTime: 0,
  121. };
  122. },
  123. created() {
  124. this.startTime = timeToDate(new Date().getTime() - 900000)
  125. this.getServerData()
  126. },
  127. mounted() {
  128. //this.defineEcharts()
  129. },
  130. methods: {
  131. defineEcharts() {
  132. var chartDom = document.getElementById("echart_demo");
  133. //console.log(chartDom)
  134. var chart = echarts.init(chartDom)
  135. let option = {
  136. xAxis: {
  137. type: 'category',
  138. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  139. },
  140. yAxis: {
  141. type: 'value'
  142. },
  143. dataZoom: [
  144. {
  145. type: 'slider',
  146. show: true,
  147. //xAxisIndex: [0],
  148. start: 90,
  149. end: 100
  150. },
  151. ],
  152. series: [{
  153. data: [120, 200, 150, 80, 70, 110, 130],
  154. type: 'bar'
  155. }]
  156. };
  157. chart.setOption(option)
  158. console.log('------------------------')
  159. console.log(chart.getOption())
  160. },
  161. //分时日周选择
  162. change(i) {
  163. this.choiceTime === i ? this.choiceTime = -1 : this.choiceTime = i
  164. this.choiceChildTime = -1
  165. },
  166. //二级时间选择
  167. getTime(i, val) {
  168. this.choiceChildTime = i
  169. this.startTime = timeToDate(new Date().getTime() - val)
  170. this.getServerData();
  171. },
  172. //获得图表数据
  173. getServerData() {
  174. let id = this.chartId
  175. let startTime = this.startTime
  176. let endTime = timeToDate(new Date().getTime())
  177. uni.$http.get(`/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`).then(res => {
  178. const data = res.data.data;
  179. const chartData = data.chartItemList
  180. //x轴数据,时间轴
  181. const timeData = chartData[0].valueTimeList
  182. let optionData = this.getChartOption(this.chartType)
  183. optionData.title.text = data.chartName
  184. //y轴数据,数据轴
  185. if (this.chartType == 'bar' || this.chartType == 'line') {
  186. const series = chartData.map(item => {
  187. return {
  188. // name: item.describe ? item.describe : item.itemName,
  189. name: item.describe ? item.describe : item.itemReadName,
  190. data: item.valueList,
  191. type: this.chartType
  192. }
  193. })
  194. optionData.xAxis.data = timeData
  195. optionData.series = series
  196. } else if (this.chartType == 'pie') {
  197. }
  198. this.option = optionData
  199. console.log(this.option)
  200. });
  201. },
  202. //获得图表option数据
  203. getChartOption(cType) {
  204. let option = {}
  205. if (cType == 'bar') {
  206. option = {
  207. title: {
  208. text: '',
  209. textStyle: {
  210. color: '#666666',
  211. fontWeight: 'normal',
  212. fontSize: '14'
  213. }
  214. },
  215. xAxis: {
  216. type: 'category',
  217. data: [],
  218. axisLabel: {
  219. formatter: function(value) {
  220. return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join(
  221. '')
  222. }
  223. }
  224. },
  225. yAxis: {
  226. type: 'value'
  227. },
  228. legend: {
  229. type: 'scroll',
  230. top: 30
  231. },
  232. tooltip: {
  233. trigger: 'axis',
  234. axisPointer: {
  235. type: 'cross',
  236. lineStyle: {
  237. type: 'dashed'
  238. },
  239. label: {
  240. show: false
  241. }
  242. },
  243. textStyle: {
  244. color: '#ffffff'
  245. },
  246. confine: true,
  247. extraCssText: 'white-space: normal; word-break: break-all;',
  248. backgroundColor: 'rgba(108,106,120,0.9)',
  249. position: 'inside',
  250. formatter: function(params) {
  251. let result = ''
  252. for (let i in params) {
  253. let value = '--'
  254. if (params[i].data !== null) {
  255. value = params[i].data
  256. }
  257. result += '<br>' + params[i].seriesName + ':' +
  258. value
  259. }
  260. return result
  261. }
  262. },
  263. dataZoom: [
  264. {
  265. type: 'slider',
  266. show: true,
  267. //xAxisIndex: [0],
  268. start: 1,
  269. end: 35
  270. },
  271. ],
  272. series: []
  273. };
  274. } else if (cType == 'line') {
  275. option = {
  276. title: {
  277. text: '',
  278. textStyle: {
  279. color: '#666666',
  280. fontWeight: 'normal',
  281. fontSize: '14'
  282. }
  283. },
  284. xAxis: {
  285. type: 'category',
  286. data: [],
  287. axisLabel: {
  288. formatter: function(value) {
  289. return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join(
  290. '')
  291. }
  292. }
  293. },
  294. yAxis: {
  295. type: 'value'
  296. },
  297. legend: {
  298. type: 'scroll',
  299. top: 30
  300. },
  301. tooltip: {
  302. trigger: 'axis',
  303. axisPointer: {
  304. type: 'cross',
  305. lineStyle: {
  306. type: 'dashed'
  307. },
  308. label: {
  309. show: false
  310. },
  311. },
  312. textStyle: {
  313. color: '#ffffff'
  314. },
  315. confine: true,
  316. extraCssText: 'white-space: normal; word-break: break-all;',
  317. backgroundColor: 'rgba(108,106,120,0.9)', //通过设置rgba调节背景颜色与透明度
  318. position: 'inside',
  319. formatter: function(params) {
  320. let result = ''
  321. for (let i in params) {
  322. let value = '--'
  323. if (params[i].data !== null) {
  324. value = params[i].data
  325. }
  326. result += '<br>' + params[i].seriesName + ':' +
  327. value
  328. }
  329. return result
  330. }
  331. },
  332. dataZoom: [{
  333. type: 'inside',
  334. start: 90,
  335. end: 100
  336. },
  337. {
  338. type: 'slider',
  339. show: true,
  340. start: 90,
  341. end: 100,
  342. bottom: 10
  343. }
  344. ],
  345. series: []
  346. };
  347. } else if (cType == 'pie') {
  348. option = {
  349. title: {
  350. text: '',
  351. textStyle: {
  352. color: '#666666',
  353. fontWeight: 'normal',
  354. fontSize: '14'
  355. }
  356. },
  357. legend: {
  358. type: 'scroll',
  359. orient: 'vertical',
  360. left: 'left',
  361. top: 20
  362. },
  363. tooltip: {
  364. trigger: 'item',
  365. confine: true,
  366. extraCssText: 'white-space: normal; word-break: break-all;',
  367. backgroundColor: 'rgba(108,106,120,0.9)',
  368. textStyle: {
  369. color: '#ffffff'
  370. },
  371. },
  372. series: []
  373. };
  374. }
  375. return option
  376. }
  377. }
  378. };
  379. </script>
  380. <script module="myecharts" lang="renderjs">
  381. let myChart
  382. export default {
  383. mounted() {
  384. console.log('bbbbbbbbb')
  385. this.initEcharts()
  386. if (typeof window.echarts === 'function') {
  387. this.initEcharts()
  388. } else {
  389. // 动态引入较大类库避免影响页面展示
  390. const script = document.createElement('script')
  391. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  392. script.src = './echarts.js'
  393. script.onload = this.initEcharts.bind(this)
  394. document.head.appendChild(script)
  395. }
  396. },
  397. methods: {
  398. initEcharts() {
  399. myChart = myecharts.init(document.getElementById('echart_demo'))
  400. console.log('aaaaaaaaa')
  401. // 观测更新的数据在 view 层可以直接访问到
  402. myChart.setOption(this.option)
  403. },
  404. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  405. // 监听 service 层数据变更
  406. myChart.setOption(newValue)
  407. },
  408. onClick(event, ownerInstance) {
  409. // 调用 service 层的方法
  410. ownerInstance.callMethod('onViewClick', {
  411. test: 'test'
  412. })
  413. }
  414. }
  415. }
  416. </script>
  417. <style scoped>
  418. .moreTime {
  419. display: flex;
  420. justify-content: space-between;
  421. align-items: center;
  422. width: 80vw;
  423. margin: auto
  424. }
  425. .bod {
  426. border-bottom: 3px solid #007AFF;
  427. }
  428. .box {
  429. border: 1px solid #007AFF !important;
  430. }
  431. .childTime {
  432. margin: 16rpx 60rpx 0 60rpx;
  433. }
  434. .childTime>div {
  435. display: flex;
  436. justify-content: space-between;
  437. align-items: center;
  438. }
  439. .childTime>div>div {
  440. width: 140rpx;
  441. padding: 10rpx;
  442. border: 1px solid #e6e6e6;
  443. border-radius: 16rpx;
  444. color: #7e7e7e;
  445. font-size: 26rpx;
  446. text-align: center;
  447. }
  448. </style>