ChartDataEditor.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <div class="chart-data-editor">
  3. <div class="editor-content">
  4. <div class="range-box">
  5. <div
  6. class="temp-range"
  7. :style="{
  8. width: tempRangeSize.width + 'px',
  9. height: tempRangeSize.height + 'px',
  10. }"
  11. ></div>
  12. <div
  13. :class="['range-line', line.type]"
  14. v-for="line in rangeLines"
  15. :key="line.type"
  16. :style="line.style"
  17. ></div>
  18. <div
  19. class="resizable"
  20. :style="resizablePointStyle"
  21. @mousedown.stop="changeSelectRange($event)"
  22. ></div>
  23. </div>
  24. <table>
  25. <tbody>
  26. <tr v-for="rowIndex in 30" :key="rowIndex">
  27. <td v-for="colIndex in 7" :key="colIndex" :class="{ 'head': colIndex === 1 && rowIndex <= selectedRange[1] }">
  28. <input
  29. :class="['item', { 'selected': rowIndex <= selectedRange[1] && colIndex <= selectedRange[0] }]"
  30. :id="`cell-${rowIndex - 1}-${colIndex - 1}`"
  31. autocomplete="off"
  32. >
  33. </td>
  34. </tr>
  35. </tbody>
  36. </table>
  37. </div>
  38. <div class="btns">
  39. <Button class="btn" @click="closeEditor()">取消</Button>
  40. <Button type="primary" class="btn" @click="getTableData()">确认</Button>
  41. </div>
  42. </div>
  43. </template>
  44. <script lang="ts">
  45. import { ChartData } from '@/types/slides'
  46. import { computed, defineComponent, onMounted, PropType, ref } from 'vue'
  47. const CELL_WIDTH = 100
  48. const CELL_HEIGHT = 32
  49. export default defineComponent({
  50. name: 'chart-data-editor',
  51. props: {
  52. data: {
  53. type: Object as PropType<ChartData>,
  54. required: true,
  55. }
  56. },
  57. setup(props, { emit }) {
  58. const selectedRange = ref([0, 0])
  59. const tempRangeSize = ref({ width: 0, height: 0 })
  60. // 当前选区的边框线条位置
  61. const rangeLines = computed(() => {
  62. const width = selectedRange.value[0] * CELL_WIDTH
  63. const height = selectedRange.value[1] * CELL_HEIGHT
  64. return [
  65. { type: 't', style: {width: width + 'px'} },
  66. { type: 'b', style: {top: height + 'px', width: width + 'px'} },
  67. { type: 'l', style: {height: height + 'px'} },
  68. { type: 'r', style: {left: width + 'px', height: height + 'px'} },
  69. ]
  70. })
  71. // 当前选区的缩放点位置
  72. const resizablePointStyle = computed(() => {
  73. const width = selectedRange.value[0] * CELL_WIDTH
  74. const height = selectedRange.value[1] * CELL_HEIGHT
  75. return { left: width + 'px', top: height + 'px' }
  76. })
  77. // 初始化图表数据:将数据格式化并填充到DOM
  78. const initData = () => {
  79. const _data: string[][] = []
  80. const { labels, series } = props.data
  81. const rowCount = labels.length
  82. const colCount = series.length
  83. for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  84. const row = [labels[rowIndex]]
  85. for (let colIndex = 0; colIndex < colCount; colIndex++) {
  86. row.push(series[colIndex][rowIndex] + '')
  87. }
  88. _data.push(row)
  89. }
  90. for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  91. for (let colIndex = 0; colIndex < colCount + 1; colIndex++) {
  92. const inputRef = document.querySelector(`#cell-${rowIndex}-${colIndex}`) as HTMLInputElement
  93. if (!inputRef) continue
  94. inputRef.value = _data[rowIndex][colIndex] + ''
  95. }
  96. }
  97. selectedRange.value = [colCount + 1, rowCount]
  98. }
  99. onMounted(initData)
  100. // 获取当前图表DOM中的数据,整理格式化后传递出去
  101. const getTableData = () => {
  102. const [col, row] = selectedRange.value
  103. const labels: string[] = []
  104. const series: number[][] = []
  105. // 第一列为系列名,实际数据从第二列开始
  106. for (let rowIndex = 0; rowIndex < row; rowIndex++) {
  107. let labelsItem = `类别${rowIndex + 1}`
  108. const labelInputRef = document.querySelector(`#cell-${rowIndex}-0`) as HTMLInputElement
  109. if (labelInputRef && labelInputRef.value) labelsItem = labelInputRef.value
  110. labels.push(labelsItem)
  111. }
  112. for (let colIndex = 1; colIndex < col; colIndex++) {
  113. const seriesItem = []
  114. for (let rowIndex = 0; rowIndex < row; rowIndex++) {
  115. const valueInputRef = document.querySelector(`#cell-${rowIndex}-${colIndex}`) as HTMLInputElement
  116. let value = 0
  117. if (valueInputRef && valueInputRef.value && !!(+valueInputRef.value)) {
  118. value = +valueInputRef.value
  119. }
  120. seriesItem.push(value)
  121. }
  122. series.push(seriesItem)
  123. }
  124. const data = { labels, series }
  125. emit('save', data)
  126. }
  127. // 关闭图表数据编辑器
  128. const closeEditor = () => emit('close')
  129. // 鼠标拖拽修改选中的数据范围
  130. const changeSelectRange = (e: MouseEvent) => {
  131. let isMouseDown = true
  132. const startPageX = e.pageX
  133. const startPageY = e.pageY
  134. const originWidth = selectedRange.value[0] * CELL_WIDTH
  135. const originHeight = selectedRange.value[1] * CELL_HEIGHT
  136. document.onmousemove = e => {
  137. if (!isMouseDown) return
  138. const currentPageX = e.pageX
  139. const currentPageY = e.pageY
  140. const x = currentPageX - startPageX
  141. const y = currentPageY - startPageY
  142. const width = originWidth + x
  143. const height = originHeight + y
  144. tempRangeSize.value = { width, height }
  145. }
  146. document.onmouseup = e => {
  147. isMouseDown = false
  148. document.onmousemove = null
  149. document.onmouseup = null
  150. const endPageX = e.pageX
  151. const endPageY = e.pageY
  152. if (startPageX === endPageX && startPageY === endPageY) return
  153. // 拖拽结束时,范围超过格子一半自动扩大到下一格(如拖动到一格半多的位置,会自动扩展到两格,横竖都同理)
  154. let width = tempRangeSize.value.width
  155. let height = tempRangeSize.value.height
  156. if (width % CELL_WIDTH > CELL_WIDTH * 0.5) width = width + (CELL_WIDTH - width % CELL_WIDTH)
  157. if (height % CELL_HEIGHT > CELL_HEIGHT * 0.5) height = height + (CELL_HEIGHT - height % CELL_HEIGHT)
  158. let row = Math.round(height / CELL_HEIGHT)
  159. let col = Math.round(width / CELL_WIDTH)
  160. if (row < 3) row = 3
  161. if (col < 2) col = 2
  162. selectedRange.value = [col, row]
  163. tempRangeSize.value = { width: 0, height: 0 }
  164. }
  165. }
  166. return {
  167. tempRangeSize,
  168. rangeLines,
  169. resizablePointStyle,
  170. changeSelectRange,
  171. selectedRange,
  172. getTableData,
  173. closeEditor,
  174. }
  175. },
  176. })
  177. </script>
  178. <style lang="scss" scoped>
  179. .chart-data-editor {
  180. width: 600px;
  181. position: relative;
  182. }
  183. .editor-content {
  184. width: 100%;
  185. height: 360px;
  186. overflow: overlay;
  187. position: relative;
  188. border-right: 1px solid #ccc;
  189. border-bottom: 1px solid #ccc;
  190. }
  191. .range-box {
  192. position: absolute;
  193. top: 0;
  194. left: 0;
  195. z-index: 100;
  196. user-select: none;
  197. }
  198. .temp-range {
  199. position: absolute;
  200. top: 0;
  201. left: 0;
  202. width: 0;
  203. height: 0;
  204. background-color: rgba($color: #888, $alpha: .3);
  205. }
  206. .range-line {
  207. position: absolute;
  208. width: 0;
  209. height: 0;
  210. left: 0;
  211. top: 0;
  212. border: 0 solid $themeColor;
  213. &.t {
  214. border-top-width: 1px;
  215. }
  216. &.b {
  217. border-bottom-width: 1px;
  218. }
  219. &.l {
  220. border-left-width: 1px;
  221. }
  222. &.r {
  223. border-right-width: 1px;
  224. }
  225. }
  226. .resizable {
  227. position: absolute;
  228. width: 12px;
  229. height: 12px;
  230. left: 0;
  231. top: 0;
  232. margin: -9px 0 0 -9px;
  233. cursor: nwse-resize;
  234. &::after {
  235. content: '';
  236. position: absolute;
  237. width: 4px;
  238. height: 12px;
  239. right: 0;
  240. top: 0;
  241. background-color: $themeColor;
  242. }
  243. &::before {
  244. content: '';
  245. position: absolute;
  246. width: 12px;
  247. height: 4px;
  248. right: 0;
  249. bottom: 0;
  250. background-color: $themeColor;
  251. }
  252. }
  253. table {
  254. width: 100%;
  255. height: 100%;
  256. user-select: none;
  257. table-layout: fixed;
  258. td {
  259. text-align: center;
  260. border: 1px solid #ccc;
  261. vertical-align: middle;
  262. width: 100px;
  263. height: 32px;
  264. &.head {
  265. background-color: rgba($color: $themeColor, $alpha: .1);
  266. }
  267. }
  268. .item {
  269. width: 100%;
  270. height: 100%;
  271. border: 0;
  272. outline: 0;
  273. font-size: 13px;
  274. text-align: center;
  275. background-color: transparent;
  276. &.selected {
  277. background-color: rgba($color: $themeColor, $alpha: .02);
  278. }
  279. }
  280. }
  281. .btns {
  282. margin-top: 10px;
  283. text-align: right;
  284. .btn {
  285. margin-left: 10px;
  286. }
  287. }
  288. </style>