Chinaunix首页 | 论坛 | 博客
  • 博客访问: 82268
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 355
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-15 10:32
文章分类
文章存档

2020年(15)

2019年(19)

我的朋友

分类: JavaScript

2020-03-02 00:30:40

前言

数据蕴藏价值,但数据的价值需要用IT 技术去发现、探索,可视化可以帮助人更好的去分析数据,信息的质量很大程度上依赖于其呈现方式。在数据分析上,热力图无疑是一种很好的方式。在很多行业中都有着广泛的应用。

最近刚好项目中需要用到3D 热力图的效果展示。网上搜了相关资料,发现大多数是2D 效果或者伪3D 的,而3D 粒子效果对于性能上的体验不是很好,于是取巧写了个3D 热力图的效果 。

部分效果图:



应用场景


大楼内的人员分布热力图。我们可以通过观察到一个区域的颜色深浅来判断该区域内实时的人员流动情况,知道哪个区域人多,哪个区域人少。该场景可适用于大楼内的警务监控,在发生突发事件时科学高效地制定分流疏导策略提供有力的帮助和支持,减少损失。亦可用于火险预警,监控区域实时温度。



室内设备温度热力图。传统的数据中心汇报方式枯燥单调、真实感不强,互动性差等,借助于3D热力图的可视化呈现方式,机房运维管理人员可大大提高工作效率及降低工作失误的可能性。

整体思路

在场景反序列化之后,设置热力图的初始参数,初始化后得到的热力图模型添加进场景中,模拟3D热力图效果,最后再添加扫描、换肤、温度提示等功能。

1.数据准备

在场景中画出热力图的区域,如图

首先确定要生成热力图的区域 areaNode,然后随机生成20  个点的信息,包含坐标position(坐标是相对红色长方体的某个顶点) 及热力值temperature 。

以下是该部分的主要代码:

  1. function getTemplateList(areaNode, hot, num) {
  2.     let heatRect = areaNode.getRect();
  3.     let { width, height } = heatRect;
  4.     let rackTall = areaNode.getTall();
  5.     hot = hot + this.random(20);
  6.     let templateList = [];
  7.     for (let i = 0; i < num; i++) {
  8.         templateList.push({
  9.             position: {
  10.                 x: 0.2 * width + this.random(0.6 * width),
  11.                 y: 0.2 * height + this.random(0.6 * height),
  12.                 z: 0.1 * rackTall + this.random(0.8 * rackTall)
  13.             },
  14.             temperature: hot
  15.         });
  16.     }
  17.     return templateList;
  18. }
  19. let heatMapArea_1 = dm.getDataByTag('heatMapArea_1');
  20. let templateList_1 = this.getTemplateList(
  21.     heatMapArea_1,
  22.     70,
  23. );

2.初始化

使用ht-thermodynamic.js 插件来生成热力图。

发热点的数据准备好后,接着配置热力图的参数,参数说明如下。

	
  1. // 默认配置
  2. let config = {
  3.     hot: 45,
  4.     min: 20,
  5.     max: 55,
  6.     size: 50,
  7.     pointNum: 20,
  8.     radius: 150,
  9.     opacity: 0.05,
  10.     colorConfig: {
  11.         0: 'rgba(0,162,255,0.14)',
  12.         0.2: 'rgba(48,255,183,0.60)',
  13.         0.4: 'rgba(255,245,48,0.70)',
  14.         0.6: 'rgba(255,73,18,0.90)',
  15.         0.8: 'rgba(217,22,0,0.95)',
  16.         1: 'rgb(179,0,0)'
  17.     },
  18.     colorStopFn: function (v, step) { return v * step * step },
  19. };
  20. // 获取区域数据
  21. let rackTall = areaNode.getTall();
  22. let heatRect = areaNode.getRect();
  23. let { width, height } = heatRect;
  24. if (width === 0 || height === 0) return;
  25. // 热力图初始化
  26. let thd = this.thd = new ht.thermodynamic.Thermodynamic3d(g3d, {
  27.     // 热力图所占用的空间
  28.     box: new ht.Math.Vector3(width, height, rackTall),
  29.     // 配置温度的最小值和最大值
  30.     min: config.min,
  31.     max: config.max,
  32.     // 每一片的渲染间隔
  33.     interval: 40,
  34.     // 为false时,温度区域交集时值不累加,取最高温度
  35.     remainMax: false,
  36.     // 每一片的透明度
  37.     opacity: config.opacity,
  38.     // 颜色步进
  39.     colorStopFn: config.colorStopFn,
  40.     // 颜色范围
  41.     gradient: config.colorConfig
  42. });

