Animation and Data Visualization in JavaScript. by Kyle Scholz for The Ajax Experience October 24 th , 2006. Overview. Data Visualization and Animation Data Visualization Examples Popular Layout Algorithms Why JavaScript? / Challenges Developer’s Toolbox Tutorials
01 / 77Animation and Data Visualization in JavaScript
1 / 17
Download Presentation
Animation and Data Visualization in JavaScript
An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
Overview Data Visualization and Animation • Data Visualization Examples • Popular Layout Algorithms • Why JavaScript? / Challenges • Developer’s Toolbox Tutorials • Modeling Static Data • Modeling Music Recommendations Animation and Data Visualization in JavaScript
Resource http://www.visualcomplexity.com/ Beautifully maintained catalog of “complex network” visualization.
Layout Generating Visualizations from Dynamic Data
Layout with Connected Graphs • Graphs are visually accessible Convey meaning by visually expressing relationships with connections, colors, and proximity. • Graphs are dynamic A single layout algorithm can service variable data sets. Animation and Data Visualization in JavaScript
Tree Layouts The Good • Familiar metaphor – lot’s of data is structured in trees (hierarchies) • Low computational complexity The Bad • Organized using geometry – May result in a lot of overlap – Can balance. • Can’t Support Circuits Animation and Data Visualization in JavaScript
Force Directed (or “Spring”) Layouts • Based on a physical model: How electrons organize in space. • Particles are the nodes in our graph. • Attractive (“Spring”) Forces are our edges. • Repulsive Forces exist between each node in the model. Animation and Data Visualization in JavaScript
Particles and Forces P3 P2 P1 Key: P Particle Spring Force Repulsive Force P4 Animation and Data Visualization in JavaScript
Why Force Directed Layouts? The Good • Produce graphs with or without circuits • Draw as few crossing edges as possible • Neat to look at and fun to use • Animated and interactive The Bad • High Computational Complexity: On2 Animation and Data Visualization in JavaScript
Why Animate? Problem • Rendering takes time. How do we know when we’re done? • Complete data set may not be immediately available. Solution • We show the user intermediate states so they can begin interacting sooner. Animation and Data Visualization in JavaScript
Why use JavaScript? • Flash, Java, ActiveX alternatives require plug-ins – Slow load time / Limited audience. • HTML offers rich, familiar presentation elements. • A JavaScript-powered graph can interact with the rest of your JavaScript application. Animation and Data Visualization in JavaScript
Challenges We aren’t seriously going to do this in JavaScript are we?
Performance We’re breaking from tradition, asking JavaScript to do some pretty intense stuff. Can JavaScript handle this? “JavaScript was designed for simple little web pages…” --(paraphrasing) IEBlog Animation and Data Visualization in JavaScript
JavaScript is 100X slower than Java? • A frequently quoted statistic claims JavaScript is: • 5000X slower than C • 100X slower than Java • Is this true? • Maybe, buy we’re comparing apples and oranges (and bananas) – Really depends on operations compared. • But is it plausible? Absolutely. Animation and Data Visualization in JavaScript
Why is JavaScript Slow? • Symbol lookup JavaScript uses late binding – Any time we use a variable, we have to search through the scope chain to find it. • No types The meaning of an operator (like “+”) depends on whether arguments can be parsed as Strings or Numbers. • Closures, Garbage Collection, etc… Animation and Data Visualization in JavaScript
Optimizing JavaScript • Optimization is a huge topic, worthy of it’s own talk. • For now, remember: • Before you start chasing milliseconds, optimize your algorithms. • Cache frequently used variables. • Keep variables local. Never store frequently used variables in the global scope. Animation and Data Visualization in JavaScript
HTML and the DOM • Sure, we can animate HTML! • We’ll need to use images and hacky solutions to make shapes and lines. • We control element positions using style attributes. Animation and Data Visualization in JavaScript
SVG == Awesome • Draws lines and many shapes • Access through the DOM • var shape = document.createElementNS(…) • shape.setAttribute( “x” 100 ) • Prettier than HTML hacks • Need plug-in to view in IE • Partially implemented in Firefox and Opera • Available in Safari soon Animation and Data Visualization in JavaScript
What’s JSViz? • A JavaScript library for generating animated connected graphs. • Currently supports Tree and Force Directed layouts. • Provides extendable architecture that can support additional layouts. Animation and Data Visualization in JavaScript
What does JSViz do? • Implements a time-controlled model that dictates positions of nodes. • Implements a view that controls display of nodes and relationships. Animation and Data Visualization in JavaScript
So What are We Building? • A Force directed Graph that models the contents of an XML file. Animation and Data Visualization in JavaScript
XML Data <root mass="1" color="#dddddd"> <node mass="1" color="#90ee90"> <node mass="1" color="#aaaaaa"></node> <node mass="1" color="#aaaaaa"> <node mass="1" color="#ddd8e6"></node> <node mass="1" color="#add8e6"></node> <node mass="1" color="#add8e6 "></node> <node mass="1" color="#add8e6 "> <node mass="1" color="#ff8888"> <node mass="1" color="#ffaa44"></node> <node mass="1" color="#ffaa44"></node> (...continues) Animation and Data Visualization in JavaScript
Using JSVizStep 1: Make data accessible Create a Loader to process data into DataGraph Your Data Loader DataGraph Animation and Data Visualization in JavaScript
XMLLoader.js var XMLLoader = function( dataGraph ) { this.init( dataGraph ); } XMLLoader.prototype = { init: function( dataGraph ) { this.http = new HTTP(); this.dataGraph = dataGraph; }, load: function( url ) { this.http.get( url, this, this.handle ); } } Animation and Data Visualization in JavaScript
XMLLoader.js (2) handle: function( request ) { var xmlDoc = request.responseXML; var root = xmlDoc.getElementsByTagName("root")[0]; // add Root Node var rootNode = new DataGraphNode( true ); var mass = root.getAttribute("mass"); rootNode.mass = mass; var color = root.getAttribute("color"); rootNode.color = color; this.dataGraph.addNode( rootNode ); // add children this.branch( root, rootNode ); }, Animation and Data Visualization in JavaScript
XMLLoader.js (3) branch: function( root, rootNode ) { var childNodes = root.childNodes; for( var i=0, l=childNodes.length; i<l; i++ ){ if( childNodes[i].nodeName == "node" ) { var node = new DataGraphNode( false ); node.parent = rootNode; var mass = childNodes[i].getAttribute("mass"); node.mass = mass; var color = childNodes[i].getAttribute("color"); node.color = color; this.dataGraph.addNode( node ); this.branch( childNodes[i], node ); } } } Animation and Data Visualization in JavaScript
Set up the HTML <html> <head> <script> function init() {} </script> <style type="text/css"> html { filter: expression( document.execCommand( "BackgroundImageCache", false, true)); } body { margin: 0; padding: 0; } </style> </head> <body onload="init()"> </body> </html> Animation and Data Visualization in JavaScript
Get the Dimensions of the Page var FRAME_WIDTH; var FRAME_HEIGHT; if (document.all) { FRAME_WIDTH = document.body.offsetWidth - 5; FRAME_HEIGHT = document.documentElement.offsetHeight - 5; } else { FRAME_WIDTH = window.innerWidth - 5; FRAME_HEIGHT = window.innerHeight - 5; } Animation and Data Visualization in JavaScript
Initialize Components var view; if ( document.implementation.hasFeature( "org.w3c.dom.svg", '1.1') ) { view = new SVGGraphView( 0, 0, FRAME_WIDTH, FRAME_HEIGHT ); } else { view = new HTMLGraphView( 0, 0, FRAME_WIDTH, FRAME_HEIGHT ); } var particleModel = new ParticleModel( view ); particleModel.start(); var control = new Control( particleModel, view ); var dataGraph = new DataGraph(); var nodeHandler = new NodeHandler( dataGraph, particleModel, view, control ); dataGraph.subscribe( nodeHandler ); Animation and Data Visualization in JavaScript
Initialize Components (2) Initialize our XMLLoader var xmlLoader = new XMLLoader( dataGraph ); xmlLoader.load( "nodes.xml" ); Also, we want to add particles to the model over time, enabling the model to organize under less entropy. var buildTimer = new Timer( 150 ); buildTimer.subscribe( nodeHandler ); buildTimer.start(); Animation and Data Visualization in JavaScript