blueHorBarChartTwo.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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="title" v-if="type=='default'">{{title}}</view>
  7. <view class="leftBtn" v-if="type=='type1'" @click='leftBtnAction'>
  8. <view class="btnName">
  9. {{title}}
  10. </view>
  11. <image class="btnIcon" src="../../static/icons/icon_drop_black@2x.png" mode=""></image>
  12. </view>
  13. <view class="rightBtn">
  14. <view style="display: flex;" @click="exportAction" v-if="isShowExport" >
  15. <view class="btnName" style="margin-right: 1px;cursor: pointer;">导出</view>
  16. <image class="icon_export" src="../../static/icons/icon_export@2x.png" mode=""></image>
  17. </view>
  18. <view style="display: flex;margin-left: 0rpx;" @click="rightBtnAction" v-if="rightTitle.length > 0">
  19. <view class="btnName">
  20. {{rightTitle}}
  21. </view>
  22. <image class="btnIcon" src="../../static/icons/icon_dropDown@2x.png" mode=""></image>
  23. </view>
  24. </view>
  25. </view>
  26. <view style="position: relative;">
  27. <view style="width: 100%; height: 260rpx; min-height: 260rpx;display: flex;flex-direction: column;align-items: center; margin-top: 0;margin-left: 0;"
  28. v-if="dataList.length==0">
  29. <image src="../../static/icons/empty_bb.png" mode="" style="width: 283rpx;height: 227rpx;"></image>
  30. <text style="opacity: 0.35;font-size: 28rpx;font-family: PingFang SC, PingFang SC-Medium;font-weight: 500;text-align: center;color: #202638;">暂时没有{{emptyDesc}}</text>
  31. </view>
  32. <view>
  33. <view v-if="dataList.length>0" class="lineView" :style="`background-color: ${isThemeColor ? themeColor: fuzhuColor}; height: ${(nameList.length * 60 - 40) + 'rpx'} `"></view>
  34. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" ref="chartId" :id="chartId"
  35. class="echarts" v-if="fuzhuColor50!=null" :style="`height: ${(nameList.length * 60) + 'rpx'}`"></view>
  36. <view class="clickDiv">
  37. <view class="clickDiv-item" v-for="(item, index) in nameList" :key="index" style="height: 70px" @click="showAllName(item)"></view>
  38. </view>
  39. </view>
  40. </view>
  41. <view class="toastDiv" v-show="isShowToast">
  42. <view class="toast">{{toastStr}}</view>
  43. </view>
  44. <view class="goMoreData" @click="getMoreDataAction" v-if="isShowMore">查看更多></view>
  45. <!-- #endif -->
  46. </view>
  47. </template>
  48. <script>
  49. import echarts from '../../static/echarts.js'
  50. export default {
  51. props: {
  52. themeColor: null,
  53. fuzhuColor: null,
  54. themeColor50: null,
  55. themeColor25: null,
  56. fuzhuColor50: null,
  57. chartId: String,
  58. title: String,
  59. rightTitle: String,
  60. isThemeColor: {
  61. type: Boolean,
  62. default: true
  63. },
  64. dataCount: {
  65. type: Number,
  66. default: 6
  67. },
  68. unit: {
  69. type: String,
  70. default: '%'
  71. },
  72. dataList: Array,
  73. yDataList: Array,
  74. isShowMore: {
  75. type: Boolean,
  76. default: false
  77. },
  78. isShowExport: {
  79. type: Boolean,
  80. default: false
  81. },
  82. emptyDesc: {
  83. type: String,
  84. default: "数据"
  85. },
  86. type: {
  87. type: String,
  88. default: 'default'
  89. }
  90. },
  91. data() {
  92. return {
  93. y2DataList: [],
  94. nameList: [],
  95. option: {
  96. // tooltip: {
  97. // // trigger: 'item',
  98. // // align: 'auto',
  99. // formatter: '{b}',
  100. // extraCssText: 'z-index:50'
  101. // },
  102. grid: {
  103. left: '36%',
  104. right: '20%',
  105. top: '0%',
  106. bottom: '0%',
  107. containLabel: false
  108. },
  109. xAxis: {
  110. type: 'value',
  111. show: false
  112. },
  113. yAxis: [{
  114. type: 'category',
  115. data: [],
  116. inverse: true,
  117. axisLabel: { //坐标轴刻度标签的相关设置。
  118. margin: 115,
  119. align: 'left',
  120. interval: 0,
  121. textStyle: {
  122. color: '#393939',
  123. fontFamily: 'Verdana',
  124. fontSize: 14,
  125. // rowNumber: 2
  126. // lineHeight: 20,
  127. }
  128. },
  129. axisTick: { //坐标轴刻度相关设置。
  130. show: false,
  131. },
  132. axisLine: { //坐标轴轴线相关设置
  133. show: false
  134. },
  135. },
  136. {
  137. type: 'category',
  138. data: [],
  139. inverse: true,
  140. axisLabel: { //坐标轴刻度标签的相关设置。
  141. margin: 60,
  142. align: 'right',
  143. textStyle: {
  144. color: '',
  145. fontFamily: 'Verdana',
  146. fontSize: 14
  147. },
  148. // formatter: (a) => {
  149. // return a + '%'
  150. // },
  151. },
  152. axisTick: { //坐标轴刻度相关设置。
  153. show: false,
  154. },
  155. axisLine: { //坐标轴轴线相关设置
  156. show: false
  157. }
  158. }
  159. ],
  160. series: {
  161. name: '',
  162. type: 'bar',
  163. barWidth: 6,
  164. data: [],
  165. itemStyle: { //图形样式
  166. normal: {
  167. barBorderRadius: 3,
  168. color: ''
  169. },
  170. }
  171. }
  172. },
  173. isShowToast: false,
  174. toastStr: ''
  175. }
  176. },
  177. mounted() {
  178. },
  179. methods: {
  180. getDataAction() {
  181. this.option.series.data = []
  182. this.option.yAxis[0].data = []
  183. this.option.yAxis[1].data
  184. this.option.series.data = this.dataList
  185. // // console.log("getDataAction -> this.dataList", this.chartId, this.dataList)
  186. var yTempDataList = JSON.parse(JSON.stringify(this.yDataList.length > 6 ? this.yDataList.slice(0, 6) : this.yDataList))
  187. this.nameList = JSON.parse(JSON.stringify(this.yDataList.length > 6 ? this.yDataList.slice(0, 6) : this.yDataList))
  188. for (var i = 0; i < yTempDataList.length; i++) {
  189. let item = yTempDataList[i]
  190. if (item.length > 7) {
  191. yTempDataList[i] = item.slice(0, 7) + "..."
  192. }
  193. }
  194. // for (var i = 0; i < yTempDataList.length; i++) {
  195. // // let item = yTempDataList[i]
  196. // // if(item.length>5){
  197. // // yTempDataList[i] = item.slice(0,5)+"..."
  198. // // }
  199. // let item = yTempDataList[i]
  200. // // if (item.length > 5) {
  201. // // yTempDataList[i] = item.slice(0, 5) + "..."
  202. // // // yTempDataList[i] = item.join()
  203. // // // yTempDataList[i] = this.insertStr()
  204. // // }
  205. // var newParamsName = ""; // 最终拼接成的字符串
  206. // var paramsNameNumber = item.length; // 实际标签的个数
  207. // var provideNumber = 6; // 每行能显示的字的个数
  208. // var rowNumber = Math.ceil(paramsNameNumber / provideNumber); // 换行的话,需要显示几行,向上取整
  209. // /**
  210. // * 判断标签的个数是否大于规定的个数, 如果大于,则进行换行处理 如果不大于,即等于或小于,就返回原标签
  211. // */
  212. // // 条件等同于rowNumber>1
  213. // if (paramsNameNumber > provideNumber) {
  214. // /** 循环每一行,p表示行 */
  215. // for (var p = 0; p < rowNumber; p++) {
  216. // var tempStr = ""; // 表示每一次截取的字符串
  217. // var start = p * provideNumber; // 开始截取的位置
  218. // var end = start + provideNumber; // 结束截取的位置
  219. // // 此处特殊处理最后一行的索引值
  220. // if (p == rowNumber - 1) {
  221. // // 最后一次不换行
  222. // tempStr = item.substring(start, paramsNameNumber);
  223. // } else {
  224. // // 每一次拼接字符串并换行
  225. // tempStr = item.substring(start, end) + "\n";
  226. // }
  227. // newParamsName += tempStr; // 最终拼成的字符串
  228. // }
  229. // } else {
  230. // // 将旧标签的值赋给新标签
  231. // newParamsName = item;
  232. // }
  233. // //将最终的字符串返回
  234. // yTempDataList[i] = newParamsName
  235. // if (yTempDataList[i].length > 12) {
  236. // yTempDataList[i] = yTempDataList[i].slice(0, 12) + '...'
  237. // }
  238. // }
  239. // console.log("当前数,--+pp", yTempDataList)
  240. this.option.yAxis[0].data = yTempDataList
  241. this.y2DataList = []
  242. var self = this
  243. this.dataList.forEach((item, index) => {
  244. self.y2DataList.push((item + self.unit))
  245. })
  246. var y2TempDataList = this.y2DataList.length > 6 ? this.y2DataList.slice(0, 6) : this.y2DataList
  247. this.option.yAxis[1].data = y2TempDataList
  248. // this.reloadColor()
  249. // console.log('柱状图A数据刷新', this.option)
  250. uni.$emit('onchanged')
  251. },
  252. reloadColor() {
  253. this.option.series.itemStyle.normal.color = new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
  254. offset: 0,
  255. color: this.isThemeColor ? this.themeColor : this.fuzhuColor
  256. }, {
  257. offset: 1,
  258. color: this.isThemeColor ? this.themeColor50 : this.fuzhuColor50
  259. }])
  260. this.option.yAxis[1].axisLabel.textStyle.color = this.isThemeColor ? this.themeColor : this.fuzhuColor
  261. },
  262. onViewClick(options) {
  263. // console.log(options)
  264. },
  265. rightBtnAction() {
  266. this.$emit('chartRightBtnAction', this.chartId)
  267. },
  268. getMoreDataAction() {
  269. this.$emit('chartGetMoreDataAction', this.chartId)
  270. },
  271. leftBtnAction() {
  272. this.$emit('chartLeftBtnAction', this.chartId)
  273. },
  274. exportAction(){
  275. this.$emit('exportAction',this.chartId)
  276. },
  277. showAllName(e) {
  278. // console.log('名字打印:', e)
  279. this.toastStr = e
  280. this.isShowToast = true
  281. var self = this
  282. var timer = setTimeout(function() {
  283. self.isShowToast = false
  284. clearTimeout(timer)
  285. }, 2000);
  286. }
  287. },
  288. watch: {
  289. chartId: {
  290. handler(e) {
  291. this.getDataAction()
  292. },
  293. immediate: true
  294. },
  295. dataList: {
  296. handler(e) {
  297. this.getDataAction()
  298. },
  299. immediate: true
  300. },
  301. yDataList: {
  302. handler(e) {
  303. this.getDataAction()
  304. },
  305. immediate: true
  306. },
  307. themeColor: {
  308. handler(e) {
  309. this.reloadColor()
  310. },
  311. immediate: true
  312. }
  313. }
  314. }
  315. </script>
  316. <script module="echarts" lang="renderjs">
  317. let myChart
  318. export default {
  319. mounted() {
  320. this.initAction();
  321. },
  322. methods: {
  323. initAction() {
  324. if (typeof window.echarts === 'function') {
  325. this.initEcharts()
  326. } else {
  327. // 动态引入较大类库避免影响页面展示
  328. const script = document.createElement('script')
  329. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  330. script.src = 'static/echarts.js'
  331. script.onload = this.initEcharts.bind(this)
  332. document.head.appendChild(script)
  333. }
  334. },
  335. initEcharts() {
  336. // console.log("initEcharts -> this.$refs", this.$refs.chartId.$el.id)
  337. myChart = echarts.init(document.getElementById(this.$refs.chartId.$el.id))
  338. // 观测更新的数据在 view 层可以直接访问到
  339. if (myChart) {
  340. myChart.setOption(this.option)
  341. myChart.resize();
  342. }
  343. },
  344. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  345. // 监听 service 层数据变更
  346. if (myChart) {
  347. myChart.setOption(newValue)
  348. myChart.resize();
  349. }
  350. },
  351. onClick(event, ownerInstance) {
  352. // 调用 service 层的方法
  353. ownerInstance.callMethod('onViewClick', {
  354. test: 'test'
  355. })
  356. }
  357. }
  358. }
  359. </script>
  360. <style scoped lang="scss">
  361. .content {
  362. width: 100%;
  363. height: 100%;
  364. // background-color: #FFFFFF;
  365. position: relative;
  366. // padding: 20rpx;
  367. padding-bottom: 20rpx;
  368. margin-bottom: 40rpx;
  369. // background-color: #4CD964;
  370. .top-Section {
  371. padding: 0 20rpx;
  372. display: flex;
  373. flex-direction: row;
  374. justify-content: space-between;
  375. font-family: Verdana;
  376. margin-bottom: 20rpx;
  377. .title {
  378. font-size: 32rpx;
  379. font-weight: bold;
  380. color: #383838;
  381. display: block;
  382. }
  383. .leftBtn {
  384. display: flex;
  385. .btnName {
  386. font-size: 32rpx;
  387. font-weight: bold;
  388. color: #383838;
  389. }
  390. .btnIcon {
  391. color: #000000;
  392. margin-left: 10rpx;
  393. margin-top: 7rpx;
  394. width: 30rpx;
  395. height: 30rpx;
  396. }
  397. }
  398. .rightBtn {
  399. font-size: 24rpx;
  400. margin-top: 5rpx;
  401. display: flex;
  402. flex-direction: row;
  403. .btnName {
  404. color: #546074;
  405. }
  406. .btnIcon {
  407. color: #000000;
  408. margin-left: 10rpx;
  409. margin-top: 2rpx;
  410. width: 30rpx;
  411. height: 30rpx;
  412. }
  413. .icon_export {
  414. color: #000000;
  415. margin-right: 20rpx;
  416. margin-left: 10rpx;
  417. margin-top: 2rpx;
  418. width: 26rpx;
  419. height: 25rpx;
  420. }
  421. }
  422. }
  423. .echarts {
  424. width: 100%;
  425. margin-top: 0;
  426. }
  427. .goMoreData {
  428. font-family: Verdana;
  429. color: #546074;
  430. font-size: 24rpx;
  431. text-align: center;
  432. margin-top: 30rpx;
  433. }
  434. .lineView {
  435. position: absolute;
  436. left: 26rpx;
  437. width: 4rpx;
  438. border-radius: 2rpx;
  439. top: 18rpx;
  440. }
  441. .clickDiv {
  442. width: 100%;
  443. height: 90%;
  444. // background-color: #4CD964;
  445. // opacity: 0.5;
  446. z-index: 100;
  447. top: 10%;
  448. left: 0;
  449. position: absolute;
  450. display: flex;
  451. flex-direction: column;
  452. .clickDiv-item {
  453. width: 100%;
  454. margin-left: 0;
  455. // background-color: #007AFF;
  456. // opacity: 0.5;
  457. margin-bottom: 5px;
  458. }
  459. }
  460. .toastDiv {
  461. position: absolute;
  462. top: 0;
  463. left: 0;
  464. width: 100%;
  465. height: 100%;
  466. display: flex;
  467. align-items: center;
  468. justify-content: center;
  469. .toast {
  470. border-radius: 10rpx;
  471. background-color: rgba($color: #333, $alpha: 0.42);
  472. font-size: 28rpx;
  473. text-align: center;
  474. padding: 20rpx;
  475. max-width: 90%;
  476. line-height: 60rpx;
  477. color: #FFFFFF;
  478. font-family: Verdana;
  479. }
  480. }
  481. }
  482. </style>