<!DOCUMENTTYPE>
<html>
<head>
<script>
var right ;
var posx = 0;
var posy = 0;
var isFirst = false;
var stepSzie = 200;
var heightSize = 50;
function generate(){
right = document.getElementById("right");
var value = document.getElementById("input").value;
var jsonObj = JSON.parse(value);
analize(jsonObj,0);
}
//递归遍历json
function analize(jsonObj,level){
for(var p in jsonObj){
var cell = jsonObj[p];
if(typeof(cell) == 'object'){
isFirst = true;
posy+=heightSize;
posx = stepSzie * (level);
right.appendChild(generateCell(p,posx,posy,level));
analize(cell,level+1);
}else{
if(isFirst){
posx = stepSzie * (level);
}
posy+=heightSize;
right.appendChild(generateCell(p,posx,posy,level));
right.appendChild(generateCell(cell,posx+stepSzie,posy,level));
}
}
}
function generateCell(text,x,y,type){
var div = document.createElement("div");
div.textContent = text;
div.style.backgroundColor = "rgb(42, 125, 171)";
div.style.position = "absolute";
div.style.top = y + "px";
div.style.left = x + "px";
div.style.color = "#fff";
div.style.paddingLeft = "2px";
div.style.paddingRight = "2px";
div.style.borderRadius = "5px";
div.style.boxShadow = "5px 5px 5px #000";
div.id = type;
return div;
}
function getById(id){
return document.getElementById(id);
}
</script>
<style>
.left{
border:1px solid #f00;
width:500px;
height:800px;
position:absolute;
left:1px;
}
.right{
border:1px solid #f00;
width:1300px;
height:800px;
position:absolute;
right:1px;
}
.inputStyle{
width: 400px;
height: 400px;
}
</style>
</head>
<body>
</body>
<div class="left">
<textarea id="input" class="inputStyle"></textarea>
<button onclick="generate();">生成</button>
<svg height="400" width="450" style="border:1px solid #f00;">
<path d="M 10 200 C50 100 300 50 300 200" stroke="blue" stroke-width="5" fill="none" />
<g stroke="black" stroke-width="3" fill="black">
<circle id="pointA" cx="10" cy="200" r="3" />
<circle id="pointB" cx="50" cy="100" r="3" />
<circle id="pointC" cx="300" cy="50" r="3" />
<circle id="pointC" cx="300" cy="200" r="3" />
</g>
</svg>
</div>
<div id="right" class="right">
</div>
</html>