ChartCarouselWarp.vue 7.1 KB
Newer Older
hucy's avatar
hucy committed
1
<!--
hucy's avatar
hucy committed
2
 * 动态echar轮播图
hucy's avatar
hucy committed
3 4
  -->
<script setup lang="ts">
hucy's avatar
hucy committed
5
import { ref, reactive, onBeforeUnmount, watch, nextTick, markRaw } from 'vue';
hucy's avatar
hucy committed
6
import { isEmpty } from 'src/common/utils';
7
import * as echarts from 'echarts';
hucy's avatar
hucy committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

interface Props {
  itemKey: string;
  list: any[];
  times?: number;
  navigation?: boolean; // 小圆点点击导航
}

const props = withDefaults(defineProps<Props>(), {
  itemKey: 'id',
  list: () => [],
  times: 10000,
  navigation: false,
});

hucy's avatar
hucy committed
23
const DOM_PIE_KEY = 'chart-dom-';
24

hucy's avatar
hucy committed
25 26 27 28
const pointActive = ref<any>(1);
const lastIndex = ref(0);
const timer = ref<any>(null);

29 30 31 32 33 34
const state = reactive({
  // vue3中使用proxy的方式监听响应式,chart会被在vue内部转换成响应式对象,从而在resize 的时候获取不到
  // markRaw 标记一个对象,使其永远不会再成为响应式对象
  domMap: markRaw(new Map()),
});

hucy's avatar
hucy committed
35 36 37 38 39 40 41 42 43 44 45 46 47
watch(
  () => props.list,
  () => {
    listChange();
  }
);

onBeforeUnmount(() => {
  offTimer();
});

// list改变
function listChange() {
48
  // console.log('listChange=', props.list);
hucy's avatar
hucy committed
49 50 51 52

  offTimer(); // 清除定时器
  if (isEmpty(props.list)) {
    resetIndex();
53
    state.domMap.clear();
hucy's avatar
hucy committed
54 55 56 57 58
  } else {
    // 如果当前下标 > list长度,则下标重新开始
    if (pointActive.value > props.list.length) {
      resetIndex();
    }
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
    nextTick(() => {
      const domMap = new Map();
      props.list.map((item: any) => {
        const id = `${DOM_PIE_KEY}${item[props.itemKey]}`;
        const hasDom = state.domMap.has(id);

        const dom: any = hasDom
          ? state.domMap.get(id).dom
          : document.getElementById(id);
        const option = item.option || null;

        let myChart: any;
        const condition = pointActive.value === item[props.itemKey];
        if (hasDom) {
          if (state.domMap.get(id).myChart) {
            myChart = state.domMap.get(id).myChart;
          } else {
            if (condition) {
              myChart = echarts.init(dom);
            } else {
              myChart = null;
            }
          }
        } else {
          if (condition) {
            myChart = echarts.init(dom);
          } else {
            myChart = null;
          }
        }

        if (condition && option) {
          // console.log('设置了', myChart);
          myChart.clear();
          myChart.setOption(option);
        }
        const val = {
          dom,
          option,
          id,
          myChart,
        };
        if (state.domMap) domMap.set(id, val);
        return val;
      });

      if (state.domMap.size < domMap.size) {
        for (const [key, value] of domMap) {
          if (state.domMap.has(key)) {
            state.domMap.set(key, domMap.get(key));
          } else {
            state.domMap.set(key, value);
          }
        }
      } else if (state.domMap.size > domMap.size) {
        for (const key of state.domMap.keys()) {
          if (domMap.has(key)) {
            state.domMap.set(key, domMap.get(key));
          } else {
            state.domMap.delete(key);
          }
        }
      } else {
        for (const key of domMap.keys()) {
          state.domMap.set(key, domMap.get(key));
        }
      }
      // console.log('state.domMap =', state.domMap);
      onStartTimer();
    });
hucy's avatar
hucy committed
129 130 131 132 133 134 135 136 137 138 139 140 141
  }
}

function onStartTimer() {
  timer.value = setTimeout(() => {
    const maxLength = props.list.length;
    if (pointActive.value >= maxLength) {
      pointActive.value = 1;
      lastIndex.value = 0;
    } else {
      pointActive.value++;
      lastIndex.value++;
    }
142 143 144 145 146

    nextTick(() => {
      renderChart();
      onStartTimer();
    });
hucy's avatar
hucy committed
147 148 149 150
  }, props.times);
}

