天天看點

js畫圖開發庫--mxgraph--[labels-标簽.html]

 js畫圖開發庫--mxgraph--[labels-标簽.html] 

js畫圖開發庫--mxgraph--[labels-标簽.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
			{
				// 修正不同浏覽器的裁剪方式
				mxClient.NO_FO = mxClient.NO_FO || mxClient.IS_SF || mxClient.IS_GC;
				
				// 在容器中建立圖形
				var graph = new mxGraph(container);
				graph.setTooltips(true);
				graph.htmlLabels = true;
				graph.vertexLabelsMovable = true;
				new mxRubberband(graph);
				new mxKeyHandler(graph);
				
				graph.isCellLocked = function(cell)
				{
					return this.isCellsLocked();
				};
				
				graph.isCellResizable = function(cell)
				{
					var geo = this.model.getGeometry(cell);
					
					return geo == null || !geo.relative;
				};
				
				// 截去的标簽内多餘的文字(隻顯示标簽寬度内的文字)
				graph.getLabel = function(cell)
				{
					var label = (this.labelsVisible) ? this.convertValueToString(cell) : '';
					var geometry = this.model.getGeometry(cell);
					
					if (!this.model.isCollapsed(cell) && geometry != null && (geometry.offset == null ||
						(geometry.offset.x == 0 && geometry.offset.y == 0)) && this.model.isVertex(cell) &&
						geometry.width >= 2)
					{
						var style = this.getCellStyle(cell);
						var fontSize = style[mxConstants.STYLE_FONTSIZE] || mxConstants.DEFAULT_FONTSIZE;
						var max = geometry.width / (fontSize * 0.625);
						
						if (max < label.length)
						{
							return label.substring(0, max) + '...';
						}
					}
					
					return label;
				};
				
				// 是否折疊
				graph.isWrapping = function(cell)
				{
					return this.model.isCollapsed(cell);
				};
				
				// 标簽是否被裁剪
				graph.isLabelClipped = function(cell)
				{
					var geometry = this.model.getGeometry(cell);
					
					return geometry != null && !geometry.relative && (geometry.offset == null ||
						(geometry.offset.x == 0 && geometry.offset.y == 0));
				};
				
				// 建立預設窗體
				var parent = graph.getDefaultParent();
								
				// 開啟更新事務
				graph.getModel().beginUpdate();
				try
				{
					var v1 = graph.insertVertex(parent, null, 'vertexLabelsMovable', 20, 20, 80, 30);

					// 子标簽的頂點
					var label11 = graph.insertVertex(v1, null, 'Label1', 0.5, 1, 0, 0, null, true);
					var label12 = graph.insertVertex(v1, null, 'Label2', 0.5, 0, 0, 0, null, true);
					
					var v2 = graph.insertVertex(parent, null, 'Wrapping and clipping is enabled only if the cell is collapsed, otherwise the label is truncated if there is no manual offset.', 200, 150, 80, 30);
					v2.geometry.alternateBounds = new mxRectangle(0, 0, 80, 30);
					var e1 = graph.insertEdge(parent, null, 'edgeLabelsMovable', v1, v2);

					// 子标簽的頂點
					var label21 = graph.insertVertex(v2, null, 'Label1', 0.5, 1, 0, 0, null, true);
					var label22 = graph.insertVertex(v2, null, 'Label2', 0.5, 0, 0, 0, null, true);
				}
				finally
				{
					// 更新事務結束
					graph.getModel().endUpdate();
				}
			}
		};
	</script>
</head>

<!-- 頁面載入時啟動程式 -->
<body οnlοad="main(document.getElementById('graphContainer'))">

	<!-- 建立帶網格桌面和曲線的一個容器  -->
	<div id="graphContainer"
		style="position:absolute;top:0px;left:0px;overflow:hidden;width:100%;height:100%;background:url('editors/images/grid.gif')">
	</div>
</body>
</html>
      

繼續閱讀