3.加载热力图

将第一步生成的发热点,设置thd的数据对象,调用thd.createThermodynamicNode()来生成热力图的3D 图元。设置其相关信息,将该图元添加进3D场景中。这样一个简单的3D 热力图就算完成了。

 
	
  1. // 加载热力图
  2. function loadThermodynamic(thd, areaNode, templateList, config) {
  3.     thd.setData(templateList);
  4.     // x,y,z面数
  5.     let node = this.heatNode = thd.createThermodynamicNode(config.size, config.size, config.size);
  6.     let p3 = areaNode.p3();
  7.     node.setAnchorElevation(0);
  8.     node.p3(p3);
  9.     node.s({
  10.         'interactive': true,
  11.         'preventDefaultWhenInteractive': false,
  12.         '3d.movable': false,
  13.         "wf.visible": false
  14.     });
  15.     g3d.dm().add(node);
  16. }

主体介绍完了,现在开始讲讲该demo的几个功能。

4.温度提示


因为在3D场景中,我不好判断当前鼠标坐标(x,y,z),所以我将tip面板放在了2D display 上,2D display 嵌在3D场景的上层。通过监听3D场景中的onMove事件来控制tip面板的显隐及值的变化。
  tip显隐控制:当鼠标移入进热力图区域时,tip显示,反之则隐藏。在这我遇到了个问题,因为我把除了热力图区块以外的设置成不可交互的,当鼠标移出区域后,无法监听到onMove事件,导致bug,tip面板始终存在着。我使用了 setTimeout 来解决这问题,延时1s后自动隐藏,但后来发现完全没必要滥用 setTimeout ,只要监听 onLeave 时隐藏tip就行了。
  tip值控制:调用ht-thermodynamic.js的方法可以获取到当前鼠标相对热力图区域的温度值thd.getHeatMapValue(e.event,'middle'),实时改变tip面板的value属性 。
  代码如下:
	
  1. // 交互效果
  2. g3d.mi(e => {
  3.     if (e.kind === 'onMove') {
  4.         let { clientX, clientY } = e.event;
  5.         if (this.templateTip) {
  6.             let value1 = this.thd1.getHeatMapValue(e.event, 'middle');
  7.             let value2 = this.thd2.getHeatMapValue(e.event, 'middle');
  8.             if (value1 || value1 === 0 || value2 || value2 === 0) {
  9.                 let position = g2d.getLogicalPoint({ x: clientX, y: clientY })
  10.                 this.templateTip.a('value', value1 || value2 || 0)
  11.                 let { width, height } = this.templateTip.getRect()
  12.                 this.templateTip.setPosition({ x: position.x + width / 2, y: position.y - height / 2 })
  13.             }
  14.         }
  15.     } else if (kind === 'onLeave') {
  16.         let tag = data.getTag()
  17.         if (tag && tag.hasOwnProperty('hoverBlock') > -1) {
  18.             this.g2d.getView().style.cursor = 'default';
  19.         }
  20.         this.templateTip && this.setVisible(this.templateTip, false)
  21.     }
  22. })

5.扫描


