line.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <div>
  3. <base-chart
  4. :id="this.id"
  5. :chartType="chartType"
  6. :chartData="chartData"
  7. :chartConfig="chartConfig"
  8. :chartSettings="chartSettings"
  9. :chartExtend="chartExtends"
  10. :loading="loading"
  11. :chartEvents="chartEvents"
  12. :extraToolbox="extraToolbox" />
  13. </div>
  14. </template>
  15. <script>
  16. import * as R from 'ramda'
  17. import XLSX from 'xlsx'
  18. import commonChartProps from './common'
  19. // eslint-disable-next-line no-unused-vars
  20. import numerify from './formatters'
  21. export default {
  22. name: 'OverviewLine',
  23. props: Object.assign({
  24. id: {
  25. type: String,
  26. default: 'overview-line',
  27. },
  28. isHistogram: {
  29. type: String,
  30. default: false,
  31. },
  32. loading: {
  33. type: Boolean,
  34. },
  35. exportName: String,
  36. }, commonChartProps()),
  37. computed: {
  38. excelColumnMap () {
  39. const columnMap = {}
  40. this.chartData && this.chartData.columns.map(item => {
  41. columnMap[item] = { field: item }
  42. })
  43. return columnMap
  44. },
  45. exportExcelData () {
  46. const { columns, rows } = this.chartData
  47. if (columns.length > 1) {
  48. const resourceNames = columns.slice(1)
  49. const times = rows.map(item => item.time)
  50. const titles = ['resource_name', ...times]
  51. const data = [titles]
  52. resourceNames.map(name => {
  53. const row = [name]
  54. times.map((time) => {
  55. const targets = rows.filter(item => item.time === time)
  56. if (targets[0]) {
  57. row.push(targets[0][name])
  58. } else {
  59. row.push('')
  60. }
  61. })
  62. data.push(row)
  63. })
  64. return data
  65. }
  66. return []
  67. },
  68. extraToolbox () {
  69. return {
  70. pdf: {
  71. name: this.exportName || this.$t('monitor.overview_alert_trend'),
  72. target: `#${this.id}`,
  73. },
  74. excel: {
  75. export: this.exportExcel,
  76. },
  77. }
  78. },
  79. predictExcelData () {
  80. if (this.seriesArr.length) {
  81. const dataList = []
  82. const dataMap = {}
  83. this.seriesArr.map(item => {
  84. if (!dataMap[item.time]) {
  85. dataMap[item.time] = R.clone(item)
  86. } else {
  87. if (!dataMap[item.time].predict) {
  88. dataMap[item.time].predict = item.predict
  89. }
  90. }
  91. })
  92. for (const key in dataMap) {
  93. dataList.push(dataMap[key])
  94. }
  95. dataList.sort((a, b) => {
  96. return a.time - b.time
  97. })
  98. return dataList
  99. }
  100. return []
  101. },
  102. chartType () {
  103. return this.isHistogram ? 've-histogram' : 've-line'
  104. },
  105. chartExtends () {
  106. /* 当属性为对象时,如果在options中对应的属性为对象(eg: tooltip)或包含对象的数组(eg: series)
  107. * 对应的配置会被合并,否则将直接覆盖对应的配置
  108. * **/
  109. const ret = {
  110. series (v) {
  111. /* smooth数据平滑, connectNulls 是否连接空数据 **/
  112. return v ? v.map(i => { i.smooth = true; i.connectNulls = true; return i }) : []
  113. },
  114. tooltip: {
  115. trigger: 'axis',
  116. axisPointer: {
  117. type: 'shadow',
  118. shadowStyle: { color: 'rgb(77, 161, 255)', opacity: 0.1 },
  119. },
  120. },
  121. grid: {
  122. bottom: '10px',
  123. top: '50px',
  124. },
  125. // dataZoom: {
  126. // type: 'inside', /* 数据缩放 **/
  127. // },
  128. }
  129. return Object.assign(ret, this.chartExtend)
  130. },
  131. chartConfig () {
  132. /* 在这里配置属性,会导致原配置被覆盖,而不是被合并 */
  133. return {
  134. height: this.chartHeigth,
  135. width: '100%',
  136. legend: { show: this.showLegend },
  137. toolbox: {
  138. show: true,
  139. feature: {
  140. magicType: {
  141. type: ['line', 'bar'],
  142. title: { line: this.$t('monitor.chart.toolbar.line'), bar: this.$t('monitor.chart.toolbar.bar') },
  143. },
  144. },
  145. },
  146. }
  147. },
  148. chartSettings () {
  149. const cs = {}
  150. if (this.chartData && this.chartData.columns && this.chartData.columns.length > 0) {
  151. cs.yAxisType = [this.yAxisFormat]
  152. }
  153. return Object.assign(cs, this.chartSetting)
  154. },
  155. },
  156. methods: {
  157. exportExcel () {
  158. const filename = `${this.exportName || this.$t('monitor.overview_alert_trend')}.xlsx`
  159. const ws_name = 'Sheet1'
  160. const wb = XLSX.utils.book_new()
  161. const ws = XLSX.utils.aoa_to_sheet(this.exportExcelData)
  162. XLSX.utils.book_append_sheet(wb, ws, ws_name)
  163. XLSX.writeFile(wb, filename)
  164. },
  165. },
  166. }
  167. </script>