Highcharts矩形树图:面积、颜色和层级遍历
Treemap用矩形面积表达层级量,不是热力图或普通树形连线图。
学完这一章,你能够
- Treemap用矩形面积表达层级量,不是热力图或普通树形连线图。
- 矩形面积和颜色分别编码
一层与多层数据
单层treemap的value控制面积,colorValue沿colorAxis控制颜色;相同面积可以有不同颜色。squarified与stripes等算法决定布局,不能把它们称为最优保证。
多层数据用唯一id和parent建立树,叶子value求上层量。本站校验缺父、重复id和环;原例指向不存在Kiwi节点的内容用显式有效拓展父节点说明,不能静默丢掉。
遍历和更大集合
第三个例子独立生成5组、40教程、240叶子共285节点,当前allowTraversingTree和interactByLeaf允许点击父块进入,再返回根。层级标签和边框按层配置。
来源省略的大数据对象及其原编辑器尚未取得,所以本站集合明确是合成教学数据。原例混用区域均值和子项合计、subtitle声明语法等问题须说明;本例不用真实医疗统计,也不把285节点观察当性能基准或原数据已恢复。
矩形面积和颜色分别编码
七个value合计24,相同值面积近似相同,像素取整有误差;colorValue仍可不同。
需要treemap及适用颜色轴模块;不是热力网格或树形连线。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。
有效父节点与层级
四个明确父节点和十个叶子;缺父、重复id和父链环先由本站函数拒绝。
布局算法与首层标签分别配置,未绑定的父节点不能静默丢失,也不能凭缺图猜关系。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。
285节点与父块遍历
本站生成5组、40教程、240叶子共285节点,点击父块实际进入并可返回根。
原大型数据对象在主文省略,原编辑器未取得;本站合成集合不计作原始数据恢复或性能基准。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。
运行代码文件:essentials.js
下列代码与本章独立工程实际脚本一致;容器、本地数据和模块随工程包保留。
"use strict";
window.mbskyErrors=[];window.addEventListener("error",e=>mbskyErrors.push(e.message));window.addEventListener("unhandledrejection",e=>mbskyErrors.push(String(e.reason)));
const plots={},base={chart:{animation:false},title:{text:"蓝色天空网 · mbsky.com"},subtitle:{text:"本站虚构学习数据"},time:{timezone:"UTC"},colors:["#2563eb","#f97316","#10b981"],plotOptions:{series:{animation:false}},accessibility:{enabled:true}};
function make(id,opts){return plots[id]=Highcharts.chart(id,Highcharts.merge(base,opts));}
make("scatter",{chart:{type:"scatter",zooming:{type:"xy"}},xAxis:{min:0,max:50,title:{text:"虚构学习时长 / 分钟"},startOnTick:true,endOnTick:true,showLastLabel:true},yAxis:{min:0,max:20,title:{text:"虚构练习次数"}},plotOptions:{scatter:{marker:{radius:5,states:{hover:{enabled:true,lineColor:"#646464"}}}},series:{tooltip:{pointFormat:"{point.x}分钟,{point.y}次"}}},series:[{name:"计划甲",color:"rgba(37,99,235,.5)",data:[[5,3],[10,4],[15,7],[20,6],[25,10],[30,11],[35,12],[40,13]]},{name:"计划乙",color:"rgba(249,115,22,.5)",data:[[8,5],[13,6],[18,8],[23,10],[28,9],[33,12],[38,14],[43,12]]}]});document.querySelector("#scatter-reset").onclick=()=>plots.scatter.zoomOut();
const bubbleData=[[1,2,1],[3,4,4],[5,3,9],[7,6,16]];
for(const [id,gradient]of [["bubble",false],["bubble-gradient",true]])make(id,{chart:{type:"bubble",zooming:{type:"xy"}},xAxis:{title:{text:"练习阶段"},gridLineWidth:1},yAxis:{title:{text:"虚构练习量"},startOnTick:false,endOnTick:false},tooltip:{pointFormat:"x={point.x},y={point.y},z={point.z}"},series:Array.from({length:gradient?2:3},(_,i)=>({name:`计划${i+1}`,data:bubbleData.map(([x,y,z])=>[x+i*.4,y+i*2,z]),minSize:10,maxSize:60,...(gradient?{marker:{fillColor:{radialGradient:{cx:.4,cy:.3,r:.7},stops:[[0,"rgba(255,255,255,.5)"],[1,Highcharts.color(i?"#f97316":"#2563eb").setOpacity(.5).get("rgba")]]}}}:{})}))});
let timer=null,updates=0;const now=Date.now();
make("dynamic",{chart:{type:"spline",marginRight:10},xAxis:{type:"datetime",tickPixelInterval:150},yAxis:{title:{text:"本站虚构练习强度"},plotLines:[{value:0,width:1,color:"#8092af"}]},legend:{enabled:false},tooltip:{formatter(){return `${Highcharts.dateFormat("%Y-%m-%d %H:%M:%S",this.x)} UTC:${Highcharts.numberFormat(this.y,2)}`;}},series:[{name:"模拟数据",data:Array.from({length:20},(_,i)=>[now+(i-19)*1000,2+Math.sin(i/4)])}]});
function stop(){if(timer!==null)clearInterval(timer);timer=null;document.querySelector("#dynamic-status").textContent=`已停止,已更新${updates}次`;}
function tick(){const c=plots.dynamic;if(!c?.series){stop();return;}updates++;const last=c.series[0].points.at(-1).x;c.series[0].addPoint([Math.max(Date.now(),last+1),2+Math.sin(updates/4)],true,true,false);document.querySelector("#dynamic-status").textContent=`每秒更新中,已更新${updates}次`;}
document.querySelector("#dynamic-start").onclick=()=>{stop();if(plots.dynamic?.series)timer=setInterval(tick,1000);};document.querySelector("#dynamic-stop").onclick=stop;document.querySelector("#dynamic-destroy").onclick=()=>{stop();plots.dynamic.destroy();document.querySelector("#dynamic").textContent="已销毁并清理定时器";};window.addEventListener("pagehide",stop);
make("click",{chart:{type:"scatter",margin:[70,50,60,80],events:{click(e){if(e.xAxis?.length&&e.yAxis?.length)this.series[0].addPoint([e.xAxis[0].value,e.yAxis[0].value]);}}},xAxis:{min:0,max:100,minRange:1,gridLineWidth:1},yAxis:{min:0,max:100,minRange:1,title:{text:"虚构数值"}},legend:{enabled:false},plotOptions:{series:{lineWidth:1,point:{events:{click(){if(this.series.points.length>1)this.remove();return false;}}}}},series:[{name:"用户本机点",data:[[20,20],[80,80]]}]});
function validateTree(data){const by=new Map();for(const q of data){if(!q.id||by.has(q.id))throw new TypeError("节点id必须唯一且非空");by.set(q.id,q);if(q.value!==undefined&&(!Number.isFinite(q.value)||q.value<0))throw new RangeError("value必须有限非负");}for(const q of data){const visited=new Set([q.id]);let parent=q.parent;while(parent){if(!by.has(parent))throw new TypeError("缺失parent节点");if(visited.has(parent))throw new TypeError("parent关系形成环");visited.add(parent);parent=by.get(parent).parent;}}return structuredClone(data);}
make("tree",{colorAxis:{minColor:"#fff",maxColor:"#2563eb"},series:[{type:"treemap",layoutAlgorithm:"squarified",data:[6,6,4,3,2,2,1].map((value,i)=>({id:`single-${i}`,name:`学习${i+1}`,value,colorValue:i+1}))}]});
const hierarchy=[{id:"html",name:"HTML5",color:"#2563eb"},{id:"css",name:"CSS",color:"#f97316"},{id:"js",name:"JavaScript",color:"#10b981"},{id:"extra",name:"拓展",color:"#8b5cf6"},...[["html",[5,3,4]],["css",[4,10,1]],["js",[1,3,3]],["extra",[2]]].flatMap(([parent,values])=>values.map((value,i)=>({id:parent+'-'+i,parent,name:`练习${i+1}`,value})))];
make("tree-levels",{series:[{type:"treemap",layoutAlgorithm:"stripes",alternateStartingDirection:true,levels:[{level:1,layoutAlgorithm:"sliceAndDice",dataLabels:{enabled:true,align:"left",verticalAlign:"top",style:{fontWeight:"bold",fontSize:"15px"}}}],data:validateTree(hierarchy)}]});
const large=[];for(let i=0;i<5;i++){const group=`group-${i}`;large.push({id:group,name:`课程组${i+1}`,color:["#2563eb","#f97316","#10b981","#8b5cf6","#e11d48"][i]});for(let j=0;j<8;j++){const unit=`${group}-unit-${j}`;large.push({id:unit,parent:group,name:`教程${j+1}`});for(let k=0;k<6;k++)large.push({id:`${unit}-${k}`,parent:unit,name:`章节${k+1}`,value:(i+1)*(j+1)+k+1});}}
make("tree-large",{series:[{type:"treemap",layoutAlgorithm:"squarified",allowTraversingTree:true,interactByLeaf:false,levelIsConstant:false,dataLabels:{enabled:false},levels:[{level:1,borderWidth:3,dataLabels:{enabled:true}}],data:validateTree(large)}]});document.querySelector("#tree-root").onclick=()=>plots["tree-large"].series[0].setRootNode("");
window.mbskyEssentials={plots,validateTree,hierarchy,large,stop,tick,get timer(){return timer},get updates(){return updates}};
矩形树图数据
| 配置或接口 | 类型/写法 | 用途与准确条件 | 本例或迁移说明 |
| value / colorValue | 有限非负数 / 颜色度量 | 面积 / 颜色 | 并非同一字段 |
| id / parent | 唯一id / 父id | 树结构关联 | 缺父/重复/环拒绝 |
| layoutAlgorithm / levels | 算法名 / 分层配置 | 矩形排列与各层样式 | 不保证某算法全局最优 |
| allowTraversingTree / interactByLeaf | boolean / boolean | 父块可遍历及交互粒度 | 旧allowDrillToNode迁移需按版本核对 |
| setRootNode | 节点id | 切换查看根 | 本站真实点击与返回已观察 |
本章独立核心源码与验证步骤
展开查看收起
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>矩形面积和颜色分别编码 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-tree" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>矩形面积和颜色分别编码</h1><p>七个value合计24,相同值面积近似相同,像素取整有误差;colorValue仍可不同。</p><iframe title="矩形面积和颜色分别编码的实际运行图表" src="../essentials.html#case-tree" style="width:100%;height:650px;border:0"></iframe><p><a href="../essentials.html#case-tree" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
尝试一下 ↗ 在独立页面修改并运行
- 七个value合计24,相同值面积近似相同,像素取整有误差;colorValue仍可不同。
- 需要treemap及适用颜色轴模块;不是热力网格或树形连线。
有效父节点与层级
展开查看收起
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>有效父节点与层级 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-tree-levels" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>有效父节点与层级</h1><p>四个明确父节点和十个叶子;缺父、重复id和父链环先由本站函数拒绝。</p><iframe title="有效父节点与层级的实际运行图表" src="../essentials.html#case-tree-levels" style="width:100%;height:650px;border:0"></iframe><p><a href="../essentials.html#case-tree-levels" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
尝试一下 ↗ 在独立页面修改并运行
- 四个明确父节点和十个叶子;缺父、重复id和父链环先由本站函数拒绝。
- 布局算法与首层标签分别配置,未绑定的父节点不能静默丢失,也不能凭缺图猜关系。
285节点与父块遍历
展开查看收起
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>285节点与父块遍历 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-tree-large" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>285节点与父块遍历</h1><p>本站生成5组、40教程、240叶子共285节点,点击父块实际进入并可返回根。</p><iframe title="285节点与父块遍历的实际运行图表" src="../essentials.html#case-tree-large" style="width:100%;height:650px;border:0"></iframe><p><a href="../essentials.html#case-tree-large" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
尝试一下 ↗ 在独立页面修改并运行
- 本站生成5组、40教程、240叶子共285节点,点击父块实际进入并可返回根。
- 原大型数据对象在主文省略,原编辑器未取得;本站合成集合不计作原始数据恢复或性能基准。
本章附件与实例包
常见错误与边界检查
- 把配置名、SVG节点或成功加载当成全部教学行为。
- 把本站合成数据、重写实例及自制图称为原始采集成功。
技术规范与官方资料
练习与参考解答
查看参考解答
分别核对数据、轴/系列映射、可见几何、交互和资源清理,不只看退出码。