js画图开发库--mxgraph--[markers-标记.html]

 js画图开发库--mxgraph--[markers-标记.html] 

 

 
js画图开发库--mxgraph--[markers-标记.html]
 

<!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
			{
				// 去锯齿效果
				mxShape.prototype.crisp = true;
				mxEllipse.prototype.crisp = false;
				mxDoubleEllipse.prototype.crisp = false;
				mxArrow.prototype.crisp = false;
				mxConnector.prototype.allowCrispMarkers = true;
				
				// 启用导航线
				mxGraphHandler.prototype.guidesEnabled = true;
				mxEdgeHandler.prototype.snapToTerminals = true;
				
				// 自定义图标
				mxMarker.markers['dash'] = function(node, type, pe, nx, ny, strokewidth, size, scale, isVml)
				{
					nx = nx * (size + strokewidth);
					ny = ny * (size + strokewidth);
					
					if (isVml)
					{
						node.setAttribute('path', 'm' + Math.floor(pe.x - nx / 2- ny / 2) + ' ' + Math.floor(pe.y - ny / 2 + nx / 2) +
							' l ' + Math.floor(pe.x + ny / 2 - 3 * nx / 2) + ' ' + Math.floor(pe.y - 3 * ny / 2 - nx / 2) +
							' e');
					}
					else
					{
						node.setAttribute('d', 'M ' + (pe.x - nx / 2 - ny / 2) + ' ' + (pe.y - ny / 2 + nx / 2) +
								' L ' + (pe.x + ny / 2 - 3 * nx / 2) + ' ' + (pe.y - 3 * ny / 2 - nx / 2) +
								' z');
					}
					
					//返回标记的偏移量
					return new mxPoint(0, 0);
				};

				// 信息形状
				function MessageShape() { };
				MessageShape.prototype = new mxCylinder();
				MessageShape.prototype.constructor = MessageShape;
				MessageShape.prototype.redrawPath = function(path, x, y, w, h, isForeground)
				{
					if (isForeground)
					{
						path.moveTo(0, 0);
						path.lineTo(w / 2, h / 2);
						path.lineTo(w, 0);
						path.end();
					}
					else
					{
						path.moveTo(0, 0);
						path.lineTo(w, 0);
						path.lineTo(w, h);
						path.lineTo(0, h);
						path.close();
					}
				};

				mxCellRenderer.prototype.defaultShapes['message'] = MessageShape;
				
				// 自定义连接线
				function LinkShape() { };
				LinkShape.prototype = new mxArrow();
				LinkShape.prototype.constructor = LinkShape;
				LinkShape.prototype.enableFill = false;
				LinkShape.prototype.addPipe = true;
				
				LinkShape.prototype.redrawPath = function(path, x, y, w, h)
				{
					// 所有点都偏移
					path.translate.x -= x;
					path.translate.y -= y;

					// 几何箭头
					var width = 10 * this.scale;

					// 元素之间的矢量图
					var p0 = this.points[0];
					var pe = this.points[this.points.length - 1];
					
					var dx = pe.x - p0.x;
					var dy = pe.y - p0.y;
					var dist = Math.sqrt(dx * dx + dy * dy);
					var length = dist;
					
					// 计算长度和宽度
					var nx = dx / dist;
					var ny = dy / dist;
					var basex = length * nx;
					var basey = length * ny;
					var floorx = width * ny/3;
					var floory = -width * nx/3;
					
					// 计算点
					var p0x = p0.x - floorx / 2;
					var p0y = p0.y - floory / 2;
					var p1x = p0x + floorx;
					var p1y = p0y + floory;
					var p2x = p1x + basex;
					var p2y = p1y + basey;
					var p3x = p2x + floorx;
					var p3y = p2y + floory;
					// p4 not necessary
					var p5x = p3x - 3 * floorx;
					var p5y = p3y - 3 * floory;
					
					// 移动
					path.moveTo(p1x, p1y);
					path.lineTo(p2x, p2y);
					path.moveTo(p5x + floorx, p5y + floory);
					path.lineTo(p0x, p0y);
					path.end();
				};

				mxCellRenderer.prototype.defaultShapes['link'] = LinkShape;

				// 在容器中创建图形
				var graph = new mxGraph(container);
				
				// 设置默认样式
				graph.getStylesheet().getDefaultVertexStyle()['fillColor'] = '#FFFFFF';
				graph.getStylesheet().getDefaultVertexStyle()['strokeColor'] = '#000000';
				graph.getStylesheet().getDefaultVertexStyle()['fontColor'] = '#000000';
				graph.getStylesheet().getDefaultVertexStyle()['fontStyle'] = '1';
				graph.getStylesheet().getDefaultEdgeStyle()['strokeColor'] = '#000000';
				graph.getStylesheet().getDefaultEdgeStyle()['fontColor'] = '#000000';
				graph.getStylesheet().getDefaultEdgeStyle()['fontStyle'] = '0';
				graph.getStylesheet().getDefaultEdgeStyle()['fontStyle'] = '0';
				graph.getStylesheet().getDefaultEdgeStyle()['startSize'] = '8';
				graph.getStylesheet().getDefaultEdgeStyle()['endSize'] = '8';

				// 创建默认窗体
				var parent = graph.getDefaultParent();
								
				graph.getModel().beginUpdate();
				try
				{
					var v1 = graph.insertVertex(parent, null, 'v1', 20, 20, 80, 30);
					var v2 = graph.insertVertex(parent, null, 'v2', 440, 20, 80, 30);
					var e1 = graph.insertEdge(parent, null, '', v1, v2, 'dashed=1;'+
						'startArrow=oval;endArrow=block;sourcePerimeterSpacing=4;startFill=0;endFill=0;');
					var e11 = graph.insertVertex(e1, null, 'Label', 0, 0, 20, 14,
						'shape=message;labelBackgroundColor=#ffffff;labelPosition=left;align=right;fontStyle=0;');
					e11.geometry.offset = new mxPoint(-10, -7);
					e11.geometry.relative = true;
					e11.connectable = false;
					
					var v3 = graph.insertVertex(parent, null, 'v3', 20, 120, 80, 30);
					var v4 = graph.insertVertex(parent, null, 'v4', 440, 120, 80, 30);
					var e2 = graph.insertEdge(parent, null, 'Label', v3, v4,
						'startArrow=dash;startSize=12;endArrow=block;labelBackgroundColor=#FFFFFF;');

					var v5 = graph.insertVertex(parent, null, 'v5', 40, 220, 40, 40, 'shape=ellipse;perimeter=ellipsePerimeter;');
					var v6 = graph.insertVertex(parent, null, 'v6', 460, 220, 40, 40, 'shape=doubleEllipse;perimeter=ellipsePerimeter;');
					var e3 = graph.insertEdge(parent, null, 'Link', v5, v6,
						'shape=link;labelBackgroundColor=#FFFFFF;');
				}
				finally
				{
					graph.getModel().endUpdate();
				}
			}
		};
	</script>
</head>
<!-- 页面载入时启动程序 -->
<body onload="main(document.getElementById('graphContainer'))">
<!-- 创建带网格壁纸和曲线的一个容器  -->
	<div id="graphContainer"
		style="overflow:hidden;position:relative;width:641px;height:381px;border:1px solid gray;cursor:default;">
	</div>
</body>
</html>

 

 

你可能感兴趣的:(js画图开发库,mxgraph,markers,标记.html])