{"id":229,"date":"2009-03-23T09:05:04","date_gmt":"2009-03-23T16:05:04","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1014"},"modified":"2009-03-23T09:05:04","modified_gmt":"2009-03-23T16:05:04","slug":"morenestingfunctionsgraphdrawing","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=229","title":{"rendered":"more(nesting(functions(graphDrawing)));"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width=800, height=600)]\r\n\r\nvar canvas:Graphics; \r\nvar graphData:Array = sineData();\r\n\r\nvar graph0:Shape = Shape(addChild(new Shape()));\r\ngraph0.x = 50;\r\ngraph0.y = 150;\r\n\r\nvar graph1:Shape = Shape(addChild(new Shape()));\r\ngraph1.x = 400;\r\ngraph1.y = 150;\r\n\r\nvar graph2:Shape = Shape(addChild(new Shape()));\r\ngraph2.x = 50;\r\ngraph2.y = 400;\r\n\r\n\/\/ use graphData to draw 3 different looking graphs:\r\n\r\ncanvas = graph0.graphics;\r\naxis(lines(graphData));\r\n\r\ncanvas = graph1.graphics;\r\naxis(dots(graphData, 0xFF0000), 0xFFCC00, 2);\r\n\r\ncanvas = graph2.graphics;\r\naxis(dots(dots(lines(lines(graphData, 0xCCCCCC, 20))), 0x0022FF, 0, 4), 0xFF);\r\n\r\n\r\n\/\/ generate data\r\nfunction sineData():Array{\r\n\tvar dat:Array = new Array();\r\n\tfor (var i:int = 0; i&lt;60; i++){\r\n\t\tdat.push(new Point(i * 4,  (30 + i) * Math.sin(i * 24 * Math.PI\/180)));\r\n\t}            \r\n\treturn dat;\r\n}\r\n\r\n\/\/ render lines\r\nfunction lines(dat:Array, col:uint=0x000000, thick:Number=0):Array{\r\n\tcanvas.lineStyle(thick, col);\r\n\tcanvas.moveTo(dat&#x5B;0].x, dat&#x5B;0].y)\r\n\tfor (var i:int = 1; i&lt;dat.length; i++){\r\n\t\t canvas.lineTo(dat&#x5B;i].x, dat&#x5B;i].y);\r\n\t}\r\n\treturn dat;\r\n}\r\n\r\n\/\/ render dots\r\nfunction dots(dat:Array, col:uint=0xFF0000, thick:Number=0, rad:Number=1.5):Array{\r\n\tcanvas.lineStyle(thick, col);\r\n\tfor (var i:int = 0; i&lt;dat.length; i++){\r\n\t\t canvas.drawCircle(dat&#x5B;i].x, dat&#x5B;i].y, rad);\r\n\t}\r\n\treturn dat;\r\n}\r\n\r\n\/\/ render graph axis\r\nfunction axis(dat:Array, col:uint=0x000000, thick:Number=0):Array{\r\n\tvar d:Array = dat.concat();\r\n\td.sortOn(&quot;y&quot;, Array.NUMERIC);\r\n\tvar lastIndex:int = d.length - 1;\r\n\tvar minY:Number = d&#x5B;0].y;\r\n\tvar maxY:Number = d&#x5B;lastIndex].y;\r\n\td.sortOn(&quot;x&quot;, Array.NUMERIC);\r\n\tvar minX:Number = d&#x5B;0].x;\r\n\tvar maxX:Number = d&#x5B;lastIndex].x;\r\n\tcanvas.lineStyle(thick, col, .2);\r\n\tcanvas.moveTo(minX, 0);\r\n\tcanvas.lineTo(maxX, 0);\r\n\tcanvas.lineStyle(thick, col);\r\n\tcanvas.moveTo(minX, minY);\r\n\tcanvas.lineTo(minX, maxY);\r\n\tcanvas.lineTo(maxX, maxY);\r\n\treturn dat;\r\n}\r\n<\/pre>\n<p>This is something I&#8217;ve been meaning to post for awhile. Finally had time to write it today&#8230; It contains functions that are designed to be nested for the purpose of rendering a small data set in a few different ways&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/graphs.jpg\" \/><\/p>\n<p>The upper left image is rendered with axis labels and lines&#8230; and it defaults to the color black (line 21):<\/p>\n<p><code>axis(lines(graphData));<\/code><\/p>\n<p>The upper right image is rendered with yellow axis and red dots (line 24):<\/p>\n<p><code>axis(dots(graphData, 0xFF0000), 0xFFCC00, 2);<\/code><\/p>\n<p>etc&#8230; (line 27)<br \/>\naxis(dots(dots(lines(lines(graphData, 0xCCCCCC, 20))), 0x0022FF, 0, 4), 0xFF);<\/p>\n<p>Alternatively you could write each function call on one line:<\/p>\n<p><code>lines(graphData, 0xCCCCCC, 20);<br \/>\nlines(graphData);<br \/>\ndots(graphData);<br \/>\ndots(graphData, 0x0022FF, 0, 4)<br \/>\naxis(graphData, 0xFF);<\/code><\/p>\n<p><i>NOTE: If you think this post is insane, please read <a href=\"https:\/\/actionsnippet.com\/?page_id=167\">the warning page<\/a> of this site&#8230;<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width=800, height=600)] var canvas:Graphics; var graphData:Array = sineData(); var graph0:Shape = Shape(addChild(new Shape())); graph0.x = 50; graph0.y = 150; var graph1:Shape = Shape(addChild(new Shape())); graph1.x = 400; graph1.y = 150; var graph2:Shape = Shape(addChild(new Shape())); graph2.x = 50; graph2.y = 400; \/\/ use graphData to draw 3 different looking graphs: canvas = graph0.graphics; axis(lines(graphData)); canvas [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,33,94],"tags":[3,4],"class_list":["post-229","post","type-post","status-publish","format-standard","hentry","category-functions","category-graphics","category-misc","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=229"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/229\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}