blueHorBarChartThree.vue 12 KB

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