js画図開発庫--mxgraph-[secondlel-サブラベル.]


<!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>