function onClickPoint(val: any, index: number) {
151
  if (index === lastIndex.value) return;
hucy's avatar
hucy committed
152 153 154 155 156 157 158 159
  offTimer();
  if (index > lastIndex.value) {
    // 右
  } else if (index < lastIndex.value) {
    // 左
  }
  pointActive.value = val[props.itemKey];
  lastIndex.value = index;
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 187 188 189 190 191 192

  nextTick(() => {
    renderChart();
    onStartTimer();
  });
}

function renderChart() {
  const key = `${DOM_PIE_KEY}${pointActive.value}`;
  const currentDom = state.domMap.get(key);
  // console.log('currentDom =', currentDom);

  if (currentDom.myChart) {
    if (currentDom.option) {
      currentDom.myChart.clear();
      currentDom.myChart.setOption(currentDom.option);
    }
  } else {
    const myChart = echarts.init(currentDom.dom);
    if (currentDom.option) {
      myChart.clear();
      myChart.setOption(currentDom.option);
    }
    const val = {
      dom: currentDom.dom,
      option: currentDom.option,
      id: currentDom.id,
      myChart,
    };
    state.domMap.set(key, val);
  }
  // console.log('domMap =', state.domMap);
  // console.log('pointActive =', pointActive.value);
hucy's avatar
hucy committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
}

function offTimer() {
  if (timer.value) {
    clearTimeout(timer.value);
    timer.value = null;
  }
}

function resetIndex() {
  pointActive.value = 1;
  lastIndex.value = 0;
}
</script>
<template>
hucy's avatar
hucy committed
208
  <div class="chart-carousel-box">
hucy's avatar
hucy committed
209 210 211 212 213 214 215 216 217 218 219
    <div class="carousel">
      <div class="carousel-content">
        <div class="carousel-item-box">
          <!-- item -->
          <template v-for="(i, index) in props.list" :key="index">
            <div
              class="item-box opacity-in"
              v-show="pointActive === i[props.itemKey]"
            >
              <!-- some item -->
              <div class="my-item-box center">
220 221 222 223
                <div
                  class="fit"
                  :id="`${DOM_PIE_KEY}${i[props.itemKey]}`"
                ></div>
hucy's avatar
hucy committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
              </div>
            </div>
          </template>
        </div>
      </div>
      <div class="carousel-points" v-if="props.navigation">
        <div
          class="point"
          :class="{ 'active-point': pointActive === i[props.itemKey] }"
          v-for="(i, index) in props.list"
          :key="index"
          @click="onClickPoint(i, index)"
        ></div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@keyframes opacity-in {
  0% {
    opacity: 0.5;
hucy's avatar
hucy committed
246
    transform: rotateY(180deg);
hucy's avatar
hucy committed
247 248 249
  }
  100% {
    opacity: 1;
hucy's avatar
hucy committed
250
    transform: rotateY(0);
hucy's avatar
hucy committed
251 252 253
  }
}

hucy's avatar
hucy committed
254
.chart-carousel-box {
hucy's avatar
hucy committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  width: 100%;
  height: 100%;
}
.carousel {
  width: 100%;
  height: 100%;
  border: 1px solid $primary;
  overflow: hidden;

  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.carousel-content {
  flex: 1;
  overflow: hidden;
}
.carousel-item-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
}
.item-box {
  width: 100%;
  height: 100%;
  overflow: auto;
  position: absolute;
284
  background-color: #fff;
hucy's avatar
hucy committed
285 286 287 288 289 290 291 292 293 294 295
  top: 0;
  left: 0;
}

.opacity-in {
  animation: opacity-in 0.45s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
}

.my-item-box {
  width: 100%;
  height: 100%;
hucy's avatar
hucy committed
296
  color: #000;
hucy's avatar
hucy committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  font-size: 40px;
}

.carousel-points {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  padding: 4px;
  padding-bottom: 0px;
}
.point {
  width: 12px;
  height: 12px;
  margin-right: 4px;
  margin-bottom: 4px;
  border-radius: 50%;
  border: 1px solid $primary;
  cursor: pointer;
  transition: all 0.5s;
}
.active-point {
  height: 10px;
  width: 30px;
  border-radius: 10px;
  background-color: $primary;
}
</style>