wangrong
2026-08-30 4c949c0dc9bcae785c281c9d8125295f09810b15
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
<script setup lang="ts">
import { computed, ref } from 'vue'
import { dvMainStoreWithOut } from '@/store/modules/data-visualization/dvMain'
import { storeToRefs } from 'pinia'
const dvMainStore = dvMainStoreWithOut()
const wRuleRef = ref(null)
const props = defineProps({
  tickLabelFormatter: {
    type: Function,
    default: value => value.toString() // 刻度标签格式化函数,默认直接转为字符串
  },
  direction: {
    type: String,
    default: 'horizontal' // 尺子方向
  }
})
 
const labelInterval = 5
 
const { canvasStyleData } = storeToRefs(dvMainStore)
 
const ticks = computed(() => {
  const result = []
  let currentValue = 0
  while (currentValue <= canvasStyleData.value.height) {
    const isLong = currentValue % (labelInterval * tickSize.value) === 0
    const label = isLong ? props.tickLabelFormatter(currentValue) : ''
    result.push({ position: (currentValue * canvasStyleData.value.scale) / 100, label, isLong })
    currentValue += tickSize.value
  }
  return result
})
 
const hStyle = computed(() => {
  return {
    height: canvasStyleData.value.height * 1.5 + 'px'
  }
})
const tickSize = computed(
  () =>
    10 *
    Math.max(Math.floor(200000 / (canvasStyleData.value.height * canvasStyleData.value.scale)), 1)
)
 
const scaleHeight = computed(
  () => (canvasStyleData.value.height * canvasStyleData.value.scale) / 100
)
 
const rulerScroll = e => {
  wRuleRef.value.scrollTo(0, e.scrollHeight)
}
 
defineExpose({
  rulerScroll
})
</script>
 
<template>
  <div class="ruler-outer-vertical" ref="wRuleRef">
    testtest
    <!--覆盖着尺子上方防止鼠标移到尺子位置滑动-->
    <div class="ruler-shadow-vertical"></div>
    <div :style="hStyle" class="ruler-outer-vertical-scroll">
      <div class="ruler" :style="{ height: `${scaleHeight}px` }">
        <div class="ruler-line" :style="{ height: `${scaleHeight}px` }"></div>
        <div
          v-for="(tick, index) in ticks"
          :key="index"
          class="ruler-tick"
          :class="{ 'long-tick': tick.isLong }"
          :style="{ left: `${tick.position}px` }"
        >
          <span v-if="tick.isLong" class="tick-label">{{ tick.label }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
 
<style scoped lang="less">
.ruler-shadow-vertical {
  position: absolute;
  width: 30px;
  height: 100%;
  z-index: 10;
  overflow: hidden;
}
 
.ruler-outer-vertical {
  position: absolute;
  width: 30px;
  height: 100%;
  overflow-y: auto;
  background-color: #2c2c2c;
  &::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
  }
}
 
.ruler-outer-vertical-scroll {
  display: flex;
  align-items: center;
  justify-content: center;
}
.ruler {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  height: 100%;
  border-left: 1px solid #974e4e;
  background-color: #2c2c2c;
}
 
.ruler-line {
  position: absolute;
  bottom: 0;
  height: 1px;
  background-color: #ac2a2a;
}
 
.ruler-tick {
  position: absolute;
  bottom: 1px;
  height: 3px;
  width: 1px;
  background-color: #e38a8a;
}
 
.long-tick {
  width: 1px;
  height: 15px;
}
 
.tick-label {
  position: absolute;
  bottom: 2px;
  font-size: 8px;
  left: 50%;
  transform: translateX(2%);
  white-space: nowrap;
}
</style>