【7.Vue 利用Heatmap.js 制作自定义热力图】

1.效果

在这里插入图片描述

2.背景

需要根据后端检测的设备的数值显示设备周围的空气情况,用户希望用热力图的方式来显示,于是在网上找了资料,发现可以用Heatmap.js来实现。
Heatmap.js 官网:https://www.patrick-wied.at/static/heatmapjs/

3.引入组件

安装Heatmap.js

npm install Heatmap.js

Vue组件引入

import Heatmap from “heatmap.js”;

4.代码实现

一个渲染热力图的容器Div:

 <div id="div_body" style="width: 100%; height: 100%"></div>

渲染热力图

  // 获取热力图
    // 点位信息
    getHeartMap(points, maxValue) {
      var heatmapInstance = Heatmap.create({
        container: document.querySelector("#div_body"),
      });

      var data = {
        max: maxValue / 20, //max值表示数据中参数最大的一个,也可以说是热力图中颜色最深的一个,是一个标准
        // data: points
        // data: [
        //   { x: 1010, y: 600, value: 555, radius: 250 }, // radius表示区域大小
        //   { x: 700, y: 300, value: 453, radius: 380 },
        //   { x: 256, y: 600, value: 122, radius: 175 },
        //   { x: 220, y: 600, value: 888, radius: 589 },
        //   { x: 380, y: 100, value: 120, radius: 254 },
        // ],
        data: points,
      };

      heatmapInstance.setData(data);
    },

5.扩展(点击点位显示点位的详情信息)

思路

创建跟点位位置相同的透明div覆盖在点位上面,给Div创建点击事件即可

代码
 <div id="div_body" style="width: 100%; height: 100%">
  <div
        v-for="item in this.heartPointList"
        @click="cleanClick(item.id, item.x, item.y)"
        :key="item.id"
        :id="item.id"
        :style="[
          { top: item.y - item.radius / 2 + 'px' },
          { left: item.x - item.radius / 2 + 'px' },
          { width: item.radius + 'px' },
          { height: item.radius + 'px' },
          { 'z-index': '99999999999' },
          // { 'background-color': 'white' },
          { position: 'absolute' },
        ]"
      ></div>
</div>
效果图

在这里插入图片描述

注意点

只要点位的value值靠近maxvule就会变红,所以如果想让数据超过maxvlaue值才变红,可以将值先判断一下,不超过maxvalue的值可以乘以一个比例系数,让其变小。这样就不会变红色了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
1. 安装echarts ``` npm install echarts --save ``` 2. 引入echarts 在需要用到echarts的组件中引入echarts ```javascript import echarts from 'echarts' ``` 3. 创建一个div容器 在需要展示热力的组件中创建一个div容器,并设置好宽高 ```html <div id="heatmap" style="width: 100%; height: 500px;"></div> ``` 4. 初始化echarts 在组件的mounted生命周期函数中,初始化echarts,并传入div容器的id ```javascript mounted() { this.initHeatMap() }, methods: { initHeatMap() { const myChart = echarts.init(document.getElementById('heatmap')) // ... } } ``` 5. 设置热力的数据 ```javascript const data = [ [0, 0, 10], [0, 1, 20], [0, 2, 30], [1, 0, 40], [1, 1, 50], [1, 2, 60], [2, 0, 70], [2, 1, 80], [2, 2, 90] ] const option = { tooltip: {}, visualMap: { min: 0, max: 100, calculable: true, orient: 'horizontal', left: 'center', bottom: '15%' }, series: [{ type: 'heatmap', data: data }] } myChart.setOption(option) ``` 6. 完整代码 ```javascript <template> <div id="heatmap" style="width: 100%; height: 500px;"></div> </template> <script> import echarts from 'echarts' export default { mounted() { this.initHeatMap() }, methods: { initHeatMap() { const myChart = echarts.init(document.getElementById('heatmap')) const data = [ [0, 0, 10], [0, 1, 20], [0, 2, 30], [1, 0, 40], [1, 1, 50], [1, 2, 60], [2, 0, 70], [2, 1, 80], [2, 2, 90] ] const option = { tooltip: {}, visualMap: { min: 0, max: 100, calculable: true, orient: 'horizontal', left: 'center', bottom: '15%' }, series: [{ type: 'heatmap', data: data }] } myChart.setOption(option) } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码写到35岁

你的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值