dataChartEdit.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <div class="d-flex chart heightFull widthFull">
  3. <div v-if="showChart" class="chart-list heightFull overflow-y-auto mr-3">
  4. <div v-for="(chart, key) in Charts" :key="key" class="chart-type mb-3" @click="onChange(key)">
  5. <div>
  6. <span class="mdi" :class="chart.icon"></span>
  7. </div>
  8. <div>
  9. {{ chart.title }}
  10. </div>
  11. </div>
  12. </div>
  13. <div class="chart-content d-flex heightFull">
  14. <FullscreenToggle v-if="showChart" class="chart-content-show heightFull white mr-3 overflow-hidden pa-3 position-relative" ref="box">
  15. <template v-slot="{ toggle, isFullscreen }">
  16. <div class="position-absolute d-flex flex-column" style="right: 10px; top: 80px; z-index: 999">
  17. <v-btn
  18. icon
  19. color="primary"
  20. @click="toggle"
  21. >
  22. <v-icon>{{ isFullscreen ? 'mdi-arrow-collapse' : 'mdi-arrow-expand'}}</v-icon>
  23. </v-btn>
  24. <v-btn
  25. icon
  26. color="primary"
  27. class="mt-3"
  28. @click="onClose"
  29. >
  30. <v-icon>mdi-close</v-icon>
  31. </v-btn>
  32. </div>
  33. <InitChart ref="chart" class="heightFull widthFull"></InitChart>
  34. </template>
  35. </FullscreenToggle>
  36. <DataChartEditChat :class="showChart ? 'widthHalf' : 'widthFull'" @render="onRender"></DataChartEditChat>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. // 属性模块
  42. import * as Charts from './utils/options.js'
  43. import DataChartEditChat from './dataChartEditChat.vue'
  44. import InitChart from '@/charts/initChart'
  45. import { cloneDeep } from 'lodash'
  46. import FullscreenToggle from '@/components/FullscreenToggle'
  47. export default {
  48. name: 'dataChartEdit',
  49. components: {
  50. DataChartEditChat,
  51. InitChart,
  52. FullscreenToggle
  53. },
  54. data () {
  55. return {
  56. showChart: false,
  57. Charts,
  58. chart: null,
  59. chartsOpt: {
  60. data: [[]],
  61. config: {
  62. xAxisData: []
  63. },
  64. key: null
  65. }
  66. }
  67. },
  68. methods: {
  69. onClose () {
  70. this.showChart = false
  71. this.chart = null
  72. },
  73. onChange (key) {
  74. this.chartsOpt.key = key
  75. this.setData()
  76. },
  77. onRender ({ type, data }) {
  78. this.showChart = true
  79. this.$nextTick(() => {
  80. if (!this.chart) {
  81. this.chart = this.$refs.chart.init()
  82. }
  83. this.chartsOpt.config.xAxisData = type
  84. this.chartsOpt.data = data
  85. this.chartsOpt.key = this.chartsOpt.key || 'bar'
  86. this.setData()
  87. })
  88. },
  89. setData () {
  90. const { data, key, config } = this.chartsOpt
  91. this.chart.showLoading()
  92. // 根据key值处理data
  93. const _option = cloneDeep(Charts[key].option)
  94. const series = _option.series
  95. const _data = []
  96. if (key === 'pie') {
  97. (data || [[]]).forEach(e => {
  98. _data.push(e.map((_e, i) => {
  99. return {
  100. value: _e,
  101. name: config.xAxisData[i] ?? _e
  102. }
  103. }))
  104. })
  105. } else {
  106. _data.push(...data)
  107. }
  108. const _tem = series[0]
  109. const { data: dataSource, ...opt } = _tem
  110. data.forEach((d, i) => {
  111. series[i] = {
  112. data: _data[i],
  113. ...opt
  114. }
  115. })
  116. if (_option.xAxis?.data) {
  117. _option.xAxis.data = config?.xAxisData ?? data[0].map((e, i) => i)
  118. }
  119. this.chart.setOption(_option, true)
  120. this.chart.hideLoading()
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang="scss" scoped>
  126. .heightFull {
  127. height: 100%;
  128. }
  129. .widthFull {
  130. width: 100%;
  131. }
  132. .widthHalf {
  133. width: 50%;
  134. }
  135. .chart {
  136. &-list {
  137. // width: 100px;
  138. .chart-type {
  139. width: 100%;
  140. height: 60px;
  141. box-sizing: border-box;
  142. display: flex;
  143. flex-direction: column;
  144. justify-content: center;
  145. align-items: center;
  146. padding: 10px;
  147. border: 1px solid #ccc;
  148. border-radius: 5px;
  149. cursor: pointer;
  150. .mdi {
  151. font-size: 24px;
  152. }
  153. &:hover {
  154. background-color: #f5f5f5;
  155. }
  156. }
  157. }
  158. &-content {
  159. width: 0;
  160. flex: 1;
  161. &-show {
  162. width: 50%;
  163. border: 1px solid #ccc !important;
  164. }
  165. }
  166. }
  167. .position {
  168. &-relative {
  169. position: relative;
  170. }
  171. &-absolute {
  172. position: absolute;
  173. }
  174. }
  175. </style>