将第三步中的 thd.createThermodynamicNode() 替换。在生成热力图对象时,不直接返回一个模型,而是选择某一个方向进行“切割”,将这一方向的长度均分为 n 份,通过 thd.getHeatMap()  方法来获取每一片的热成像。n 的值理论上可以取任意值,但为了渲染效果更好一点,这里我取的是 50,不至于太多而导致首次渲染时间过长。每切出一个面,我们就在热力区域的相对位置上动态创建一个 ht.Node ,接着使用 ht.Default.setImage() 将切出来的面注册成图片,去设置成该 node 的贴图(只需设置切割方向上的两个面就行)。最后将所有的 node 添加进 dataModel( ht 中承载 Data 数据的模型)。

扫描功能,有两种方案。第一种是在步骤3切割贴片时,不去创建n个  node,而是只创建一个,然后动态去设置该node的贴图及坐标,模拟扫描效果;第二种依旧创建n个node,然后全部隐藏,通过不同时刻来控制让其中某一个节点显示,模拟扫描功能。这里我采用了第二种,因为第一种要去频繁的修改多种属性才能达到效果,第二种的话只要控制其 '3d.visible'。

主要代码如下:


  1. let length;
  2. if (dir === 'z') {
  3.     length = rackTall;
  4. }
  5. else if (dir === 'x') {
  6.     length = width;
  7. }
  8. else if (dir === 'y') {
  9.     length = height;
  10. }
  11. let size = config.size;
  12. for (let index = 0; index < size; index++) {
  13.     // 热力切图间隔
  14.     const offset = length / size;
  15.     let timer = setTimeout(() => {
  16.         let ctx = thd.getHeatMap(index * offset, dir, colorConfig);
  17.         let floor = this.getHeatFloor(
  18.             areaNode,
  19.             dir,
  20.             ctx,
  21.             index,
  22.             size,
  23.             config
  24.         );
  25.         this.floors.push(floor);
  26.         dm.add(floor);
  27.     }, 0);
  28.     this.timers.push(timer);
  29. }
  30. function start() {
  31.     this.hide();
  32.     this.anim = true;
  33.     this.count = 0;
  34.     let frames = this.floors.length;
  35.     let params = {
  36.         frames, // 动画帧数
  37.         interval: 50, // 动画帧间隔毫秒数
  38.         easing: t => {
  39.             return t;
  40.         },
  41.         finishFunc: () => {
  42.             if (this.anim) {
  43.                 this.start();
  44.             }
  45.         },
  46.         action: (v, t) => {
  47.             this.count++;
  48.             this.show(this.count);
  49.         }
  50.     };
  51.     this.scanning = ht.Default.startAnim(params);
  52. }
  53. function hide(index) {
  54.     if (index || index === 0) {
  55.         this.floors.forEach((i, j) => {
  56.             if (index === j) {
  57.                 i.s('3d.visible', false);
  58.             }
  59.             else {
  60.                 i.s('3d.visible', true);
  61.             }
  62.         });
  63.     }
  64.     else {
  65.         this.floors.forEach(i => {
  66.             i.s('3d.visible', false);
  67.         });
  68.     }
  69. }
  70. function show(index) {
  71.     if (index || index === 0) {
  72.         this.floors.forEach((i, j) => {
  73.             if (index === j) {
  74.                 i.s('3d.visible', true);
  75.             }
  76.             else {
  77.                 i.s('3d.visible', false);
  78.             }
  79.         });
  80.     }
  81.     else {
  82.         this.floors.forEach(i => {
  83.             i.s('3d.visible', true);
  84.         });
  85.     }
  86. }

