legend.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="legend-container">
  3. <div class="legend-big">
  4. <div class="color-bar-big" v-if="background" :style="{ background: background }"></div>
  5. </div>
  6. <div class="legend">
  7. <div class="color-bar" v-if="background" :style="{ background: background }"></div>
  8. <div class="labels">
  9. <div
  10. class="level"
  11. v-for="(level, index) in levels"
  12. :key="index"
  13. :style="{ left: `${(index / (levels.length - 1)) * 100}%` }"
  14. >
  15. <div class="tick"></div> <!-- 添加刻度 -->
  16. <div class="label">{{ level }}</div> <!-- 将标签内容放入一个新的div中 -->
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <script setup>
  23. import eventBus from "@/api/eventBus";
  24. import {ref} from "vue";
  25. let colorsRef = ref([]);
  26. let levels = ref([]);
  27. let background = ref("");
  28. let legendImg = ref(null);
  29. function gradientListener() {
  30. const colorStops = colorsRef.value.map((color, index) => {
  31. const percentage = (index / (colorsRef.value.length - 1)) * 100;
  32. return `${color} ${percentage}%`;
  33. });
  34. background.value = `linear-gradient(to right, ${colorStops.join(", ")})`;
  35. console.log(background.value)
  36. }
  37. function updateColors({colors,labels,name, legendUrl}) {
  38. if(legendUrl){
  39. legendImg.value = legendUrl
  40. return
  41. }
  42. legendImg.value = null
  43. if(colors === undefined || colors.length === 0) return;
  44. colorsRef.value = colors;
  45. levels.value = colors.map((color, index) => {
  46. return labels[index];
  47. });
  48. gradientListener()
  49. }
  50. eventBus.off("alarmList:changeMapLayer", updateColors)
  51. eventBus.on("alarmList:changeMapLayer", updateColors)
  52. </script>
  53. <style lang="scss" scoped>
  54. .legend-container {
  55. display: flex;
  56. flex-direction: column;
  57. align-items: center;background: #fff;
  58. padding: 10px 10px 30px 10px;
  59. opacity: 0.6;
  60. }
  61. .legend-big{
  62. width: 100%;
  63. }
  64. .legend {
  65. margin-top: 10px;
  66. width: 50%;
  67. }
  68. .color-bar-big{
  69. width: 100%;
  70. height: 30px;
  71. }
  72. .color-bar {
  73. width: 100%;
  74. height: 10px;
  75. border: 1px solid #000;
  76. }
  77. .labels {
  78. position: relative;
  79. margin-top: 5px;
  80. .level {
  81. position: absolute;
  82. top: 50%; /* 将水平标签居中 */
  83. transform: translateY(-50%);
  84. width: 2px; /* 设置刻度条的宽度 */
  85. height: 100%;
  86. color: #000;
  87. font-size: 12px;
  88. }
  89. .tick {
  90. position: absolute;
  91. top: -5px; /* 调整刻度的位置,使其位于标签上方 */
  92. height: 10px; /* 刻度的高度 */
  93. width: 2px; /* 刻度的宽度 */
  94. background-color: black; /* 刻度的颜色 */
  95. }
  96. .label {
  97. position: absolute;
  98. top: 10px; /* 调整标签的位置,使其位于刻度下方 */
  99. left: 0px; /* 通过调整这个值来使标签居中于刻度 */
  100. transform: translateX(-50%);
  101. white-space: nowrap;
  102. }
  103. }
  104. </style>