dataChartEdit.vue 3.9 KB

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