前面用highchart尝试画了一次,但是因为种种因素,还是用js+div+css实现起来比较方便。
代码如下,可直接复制粘贴。
html部分:
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
动态绘制结构图
* {
margin: 0;
padding: 0;
}
div.rect {
height: 40px;
border-radius: 10px;
background: pink;
text-align: center;
padding: 5px;
margin: 5px 5px;
}
div.rect:hover {
cursor: pointer;
}
div.rect p {
font-size: 14px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
div.flex {
display: flex;
justify-content: center;
flex-wrap: wrap
}
div.flex > div {
width: 25%;
}
div.arrow {
margin: 0 10px 0;
}
div.arrow-body {
width: 20px;
height: 50px;
background: #ccc;
margin: 0 auto;
}
div.arrow-footer {
width: 0;
height: 0;
border-width: 30px 30px 0;
border-style: solid;
border-color: #ccc transparent transparent; /*灰 透明 透明 */
margin: 0 auto;
}
#container {
width: 650px;
/*height: 450px;*/
margin: 50px auto;
/*padding-top: 10px;*/
/*padding-right: 10px;*/
/*overflow-y: auto*/
}
js部分:
let data = {
root: [
{title: '根节点1', color: 'red'},
{title: '根节点2', color: 'yellow'},
{title: '根节点3', color: 'skyblue'},
],
child: [
{title: '节点', color: '#cca057'}
],
child2: [
{title: '子节点1'},
{title: '子节点2阿诗阿范德萨士大夫士大夫实打实地方的所得税法的萨芬撒地方丹顿'},
{title: '子节点3子节点3子节点3子节点3'},
{title: '子节点4'},
{title: '子节点5'},
{title: '子节点6'},
{title: '子节点7'},
{title: '子节点8'}
]
};
let pic = {
load(domName, datas) {
let num = Object.keys(datas).length;
Object.keys(datas).forEach((i, j) => {
pic.rect('container', datas[i], (j + 1) === num);
});
this.bindEvents()
},
rect(domName, data, isEnd) {
let div = document.createElement("div");
div.classList.add("flex");
for (let i in data) {
div.innerHTML += '
\n' +
'
\n' +
'
' + data[i].title + '
\n' +
'
\n' +
'
';
}
document.getElementById(domName).appendChild(div);
if (!isEnd) {
let arrow = document.createElement("div");
arrow.classList.add("arrow");
arrow.innerHTML +=
'
'
document.getElementById(domName).appendChild(arrow)
}
},
bindEvents() {
let rect = document.getElementsByClassName('rect');
for (let i = 0, rule; rule = rect[i++];) {
rule.addEventListener('click', () => {
let title = rule.getAttribute('data');
alert(title);
});
}
}
};
window.onload = function () {
pic.load('container', data);
};
效果图:
总结:
想比highchart自由绘图,该种方案最省时省力省心。