小小儁爺
2025-03-06 e9a02d180b91ee7a383d25d5e19853630026b4b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<template>
  <div
    class="gantt-timeline"
    :style="{'margin-left':-cellWidth/2+'px'}"
  >
    <div
      v-for="(day,index) in getDays"
      :key="index"
      class="gantt-timeline-block"
      :style="{width:getTimeScales(day).length*cellWidth+'px'}"
    >
      <div
        class="gantt-timeline-day "
        :style="heightStyle"
 
      >
        {{ day.format('YYYY-MM-DD') }}
        {{ getDayMy(day.format('YYYY-MM-DD')) }}
      </div>
      <div
        class="gantt-timeline-scale"
        :style="heightStyle"
 
 
      >
<!--        style="border-right: 1px solid #eee"-->
        <div
          v-for="(hour,ind) in getTimeScales(day)"
          :key="ind"
          :style="cellWidthStyle"
 
 
        >
<!--          style="border-right: 1px solid #eee;"-->
          {{ hour }}
          <!-- <span v-if="hour !='01' ">{{hour}}</span> -->
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import dayjs from 'dayjs'
import { getBeginTimeOfTimeLine } from '../../utils/timeLineUtils.js'
 
const START_DAY = Symbol()
const MIDDLE_DAY = Symbol()
const END_DAY = Symbol()
 
function isSameDay(one, two) {
  return one.isSame(two, 'day')
}
 
export default {
  name: 'Timeline',
 
  props: {
    start: {
      type: dayjs
    },
    end: {
      type: dayjs
    },
    cellWidth: {
      type: Number
    },
    titleHeight: {
      type: Number
    },
    scale: {
      type: Number
    }
  },
 
  computed: {
    /**
     * 天列表
     * @returns {[dayjs]} 该data中所有需要渲染的数据
     */
    getDays() {
      const temp = []
      let { start, end } = this
 
      for (; !isSameDay(start, end); start = start.add(1, 'day')) {
        temp.push(start)
      }
      temp.push(start)
 
      return temp
    },
    cellWidthStyle() {
      return {
        width: `${this.cellWidth}px`,
 
        // width: `${this.cellWidth/2}px`,
 
 
        // marginLeft:`${this.cellWidth/2}px`
      }
    },
    heightStyle() {
      return {
        height: this.titleHeight + 'px',
        'line-height': this.titleHeight + 'px'
      }
    }
  },
 
  methods: {
    // 算出星期
    getDayMy(day) {
      const weekArray = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六')
      return weekArray[new Date(day).getDay()]
    },
    /**
     * 获取时间刻度数组
     *
     * @param {dayjs} date
     * @returns {[string]} 该data中所有需要渲染的数据
     */
    getTimeScales(date) {
      const { start, end } = this
      if (isSameDay(date, start)) {
        return this.generateTimeScale(START_DAY)
      } else if (isSameDay(date, end)) {
        return this.generateTimeScale(END_DAY)
      } else {
        return this.generateTimeScale(MIDDLE_DAY)
      }
    },
    /**
     * 生成时间刻度数组
     *
     * @param {Symbol} type
     * @returns {[string]} 该data中所有需要渲染的数据
     */
    generateTimeScale(type) {
      const totalblock = []
      const { start, end, scale } = this
      let a, b
      switch (type) {
        case START_DAY: // 和start同一天
          a = getBeginTimeOfTimeLine(start, scale)
          // start和end同一天特殊处理
          if (isSameDay(start, end)) {
            b = end
          } else {
            b = start.endOf('day')
          }
          break
        case END_DAY: // 和end 同一天
          a = end.startOf('day')
          b = end
          break
        case MIDDLE_DAY: // start和end中间的天
          a = start.startOf('day')
          b = start.endOf('day')
          break
        default:
          throw new TypeError('错误的计算类型')
      }
      while (!a.isAfter(b)) {
        // if (scale >= 60) {
        //   totalblock.push(a.format('HH'))
        // } else {
        totalblock.push(a.format('HH:mm'))
        // }
        a = a.add(scale, 'minute')
      }
      // console.log(1, totalblock)
      return totalblock
    }
  }
}
</script>
<style lang="scss" scoped>
.gantt-timeline-day {
  //border-right: 1px solid #ebeef5;
  border-bottom: 1px solid #ebeef5;
  //background: rgb(245, 245, 245);
  background: #fff;
  color: #909399;
  font-size: 14px;
}
</style>