Hello,
I'm using infoviz and web2py and trying to create the Force Directed graph.
here is my code:
************************************************************************controller:
def visual():
import json
import os
infile = open(os.path.join(request.folder, 'private', 'jsondata.txt'))
data = json.load(infile)
return dict(data=data)
************************************************************************
view:
{{extend 'layout.html'}}
<head>
<title>ForceDirected - Force Directed Static Graph</title>
<!-- CSS Files -->
<link type="text/css" href="{{=URL('static','css/base.css')}}"
rel="stylesheet" />
<link type="text/css" href="{{=URL('static','css/ForceDirected.css')}}"
rel="stylesheet" />
<!-- JIT Library File -->
<script language="javascript" type="text/javascript"
src="{{=URL('static','js/jit.js')}}"></script>
<!-- Example File -->
<script language="javascript" type="text/javascript"
src="{{=URL('static','js/example1.js')}}"></script>
</head>
{{=data}}
<body onload="init();">
<div id="container">
<div id="left-container">
<div class="text">
<h4>
Force Directed Static Graph
</h4>
A static JSON Graph structure is used as input for this
visualization.<br /><br />
You can <b>zoom</b> and <b>pan</b> the visualization by
<b>scrolling</b> and <b>dragging</b>.<br /><br />
You can <b>change node positions</b> by <b>dragging the nodes
around</b>.<br /><br />
The clicked node's connections are displayed in a relations
list in the right column.<br /><br />
The JSON static data is customized to provide different node
types, colors and widths.
</div>
<div id="id-list"></div>
</div>
<div id="center-container">
<div id="infovis"></div>
</div>
<div id="right-container">
<div id="inner-details"></div>
</div>
<div id="log"></div>
</div>
</body>
</html>
************************************************************************example1.js
file:
var labelType, useGradients, nativeTextSupport, animate;
(function() {
var ua = navigator.userAgent,
iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
typeOfCanvas = typeof HTMLCanvasElement,
nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas ==
'function'),
textSupport = nativeCanvasSupport
&& (typeof
document.createElement('canvas').getContext('2d').fillText == 'function');
//I'm setting this based on the fact that ExCanvas provides text support
for IE
//and that as of today iPhone/iPad current text support is lame
labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native'
: 'HTML';
nativeTextSupport = labelType == 'Native';
useGradients = nativeCanvasSupport;
animate = !(iStuff || !nativeCanvasSupport);
})();
var Log = {
elem: false,
write: function(text){
if (!this.elem)
this.elem = document.getElementById('log');
this.elem.innerHTML = text;
this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
}
};
function init(){
// init data
var json = {{=XML(data)}};
// end
// init ForceDirected
var fd = new $jit.ForceDirected({
//id of the visualization container
injectInto: 'infovis',
//Enable zooming and panning
//by scrolling and DnD
Navigation: {
enable: true,
//Enable panning events only if we're dragging the empty
//canvas (and not a node).
panning: 'avoid nodes',
zooming: 10 //zoom speed. higher is more sensible
},
// Change node and edge styles such as
// color and width.
// These properties are also set per node
// with dollar prefixed data-properties in the
// JSON structure.
Node: {
overridable: true
},
Edge: {
overridable: true,
color: '#23A4FF',
lineWidth: 0.4
},
//Native canvas text styling
Label: {
type: labelType, //Native or HTML
size: 10,
style: 'bold'
},
//Add Tips
Tips: {
enable: true,
onShow: function(tip, node) {
//count connections
var count = 0;
node.eachAdjacency(function() { count++; });
//display node info in tooltip
tip.innerHTML = "<div class=\"tip-title\">" + node.name + "</div>"
+ "<div class=\"tip-text\"><b>connections:</b> " + count +
"</div>";
}
},
// Add node events
Events: {
enable: true,
type: 'Native',
//Change cursor style when hovering a node
onMouseEnter: function() {
fd.canvas.getElement().style.cursor = 'move';
},
onMouseLeave: function() {
fd.canvas.getElement().style.cursor = '';
},
//Update node positions when dragged
onDragMove: function(node, eventInfo, e) {
var pos = eventInfo.getPos();
node.pos.setc(pos.x, pos.y);
fd.plot();
},
//Implement the same handler for touchscreens
onTouchMove: function(node, eventInfo, e) {
$jit.util.event.stop(e); //stop default touchmove event
this.onDragMove(node, eventInfo, e);
},
//Add also a click handler to nodes
onClick: function(node) {
if(!node) return;
// Build the right column relations list.
// This is done by traversing the clicked node connections.
var html = "<h4>" + node.name + "</h4><b> connections:</b><ul><li>",
list = [];
node.eachAdjacency(function(adj){
list.push(adj.nodeTo.name);
});
//append connections information
$jit.id('inner-details').innerHTML = html + list.join("</li><li>")
+ "</li></ul>";
}
},
//Number of iterations for the FD algorithm
iterations: 200,
//Edge length
levelDistance: 130,
// Add text to the labels. This method is only triggered
// on label creation and only for DOM labels (not native canvas ones).
onCreateLabel: function(domElement, node){
domElement.innerHTML = node.name;
var style = domElement.style;
style.fontSize = "0.8em";
style.color = "#ddd";
},
// Change node styles when DOM labels are placed
// or moved.
onPlaceLabel: function(domElement, node){
var style = domElement.style;
var left = parseInt(style.left);
var top = parseInt(style.top);
var w = domElement.offsetWidth;
style.left = (left - w / 2) + 'px';
style.top = (top + 10) + 'px';
style.display = '';
}
});
// load JSON data.
fd.loadJSON(json);
// compute positions incrementally and animate.
fd.computeIncremental({
iter: 40,
property: 'end',
onStep: function(perc){
Log.write(perc + '% loaded...');
},
onComplete: function(){
Log.write('done');
fd.animate({
modes: ['linear'],
transition: $jit.Trans.Elastic.easeOut,
duration: 2500
});
}
});
// end
}
**********************************************************
I've been trying to follow the examples of first converting the json data
to XML, and it looks correct in the view when I type {{=data}} to make sure
the json is getting passed properly.
However when I type var json = {{=XML(data)}}; in my example1.js folder
nothing happens. Do I need to somehow load the data into the view before
the javascript starts processing it?
Here is the json data:
[{u'adjacencies': [u'graphnode21', {u'nodeTo': u'graphnode1', u'nodeFrom':
u'graphnode0', u'data': {u'$color': u'#557EAA'}}, {u'nodeTo':
u'graphnode13', u'nodeFrom': u'graphnode0', u'data': {u'$color':
u'#909291'}}, {u'nodeTo': u'graphnode14', u'nodeFrom': u'graphnode0',
u'data': {u'$color': u'#557EAA'}}, {u'nodeTo': u'graphnode15', u'nodeFrom':
u'graphnode0', u'data': {u'$color': u'#557EAA'}}, {u'nodeTo':
u'graphnode16', u'nodeFrom': u'graphnode0', u'data': {u'$color':
u'#557EAA'}}, {u'nodeTo': u'graphnode17', u'nodeFrom': u'graphnode0',
u'data': {u'$color': u'#557EAA'}}], u'data': {u'$type': u'circle', u'$dim':
10, u'$color': u'#83548B'}, u'id': u'graphnode0', u'name': u'graphnode0'},
{u'adjacencies': [{u'nodeTo': u'graphnode2', u'nodeFrom': u'graphnode1',
u'data': {u'$color': u'#557EAA'}}, {u'nodeTo': u'graphnode4', u'nodeFrom':
u'graphnode1', u'data': {u'$color': u'#909291'}}, {u'nodeTo':
u'graphnode5', u'nodeFrom': u'graphnode1', u'data': {u'$color':
u'#909291'}}, {u'nodeTo': u'graphnode6', u'nodeFrom': u'graphnode1',
u'data': {u'$color': u'#909291'}}, {u'nodeTo': u'graphnode7', u'nodeFrom':
u'graphnode1', u'data': {u'$color': u'#909291'}}, {u'nodeTo':
u'graphnode8', u'nodeFrom': u'graphnode1', u'data': {u'$color':
u'#909291'}}, {u'nodeTo': u'graphnode10', u'nodeFrom': u'graphnode1',
u'data': {u'$color': u'#557EAA'}}, {u'nodeTo': u'graphnode11', u'nodeFrom':
u'graphnode1', u'data': {u'$color': u'#909291'}}, {u'nodeTo':
u'graphnode12', u'nodeFrom': u'graphnode1', u'data': {u'$color':
u'#909291'}}, {u'nodeTo': u'graphnode13', u'nodeFrom': u'graphnode1',
u'data': {u'$color': u'#557EAA'}}, {u'nodeTo': u'graphnode14', u'nodeFrom':
u'graphnode1', u'data': {u'$color': u'#557EAA'}}, {u'nodeTo':
u'graphnode15', u'nodeFrom': u'graphnode1', u'data': {u'$color':
u'#557EAA'}}, {u'nodeTo': u'graphnode16', u'nodeFrom': u'graphnode1',
u'data': {u'$color': u'#909291'}}, {u'nodeTo': u'graphnode17', u'nodeFrom':
u'graphnode1', u'data': {u'$color': u'#557EAA'}}], u'data': {u'$type':
u'circle', u'$dim': 11, u'$color': u'#EBB056'}, u'id': u'graphnode1',
u'name': u'graphnode1'}, {u'adjacencies': [{u'nodeTo': u'graphnode5',
u'nodeFrom': u'graphnode2', u'data': {u'$color': u'#909291'}}, {u'nodeTo':
u'graphnode9', u'nodeFrom': u'graphnode2', u'data': {u'$color':
u'#557EAA'}}, {u'nodeTo': u'graphnode18', u'nodeFrom': u'graphnode2',
u'data': {u'$color': u'#557EAA'}}], u'data': {u'$type': u'circle', u'$dim':
7, u'$color': u'#416D9C'}, u'id': u'graphnode2', u'name': u'graphnode2'},
{u'adjacencies': [{u'nodeTo': u'graphnode5', u'nodeFrom': u'graphnode3',
u'data': {u'$color': u'#909291'}}, {u'nodeTo': u'graphnode9', u'nodeFrom':
u'graphnode3', u'data': {u'$color': u'#557EAA'}}, {u'nodeTo':
u'graphnode10', u'nodeFrom': u'graphnode3', u'data': {u'$color':
u'#557EAA'}}, {u'nodeTo': u'graphnode12', u'nodeFrom': u'graphnode3',
u'data': {u'$color': u'#557EAA'}}], u'data': {u'$type': u'square', u'$dim':
10, u'$color': u'#416D9C'}, u'id': u'graphnode3', u'name': u'graphnode3'},
{u'adjacencies': [], u'data': {u'$type': u'square', u'$dim': 11, u'$color':
u'#83548B'}, u'id': u'graphnode4', u'name': u'graphnode4'},
{u'adjacencies': [{u'nodeTo': u'graphnode9', u'nodeFrom': u'graphnode5',
u'data': {u'$color': u'#909291'}}], u'data': {u'$type': u'triangle',
u'$dim': 8, u'$color': u'#C74243'}, u'id': u'graphnode5', u'name':
u'graphnode5'}, {u'adjacencies': [{u'nodeTo': u'graphnode10', u'nodeFrom':
u'graphnode6', u'data': {u'$color': u'#557EAA'}}, {u'nodeTo':
u'graphnode11', u'nodeFrom': u'graphnode6', u'data': {u'$color':
u'#557EAA'}}], u'data': {u'$type': u'circle', u'$dim': 11, u'$color':
u'#83548B'}, u'id': u'graphnode6', u'name': u'graphnode6'},
{u'adjacencies': [], u'data': {u'$type': u'triangle', u'$dim': 12,
u'$color': u'#EBB056'}, u'id': u'graphnode7', u'name': u'graphnode7'},
{u'adjacencies': [], u'data': {u'$type': u'star', u'$dim': 10, u'$color':
u'#C74243'}, u'id': u'graphnode8', u'name': u'graphnode8'},
{u'adjacencies': [], u'data': {u'$type': u'circle', u'$dim': 12, u'$color':
u'#83548B'}, u'id': u'graphnode9', u'name': u'graphnode9'},
{u'adjacencies': [{u'nodeTo': u'graphnode11', u'nodeFrom': u'graphnode10',
u'data': {u'$color': u'#909291'}}], u'data': {u'$type': u'triangle',
u'$dim': 11, u'$color': u'#70A35E'}, u'id': u'graphnode10', u'name':
u'graphnode10'}, {u'adjacencies': [], u'data': {u'$type': u'circle',
u'$dim': 11, u'$color': u'#70A35E'}, u'id': u'graphnode11', u'name':
u'graphnode11'}, {u'adjacencies': [], u'data': {u'$type': u'triangle',
u'$dim': 10, u'$color': u'#83548B'}, u'id': u'graphnode12', u'name':
u'graphnode12'}, {u'adjacencies': [{u'nodeTo': u'graphnode14', u'nodeFrom':
u'graphnode13', u'data': {u'$color': u'#557EAA'}}], u'data': {u'$type':
u'star', u'$dim': 7, u'$color': u'#EBB056'}, u'id': u'graphnode13',
u'name': u'graphnode13'}, {u'adjacencies': [], u'data': {u'$type':
u'triangle', u'$dim': 12, u'$color': u'#EBB056'}, u'id': u'graphnode14',
u'name': u'graphnode14'}, {u'adjacencies': [{u'nodeTo': u'graphnode16',
u'nodeFrom': u'graphnode15', u'data': {u'$color': u'#557EAA'}}, {u'nodeTo':
u'graphnode17', u'nodeFrom': u'graphnode15', u'data': {u'$color':
u'#557EAA'}}], u'data': {u'$type': u'triangle', u'$dim': 11, u'$color':
u'#83548B'}, u'id': u'graphnode15', u'name': u'graphnode15'},
{u'adjacencies': [{u'nodeTo': u'graphnode17', u'nodeFrom': u'graphnode16',
u'data': {u'$color': u'#557EAA'}}], u'data': {u'$type': u'star', u'$dim':
7, u'$color': u'#C74243'}, u'id': u'graphnode16', u'name': u'graphnode16'},
{u'adjacencies': [], u'data': {u'$type': u'circle', u'$dim': 7, u'$color':
u'#416D9C'}, u'id': u'graphnode17', u'name': u'graphnode17'},
{u'adjacencies': [{u'nodeTo': u'graphnode19', u'nodeFrom': u'graphnode18',
u'data': {u'$color': u'#557EAA'}}, {u'nodeTo': u'graphnode20', u'nodeFrom':
u'graphnode18', u'data': {u'$color': u'#557EAA'}}], u'data': {u'$type':
u'triangle', u'$dim': 9, u'$color': u'#EBB056'}, u'id': u'graphnode18',
u'name': u'graphnode18'}, {u'adjacencies': [], u'data': {u'$type':
u'circle', u'$dim': 8, u'$color': u'#70A35E'}, u'id': u'graphnode19',
u'name': u'graphnode19'}, {u'adjacencies': [], u'data': {u'$type': u'star',
u'$dim': 8, u'$color': u'#C74243'}, u'id': u'graphnode20', u'name':
u'graphnode20'}]
Its right out of the example from infoviz
Really appreciate any help and thank you for such a beautiful framework.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.