<!Doctype html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>子标签</title>
<!-- 如果本文件的包与src不是在同一个目录,就要将basepath设置到src目录下 -->
<script type="text/javascript">
mxBasePath = '../src';
</script>
<!-- 引入支持库文件 -->
<script type="text/javascript" src="../src/js/mxClient.js"></script>
<!-- 示例代码 -->
<script type="text/javascript">
// 程序启动方法
function main(container)
{
// 浏览器兼容检测
if (!mxClient.isBrowserSupported())
{
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// 在图形中创建容器
var graph = new mxGraph(container);
// 禁止折叠图标
graph.isCellFoldable = function(cell)
{
return false;
}
var secondLabelVisible = true;
// 返回对于一个给定单元格的编号
graph.getSecondLabel = function(cell)
{
if (!this.model.isEdge(cell))
{
// 可能返回一个字符串
return "ID="+cell.id;
}
return null;
};
var relativeChildVerticesVisible = true;
// 隐藏相对于子元素的顶点
graph.isCellVisible = function(cell)
{
return !this.model.isVertex(cell) || cell.geometry == null ||
!cell.geometry.relative ||
cell.geometry.relative == relativeChildVerticesVisible;
};
// 在图形中根据模型编号创建模型
var createShape = graph.cellRenderer.createShape;
graph.cellRenderer.createShape = function(state)
{
createShape.apply(this, arguments);
if (secondLabelVisible &&
!state.cell.geometry.relative)
{
var secondLabel = graph.getSecondLabel(state.cell);
if (secondLabel != null && state.shape != null && state.secondLabel == null)
{
state.secondLabel = new mxText(secondLabel, new mxRectangle(),
mxConstants.ALIGN_LEFT, mxConstants.ALIGN_BOTTOM);
// 标签的样式
state.secondLabel.color = 'black';
state.secondLabel.family = 'Verdana';
state.secondLabel.size = 8;
state.secondLabel.fontStyle = mxConstants.FONT_ITALIC;
state.secondLabel.background = 'yellow';
state.secondLabel.border = 'black';
state.secondLabel.dialect = state.shape.dialect;
state.secondLabel.init(state.view.getDrawPane());
}
}
};
// 移动/调整大小已被重新绘制的元素
var redraw = graph.cellRenderer.redraw;
graph.cellRenderer.redraw = function(state)
{
redraw.apply(this, arguments);
if (state.shape != null && state.secondLabel != null)
{
var scale = graph.getView().getScale();
var bounds = new mxRectangle(state.x + state.width - 8 * scale, state.y + 8 * scale, 0, 0);
state.secondLabel.value = graph.getSecondLabel(state.cell);
state.secondLabel.scale = scale;
state.secondLabel.bounds = bounds;
state.secondLabel.redraw();
}
};
// 销毁图形编号
var destroy = graph.cellRenderer.destroy;
graph.cellRenderer.destroy = function(state)
{
destroy.apply(this, arguments);
if (state.secondLabel != null)
{
state.secondLabel.destroy();
state.secondLabel = null;
}
};
// 创建默认窗口
var parent = graph.getDefaultParent();
// 开启更新事务
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'Hello,', 30, 20, 80, 30);
// 创建子元素
var v11 = graph.insertVertex(v1, null, 'World', 1, 1, 0, 0, 'align=left;verticalAlign=top;labelBackgroundColor=red;labelBorderColor=black', true);
v11.geometry.offset = new mxPoint(-8, -8);
var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
// 创建子元素
var v21 = graph.insertVertex(v2, null, 'World', 1, 1, 0, 0, 'align=left;verticalAlign=top;fillColor=red;rounded=1;spacingLeft=4;spacingRight=4', true);
v21.geometry.offset = new mxPoint(-8, -8);
graph.updateCellSize(v21);
var e1 = graph.insertEdge(parent, null, '', v1, v2);
}
finally
{
// 结束更新事务
graph.getModel().endUpdate();
}
// 添加切换子元素按钮
document.body.insertBefore(mxUtils.button('切换到子元素Toggle Child Vertices',
function(evt)
{
relativeChildVerticesVisible = !relativeChildVerticesVisible;
graph.refresh();
}
), document.body.firstChild);
// 添加切换编号按钮
document.body.insertBefore(mxUtils.button('切换到编号Toggle IDs',
function(evt)
{
secondLabelVisible = !secondLabelVisible;
graph.refresh();
}
), document.body.firstChild);
}
};
</script>
</head>
<!-- 页面载入时启动程序 -->
<body onload="main(document.getElementById('graphContainer'))">
<!-- 创建一个带网格壁纸的容器 -->
<div id="graphContainer"
style="overflow:hidden;width:321px;height:241px;background:url('editors/images/grid.gif')">
</div>
</body>
</html>