RevenueChart.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <!-- 竖形柱状图 -->
  2. <script lang="ts" setup>
  3. import * as echarts from 'echarts'
  4. import { useThemeStore } from '@/stores/modules/theme'
  5. import { onMounted, ref, reactive, watch, computed } from 'vue'
  6. import updateIcon from '../image/xiazai.png'
  7. import * as XLSX from 'xlsx'
  8. import { formatNumber } from '@/utils/tools'
  9. const themeStore = useThemeStore()
  10. const props = defineProps({
  11. BarData: Object,
  12. barHeight: Object,
  13. RevenueStartDate: String,
  14. RevenueEndDate: String
  15. })
  16. const bar_data = ref(props.BarData)
  17. const bar_ref = ref()
  18. watch(
  19. () => bar_data.value,
  20. (current) => {
  21. bar_data.value = current
  22. initOption.xAxis.data = barName.value
  23. initOption.series = bar_series.value
  24. initOption.legend.data = Name.value
  25. initOption.toolbox.show = isShowTooltips.value
  26. nextTick(() => {
  27. initChart()
  28. })
  29. },
  30. {
  31. deep: true
  32. }
  33. )
  34. watch(
  35. () => bar_data.value,
  36. (current) => {
  37. bar_data.value = current
  38. initOption.yAxis.max = Max.value
  39. initOption.yAxis.interval = interval.value
  40. nextTick(() => {
  41. initChart()
  42. })
  43. },
  44. {
  45. deep: true,
  46. once: true
  47. }
  48. )
  49. // 最大值
  50. const Max = computed(() => {
  51. return bar_data.value?.Max
  52. })
  53. // 刻度
  54. const interval = computed(() => {
  55. return bar_data.value?.interval
  56. })
  57. // x轴值
  58. const barName = computed(() => {
  59. return bar_data.value?.barList
  60. })
  61. // title
  62. const bar_title = computed(() => {
  63. return bar_data.value?.bar_title
  64. })
  65. // series
  66. const bar_series = computed(() => {
  67. return bar_data.value?.barSeries
  68. })
  69. // legend标题
  70. const Name = computed(() => {
  71. if (bar_data.value?.barSeries) {
  72. return bar_data.value?.barSeries.map((item: any) => {
  73. return item.name
  74. })
  75. } else {
  76. return []
  77. }
  78. })
  79. const isShowTooltips = computed(() => {
  80. return bar_data.value?.isShowTooltips
  81. })
  82. const exportData = ({ filename }: any) => {
  83. $api
  84. .RevenueDownload({
  85. date_start: props.RevenueStartDate,
  86. date_end: props.RevenueEndDate
  87. })
  88. .then((res: any) => {
  89. if (res.code === 200) {
  90. let header: any = []
  91. let data: any = []
  92. let sheetname: any = []
  93. let filterA = res.data.r2_cloumn.map((el: any) => {
  94. return el.dataIndex
  95. })
  96. let titleA = res.data.r2_cloumn.map((el: any) => {
  97. return el.title
  98. })
  99. let filterB = res.data.r3_cloumn.map((el: any) => {
  100. return el.dataIndex
  101. })
  102. let titleB = res.data.r3_cloumn.map((el: any) => {
  103. return el.title
  104. })
  105. let result: any = [
  106. {
  107. tHeader: titleA,
  108. filterVal: filterA,
  109. tableDatas: res.data.r2,
  110. sheetName: res.data.r2_title
  111. },
  112. {
  113. tHeader: titleB,
  114. filterVal: filterB,
  115. tableDatas: res.data.r3,
  116. sheetName: res.data.r3_title
  117. }
  118. ]
  119. let formatJson = (filterVal: any, jsonData: any) => {
  120. return jsonData.map((v: any) => filterVal.map((j: any) => v[j]))
  121. }
  122. for (var i in result) {
  123. header.push(result[i].tHeader)
  124. sheetname.push(result[i].sheetName)
  125. data.push(formatJson(result[i].filterVal, result[i].tableDatas))
  126. }
  127. // 将表头插入数据数组中
  128. for (let i = 0; i < header.length; i++) {
  129. data[i].unshift(header[i])
  130. }
  131. let ws_name = sheetname
  132. // 创建工作簿对象
  133. let wb = XLSX.utils.book_new()
  134. let ws: any = []
  135. // 创建每个工作表并设置列宽
  136. for (let j = 0; j < header.length; j++) {
  137. ws.push(XLSX.utils.aoa_to_sheet(data[j]))
  138. let arr: any = []
  139. header[j].forEach((val: any) => {
  140. arr.push({
  141. wpx: 120
  142. })
  143. })
  144. ws[j]['!cols'] = arr
  145. }
  146. // 将工作表对象添加到工作簿中
  147. for (let k = 0; k < header.length; k++) {
  148. wb.SheetNames.push(ws_name[k])
  149. wb.Sheets[ws_name[k]] = ws[k]
  150. }
  151. XLSX.writeFile(wb, filename + '.xlsx') // 导出文件
  152. }
  153. })
  154. .finally(() => {})
  155. }
  156. // 数额
  157. const initOption = reactive({
  158. //标题
  159. title: {
  160. text: bar_title.value || 'Total: 0', //主标题
  161. left: 19,
  162. top: 9.5,
  163. textStyle: {
  164. color: '#2B2F36',
  165. fontWeight: '700',
  166. fontFamily: 'Lato-Light',
  167. fontSize: '14px'
  168. }
  169. },
  170. // 间距
  171. grid: {
  172. top: '18%',
  173. left: '1%',
  174. right: '2%',
  175. bottom: '5%',
  176. containLabel: true // 距离包含坐标轴上的文字
  177. },
  178. // hover时的文字显示
  179. tooltip: {
  180. show: true,
  181. trigger: 'axis',
  182. axisPointer: {
  183. type: 'shadow'
  184. },
  185. backgroundColor: '#2b2f36',
  186. borderColor: '#2b2f36',
  187. formatter: function (params: any) {
  188. let str: any = ''
  189. let allnum: any = 0
  190. str += '<div style= ' + 'color:#FFF;font-family: Lato-Light>' + params[0].name + '</div>'
  191. params.forEach((item: any) => {
  192. allnum += item.value
  193. allnum = Number(allnum.toFixed(2))
  194. str +=
  195. '<div style= ' +
  196. 'color:#FFF>' +
  197. item.marker +
  198. item.seriesName +
  199. ': ' +
  200. formatNumber(item.value) +
  201. '</div>'
  202. })
  203. allnum = allnum.toFixed(2)
  204. str +=
  205. '<div style= ' +
  206. 'color:#FFF;font-family: Lato-Light>Total: ' +
  207. formatNumber(allnum) +
  208. '</div>'
  209. return str
  210. },
  211. textStyle: {
  212. color: '#FFF',
  213. fontWeight: 700,
  214. fontFamily: 'Lato-Light',
  215. fontSize: '14px'
  216. }
  217. },
  218. xAxis: {
  219. type: 'category',
  220. data: barName.value,
  221. axisTick: {
  222. show: false
  223. },
  224. axisLine: {
  225. lineStyle: {
  226. color: '#eaebed'
  227. }
  228. },
  229. axisLabel: {
  230. interval: 0,
  231. fontFamily: 'Lato-Light',
  232. color: '#B5B9BF'
  233. }
  234. },
  235. yAxis: {
  236. type: 'value',
  237. scale: true,
  238. splitLine: {
  239. show: true,
  240. lineStyle: {
  241. type: 'dashed',
  242. color: '#eaebed'
  243. }
  244. },
  245. axisLine: {
  246. show: true,
  247. lineStyle: {
  248. color: '#eaebed'
  249. }
  250. },
  251. axisLabel: {
  252. fontFamily: 'Lato-Light',
  253. color: '#B5B9BF',
  254. formatter: function (value: any) {
  255. return formatNumber(value, 0)
  256. }
  257. },
  258. min: 0, // 最小值
  259. max: Max.value, // 最大值
  260. interval: interval.value // 刻度
  261. },
  262. legend: {
  263. data: Name.value,
  264. top: '3%',
  265. itemGap: 30,
  266. left: '40%',
  267. itemHeight: 8, //修改icon图形大小
  268. itemWidth: 8, //修改icon图形大小
  269. textStyle: {
  270. fontSize: 12,
  271. fontFamily: 'Lato-Light',
  272. color: '#646A73'
  273. }
  274. },
  275. series: bar_series.value,
  276. toolbox: {
  277. iconStyle: {
  278. borderColor: '#2B2F36'
  279. },
  280. emphasis: {
  281. iconStyle: {
  282. borderColor: '#ff7500'
  283. } // hover上去时的颜色
  284. },
  285. show: isShowTooltips.value, // 显示工具箱
  286. top: 6,
  287. right: 8,
  288. feature: {
  289. saveAsImage: { show: true, name: 'Revenue Spent', type: 'png', backgroundColor: '#fff' }
  290. // myTool1: {
  291. // show: true,
  292. // title: 'update',
  293. // icon: 'image://' + updateIcon,
  294. // onclick: function () {
  295. // let filename = 'Revenue Spent Details ' + barName.value[0] + '-' + barName.value[barName.value.length - 1]
  296. // exportData({ filename: filename })
  297. // }
  298. // }
  299. },
  300. showTitle: false
  301. }
  302. })
  303. onMounted(() => {
  304. initChart()
  305. watch(
  306. () => themeStore.theme,
  307. (newVal) => {
  308. if (newVal === 'dark') {
  309. initOption.title.textStyle.color = '#fff'
  310. initOption.xAxis.axisLine.lineStyle.color = '#3F434A'
  311. initOption.xAxis.axisLine.lineStyle.color = '#3F434A'
  312. initOption.yAxis.axisLine.lineStyle.color = '#3F434A'
  313. initOption.legend.textStyle.color = 'rgba(240,241,243,0.7)'
  314. initOption.toolbox.iconStyle.borderColor = '#f0f1f3'
  315. initOption.toolbox.feature.saveAsImage.backgroundColor = '#3F434A'
  316. initChart()
  317. } else {
  318. initOption.title.textStyle.color = '#2B2F36'
  319. initOption.xAxis.axisLine.lineStyle.color = '#eaebed'
  320. initOption.yAxis.axisLine.lineStyle.color = '#eaebed'
  321. initOption.yAxis.splitLine.lineStyle.color = '#eaebed'
  322. initOption.legend.textStyle.color = '#646A73'
  323. initOption.toolbox.iconStyle.borderColor = '#2B2F36'
  324. initOption.toolbox.feature.saveAsImage.backgroundColor = '#fff'
  325. initChart()
  326. }
  327. },
  328. {
  329. immediate: true,
  330. deep: true
  331. }
  332. )
  333. })
  334. const initChart = () => {
  335. const bar_chart = echarts.init(bar_ref.value)
  336. //图表响应式
  337. window.addEventListener('resize', () => {
  338. bar_chart.resize()
  339. })
  340. bar_chart.on('legendselectchanged', function (event: any) {
  341. const selected = event.selected
  342. let trueCount = 0
  343. let trueObj: any = {}
  344. let truearr: any = []
  345. for (var key in selected) {
  346. if (selected[key]) {
  347. trueCount++
  348. trueObj[key] = selected[key]
  349. }
  350. }
  351. if (trueCount == 1) {
  352. initOption.series.forEach((item: any, index: any) => {
  353. if (item.name == Object.keys(trueObj)) {
  354. initOption.series[index].label.show = true
  355. initOption.title.text = `Total: ${formatNumber(initOption.series[index].total)}`
  356. }
  357. })
  358. } else {
  359. initOption.series.forEach((item: any, index: any) => {
  360. initOption.series[index].label.show = false
  361. initOption.title.text = `Total: `
  362. })
  363. }
  364. if (trueCount != 0) {
  365. initOption.series.forEach((item: any) => {
  366. if (Object.keys(trueObj).includes(item.name)) {
  367. truearr.push(item)
  368. }
  369. })
  370. const max = Math.max.apply(
  371. Math,
  372. truearr.map((ele: any) => {
  373. return ele.Max
  374. })
  375. )
  376. const interval = Math.max.apply(
  377. Math,
  378. truearr.map((ele: any) => {
  379. return ele.interval
  380. })
  381. )
  382. initOption.yAxis.max = max
  383. initOption.yAxis.interval = interval
  384. }
  385. bar_chart.setOption(initOption)
  386. })
  387. bar_chart.setOption(initOption)
  388. }
  389. </script>
  390. <template>
  391. <div class="com-container">
  392. <div ref="bar_ref" id="bar_chart" :style="props.barHeight"></div>
  393. </div>
  394. </template>
  395. <style lang="scss" scoped>
  396. .com-container {
  397. width: 100%;
  398. height: 100%;
  399. overflow: hidden;
  400. position: relative;
  401. }
  402. #bar_chart {
  403. width: 100%;
  404. }
  405. </style>