第一种方式实现主要代码:

  1. getHeatFloor(node, dir, config) {
  2.     let { width, height } = node.getRect();
  3.     let rackTall = node.getTall();
  4.     let s3 = [1, rackTall, height];
  5.     let floor = new ht.Node();
  6.     floor.setTag('hotspot');
  7.     floor.setAnchor3d({
  8.         x: 0.5,
  9.         y: 0.5,
  10.         z: 0.5
  11.     });
  12.     floor.s3(s3);
  13.     floor.s({
  14.         interactive: true,
  15.         preventDefaultWhenInteractive: false,
  16.         '3d.selectable': true,
  17.         '3d.movable': false,
  18.         'all.visible': false,
  19.         [Top + '.visible']: true,
  20.         [Top + '.opacity']: config.opacity,
  21.         [Top + '.transparent']: true,
  22.         [Top + '.reverse.flip']: true,
  23.         [Top + '.color']: 'rgba(51,255,231,0.10)'
  24.     });
  25.     return floor
  26. }
  27. getHeatFloorInfo(node, dir, ctx, index, size, config) {
  28.     let { width, height } = node.getRect();
  29.     let rackTall = node.getTall();
  30.     let point = node.getPosition3d();
  31.     let part = 0;
  32.     let p3, s3;

  33.     let Top = 'top';
  34.     if (!dir) {
  35.         dir = 'z';
  36.     }
  37.     // 热力图的yz方向与ht的yz方向相反 dir=z代表的是竖直方向
  38.     if (dir === 'x') {
  39.         Top = 'left';
  40.         part = (width / size) * index;
  41.         p3 = [
  42.             point[0] - width / 2 + part,
  43.             point[1] + rackTall / 2,
  44.             point[2]
  45.         ];
  46.         // p3 = [point[0] + part, point[1], point[2]];
  47.         s3 = [1, rackTall, height];
  48.     }
  49.     else if (dir === 'y') {
  50.         Top = 'front';
  51.         part = (height / size) * index;
  52.         p3 = [
  53.             point[0],
  54.             point[1] + rackTall / 2,
  55.             point[2] - height / 2 + part
  56.         ];
  57.         s3 = [width, rackTall, 1];
  58.     }
  59.     else if (dir === 'z') {
  60.         Top = 'top';
  61.         part = (rackTall / size) * index;
  62.         p3 = [point[0], point[1] + part, point[2]];
  63.         s3 = [width, 1, height];
  64.     }
  65.     let heatName = this.generateUUID();
  66.     ht.Default.setImage('heatMap' + heatName, ctx);
  67.     this.heatFloorInfo.push(
  68.         {
  69.             img: 'heatMap' + heatName,
  70.             p3
  71.         }
  72.     )
  73. }
  74. show(index){
  75.     let info = this.heatFloorInfo[index]
  76.     this.floor.p3(info.p3)
  77.     this.floor.s('3d.visible', true);
  78.     this.floor.s('top.image', info.img);
  79.     // 手动刷新
  80.     this.floor.iv();
  81. }

6.换肤


换肤的实现原理:根据不同的场景值去动态修改ht.graph3d.Graph3dView的背景色及墙的颜色等。

代码:


  1. function changeSkin() {
  2.         let backgroundColor = this.g3d.dm().getBackground(),
  3.             dark_bg = this.g3d.dm().getDataByTag('dark_skin'),
  4.             light_bg = this.g3d.dm().getDataByTag('light_skin');
  5.         if (backgroundColor !== 'rgb(255,255,255)') {
  6.             this.g3d.dm().setBackground('rgb(255,255,255)');
  7.         } else {
  8.             this.g3d.dm().setBackground('rgb(0,0,0)');
  9.         }

  10.         dark_bg.s('2d.visible', !dark_bg.s('2d.visible'));
  11.         dark_bg.s('3d.visible', !dark_bg.s('3d.visible'));

  12.         light_bg.s('2d.visible', !light_bg.s('2d.visible'));
  13.         light_bg.s('3d.visible', !light_bg.s('3d.visible'));
  14.     }

本篇就介绍到了,目前ht-thermodynamic.js还处于测试阶段,待到相对成熟后再更新该demo,有兴趣了解更多关于2D/3D可视化的构建,可翻阅其他文章的例子,HT会给你很多不可思议的东西。

阅读(1340) | 评论(0) | 转发(0) |
0

上一篇:基于 HTML5 Canvas 的 3D 热力云图效果

下一篇:没有了

给主人留下些什么吧!~~