Home > matgraph > @graph > graffle.m

graffle

PURPOSE ^

graffle(g, filename, width, rad) --- write graph in OmniGraffle format

SYNOPSIS ^

function graffle(g, filename, width, rad)

DESCRIPTION ^

 graffle(g, filename, width, rad) --- write graph in OmniGraffle format

 This writes a graph to the disk with the file name specified in the
 second argument (default is 'graph.graffle'). 
 The width (3rd argument) gives the overall size of the plot
 (default=450).
 The radius (4th argument) gives the size of a vertex (default=12).
 Note units are points = 1/72 inch. So 18 = 1/4 inch.
 
 The output file can then be opened in OmniGraffle, a graph drawing 
 program for Macintosh.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function graffle(g, filename, width, rad)
0002 % graffle(g, filename, width, rad) --- write graph in OmniGraffle format
0003 %
0004 % This writes a graph to the disk with the file name specified in the
0005 % second argument (default is 'graph.graffle').
0006 % The width (3rd argument) gives the overall size of the plot
0007 % (default=450).
0008 % The radius (4th argument) gives the size of a vertex (default=12).
0009 % Note units are points = 1/72 inch. So 18 = 1/4 inch.
0010 %
0011 % The output file can then be opened in OmniGraffle, a graph drawing
0012 % program for Macintosh.
0013 
0014 DEFAULT_FILE_NAME = 'graph.graffle';
0015 DEFAULT_RADIUS = 12;
0016 PAGE_SIZE = 450;
0017 
0018 if nargin == 1
0019     filename = DEFAULT_FILE_NAME;
0020 end
0021 
0022 if nargin < 3
0023     width = PAGE_SIZE;
0024 end
0025 
0026 if nargin < 4
0027     rad = DEFAULT_RADIUS;
0028 end
0029 
0030 fid = fopen(filename,'w');
0031 
0032 if (fid == -1)
0033     error(['Cannot open ', filename, ' for output']);
0034 end
0035 
0036 if (~hasxy(g))
0037     embed(g)
0038 end
0039 
0040 xy = getxy(g);
0041 x = xy(:,1);
0042 y = xy(:,2);
0043 
0044 
0045 % move lower left to 0,0
0046 x = x-min(x);
0047 y = -y;
0048 y = y-min(y);
0049 
0050 % rescale to fit in reasonable square
0051 m = max([x;y]);
0052 scale = width/m;
0053 
0054 x = round(scale*x)+5;
0055 y = round(scale*y)+5;
0056 
0057 
0058 % write XML header line
0059 fprintf(fid,'<?xml version="1.0" encoding="UTF-8"?>\n');
0060 
0061 % write start of XML GraphicsList array
0062 fprintf(fid,'<dict>\n');
0063 write_key(fid, 'GraphicsList');
0064 fprintf(fid,'<array>');
0065 
0066 
0067 % write vertices to XML file
0068 for v = 1:nv(g)
0069     write_vertex(fid, v, x(v), y(v), rad)
0070 end
0071 
0072 % write edges
0073 elist = edges(g);
0074 m = ne(g);
0075 for i=1:m
0076     u = elist(i,1);
0077     v = elist(i,2);
0078     write_edge(fid, u, v);
0079 end
0080 
0081 
0082 % end the array
0083 fprintf(fid,'</array>\n');
0084 % end the dictionary
0085 fprintf(fid,'</dict>\n');
0086 
0087 fclose(fid);
0088 
0089 
0090 
0091 % page area:
0092 % x values run 0 to 520 or so
0093 % y values run 0 to 700 or so
0094 
0095 
0096 function write_key(fid,key_name)
0097 fprintf(fid,[' <key>', key_name, '</key>\n']);
0098 
0099 function write_string(fid, string)
0100 fprintf(fid,[' <string>', string, '</string>\n']);
0101 
0102 function write_integer(fid,n)
0103 fprintf(fid,[' <integer>', int2str(n), '</integer>\n']);
0104 
0105 
0106 % write vertex v @ coordinates (x,y) with radius r to output
0107 function write_vertex(fid, v, x, y, r)
0108 fprintf(fid,'<dict>\n');
0109 write_key(fid,'Bounds');
0110 fprintf(fid,[' <string>{{', int2str(x), ',', int2str(y), '}, {', ...
0111     int2str(r), ',', int2str(r),'}}</string>\n']);
0112 write_key(fid,'Class');
0113 write_string(fid, 'ShapedGraphic');
0114 write_key(fid,'ID');
0115 write_integer(fid,v);
0116 write_key(fid,'Shape');
0117 write_string(fid,'Circle');
0118 write_key(fid,'Style');
0119 fprintf(fid,' <dict>\n');
0120 write_key(fid,'shadow');
0121 fprintf(fid,' <dict>\n');
0122 write_key(fid,'Draws');
0123 write_string(fid, 'NO');
0124 fprintf(fid,' </dict>\n </dict>\n</dict>\n\n');
0125 
0126 
0127 
0128 
0129 % write an edge
0130 function write_edge(fid, v, w)
0131 fprintf(fid,'<dict>\n');
0132 write_key(fid,'Class');
0133 write_string(fid,'LineGraphic');
0134 
0135 write_key(fid,'Head');
0136 fprintf(fid,' <dict>\n');
0137 write_key(fid,'ID');
0138 write_integer(fid,v);
0139 fprintf(fid,' </dict>\n');
0140 
0141 
0142 write_key(fid,'Tail');
0143 fprintf(fid,' <dict>\n');
0144 write_key(fid,'ID');
0145 write_integer(fid,w);
0146 fprintf(fid,' </dict>\n');
0147 
0148 fprintf(fid,'</dict>\n');
0149 
0150

Generated on Thu 13-Mar-2008 14:23:52 by m2html © 2003