vue+heatmap.js实现进店客流热力图显示

heatmapjs插件地址:heatmapjs  

使用vue实现热力图,切换不同日期之后,会出现多个canvas覆盖情况;

我的解决方案是:遍历dom元素,删除之前的canvas元素 。

点击放大旋转90度展示

//获取dom元素 
    const heatmapBox = val === 'show' ? document.getElementById("heatmap1") :                 
    document.getElementById("heatmap");

    const oldCanvas = heatmapBox.children[2];
//删除之前的节点,在重新渲染之前需要判断是否存在
    if (oldCanvas !== undefined) {
       heatmapBox.removeChild(heatmapBox.children[1]);
    }

手机端展示,点击热力图全屏显示,图片旋转坐标计算。 初始热力图坐标(0,0)位于左上角。

<template>
  <div class="passenger__heatMpa">
    <div id="heatmap" style="width: 100%;" @click="Overlay">
      <img :height="heightNum" id="baidu-img" src="@/assets/img/heatMap.jpg" />
    </div>
    <van-overlay :show="show" @click="showOverlay" z-index="9999999">
      <div class="wrapper" >
        <div
          class="block"
          id="heatmap1"
          :style="{width: `${screenHeight}px`}"
           style="transform: rotate(90deg);"
        >
          <img :width="`${screenHeight}px`" :style="{minWidth: `${screenHeight}px`}"  src="@/assets/img/heatMap.jpg" />
        </div>
      </div>
    </van-overlay>
  </div>
</template>
<script>
import Heatmap from "heatmap.js";
import { TourService } from "@/api/tour-service";
import { formatDate } from "@/utils/format-data";
import { Overlay } from "vant";
export default {
  name: "passengerHeatMpa",
  components: {
    [Overlay.name]: Overlay
  },
  data() {
    return {
      show: false,
      heightNum: Math.ceil(720 / (1280 / this.screenWidth).toFixed(2)),
      xAxis: [],
      listX: [],
      listY: [],
      screenWidth: document.body.clientWidth - 20,
      screenHeight: document.body.clientHeight - 20
    };
  },
  methods: {
    Overlay() {
      this.show = true;
      this.initChart('show');
    },
    showOverlay() {
      this.show = false;
    },
    //业务数据请求
    async getData(val) {
      this.listX = [];
      this.listY = [];
      let { records } = await this.service.getPerLocation({
        orgSeq: val.orgSeq,
        optionTime: `${formatDate(
          new Date(val.currentDate),
          "yyyy-MM-dd 00:00:00"
        )}`
      });
      //遍历数据
      if (records) {
        records.map(item => {
          item.personxList.map(s => {
            this.listX.push(s);
          });
        });
        records.map(item => {
          item.personyList.map(s => {
            this.listY.push(s);
          });
        });
      }
      this.initChart();
    },
    initChart(val = 'default') {
      console.log(val)
      // 创建一个heatmap实例对象
      this.$nextTick(() => {
        const heatmapBox = val === 'show' ? document.getElementById("heatmap1") : document.getElementById("heatmap");
        //计算图片缩放比例
        let widthNum = val === 'show' ? (1280 / this.screenHeight).toFixed(2) : (1280 / this.screenWidth).toFixed(2);
        const heatmapInstance = Heatmap.create({
          container: heatmapBox,
           //根据图片大小设置不同的热力大小
          radius: val === 'show' ? Math.ceil(this.screenHeight / 33) : Math.ceil(this.screenWidth / 33),
          maxOpacity: 0.5,
          minOpacity: 0,
          blur: 0.75
        });
        const oldCanvas = heatmapBox.children[2];
        // //删除之前生成的热力图
        if (oldCanvas !== undefined) {
          //删除之前的节点
          heatmapBox.removeChild(heatmapBox.children[1]);
        }
        let points = [];
        this.listX.map((item, index) => {
          let point = {
            //根据屏幕分辨率,相对应的坐标,旋转之后坐标(0,0)位于右上角
            x:  Math.ceil(item / widthNum), 
            y:  Math.ceil(this.listY[index] / widthNum),
            value: 10
          };
          points.push(point);
        });
        let data = {
          max: 100,
          min: 0,
          data: points
        };
        // 因为data是一组数据,直接setData
        heatmapInstance.setData(data);
      });
    }
  },
  mounted() {},
  created() {
    this.service = new TourService();
  }
};
</script>
<style lang="less" scoped>
.passenger__heatMpa {
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    border: none;
  }
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值