代码
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset=“UTF-8”>
- <title></title>
- <style>
- div {
- width: 600px;
- height: 400px;
- border: none;
- }
- </style>
- </head>
- <body>
- <div id=“heatmap”></div>
- </body>
- <script src=“https://cdn.bootcss.com/heatmap.js/2.0.2/heatmap.min.js”></script>
- <script type=“text/javascript”>
- // 创建一个heatmap实例对象
- var heatmapInstance = h337.create({
- container: document.querySelector(‘#heatmap’),
- });
- //构建一些随机数据点
- var getPoints = function () {
- var points = [];
- var max = 0;
- var width = 600;
- var height = 400;
- var len = 200;
- while (len–) {
- var val = Math.floor(Math.random() * 100);
- max = Math.max(max, val);
- var point = {
- x: Math.floor(Math.random() * width),
- y: Math.floor(Math.random() * height),
- value: val
- };
- points.push(point);
- }
- var data = {
- max: max,
- data: points
- };
- return data;
- }
- //数据绑定
- setInterval(function () {
- heatmapInstance.setData(getPoints());
- }, 3000)
- heatmapInstance.setData(getPoints());
- </script>
- </html>
例子详解
- h337.create({
- container: document.getElementById(“text”),
- backgroundColor: ‘red’, // ‘#121212’ ‘rgba(0,102,256,0.2)’
- gradient: {
- ‘0.5’: ‘blue’,
- ‘0.8’: ‘red’,
- ‘0.95’: ‘white’,
- ‘0.6’: ‘yellow’,
- ‘0.5’: ‘green’
- },
- radius: 10, // [0,+∞)
- opacity: 0.8,
- });
h337.create(configObject)
创建heatmap实例,configObject是一个json对象,里面有很多参数
参数详情
container 页面操作div的dom对象,如div的id为test,可以写成
backgroundColor 画板的背景颜色设置,支持rgb(a),颜色名称,但必须要用引号
gradient 设置热点图的光圈颜色,数值为[0,1],数值大的在光圈内侧,数值相等则靠下的生效,数值设置不分大小顺序,并可以同时设置很多颜色
radius 设置光圈的半径大小,值>=0,=0取得是默认值
opacity 光圈透明度设置[0,1],如果值设置了,会重写maxOpacity和minOpacity的值
更多请参照文档: https://www.patrick-wied.at/static/heatmapjs/docs.html
本文链接地址: 利用 heatmap.js 创建热力图