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 addEvent(obj, type, fn) { if (obj.addEventListener) obj.addEventListener(type, fn, false); else obj.attachEvent('on' + type, fn); }; function init() { //init data var json = { 'id': 'root', 'name': 'RGraph( RGraph )', 'data': { '$type': 'none' }, 'children':[ { 'id':'pie10', 'name': 'pie1', 'data': { '$aw': 20, '$color': '#f55' }, 'children': [ { 'id':'pie100', 'name': 'pc1', 'data': { '$aw': 20, '$color': '#55f' }, 'children': [] }, { 'id':'pie101', 'name': 'pc2', 'data': { '$aw': 70, '$color': '#66f' }, 'children': [] }, { 'id':'pie102', 'name': 'pc3', 'data': { '$aw': 10, '$color': '#77f' }, 'children': [] } ] }, { 'id':'pie20', 'name': 'pie2', 'data': { '$aw': 40, '$color': '#f77' }, 'children': [ { 'id':'pie200', 'name': 'pc1', 'data': { '$aw': 40, '$color': '#88f' }, 'children': [] }, { 'id':'pie201', 'name': 'pc2', 'data': { '$aw': 60, '$color': '#99f' }, 'children': [] } ] }, { 'id':'pie30', 'name': 'pie3', 'data': { '$aw': 10, '$color': '#f99' }, 'children': [ { 'id':'pie300', 'name': 'pc1', 'data': { '$aw': 100, '$color': '#aaf' }, 'children': [] } ] } ] }; var jsonpie = { 'id': 'root', 'name': 'RGraph based Pie Chart', 'data': { '$type': 'none' }, 'children':[ { 'id':'pie1', 'name': 'pie1', 'data': { '$aw': 20, '$color': '#f55' }, 'children': [] }, { 'id':'pie2', 'name': 'pie2', 'data': { '$aw': 40, '$color': '#f77' }, 'children': [] }, { 'id':'pie3', 'name': 'pie3', 'data': { '$aw': 10, '$color': '#f99' }, 'children': [] }, { 'id':'pie4', 'name': 'pie4', 'data': { '$aw': 30, '$color': '#fbb' }, 'children': [] } ] }; //end var infovis = document.getElementById('infovis'); var w = infovis.offsetWidth, h = infovis.offsetHeight; //init canvas //Create new canvas instances. var canvas = new Canvas('mycanvas', { 'injectInto': 'infovis', 'width': w, 'height': h, //Optional: create a background canvas and plot //concentric circles in it. 'backgroundCanvas': { 'styles': { 'strokeStyle': '#555' }, 'impl': { 'init': function(){}, 'plot': function(canvas, ctx){ var times = 4, d = 100; var pi2 = Math.PI * 2; for (var i = 1; i <= times; i++) { ctx.beginPath(); ctx.arc(0, 0, i * d, 0, pi2, true); ctx.stroke(); ctx.closePath(); } } } } }); //end //init nodetypes //Here we implement custom node rendering types for the RGraph //Using this feature requires some javascript and canvas experience. RGraph.Plot.NodeTypes.implement({ //This node type is used for plotting pie-chart slices as nodes 'nodepie': function(node, canvas) { var span = node.angleSpan, begin = span.begin, end = span.end; var polarNode = node.pos.getp(true); var polar = new Polar(polarNode.rho, begin); var p1coord = polar.getc(true); polar.theta = end; var p2coord = polar.getc(true); var ctx = canvas.getCtx(); ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(p1coord.x, p1coord.y); ctx.moveTo(0, 0); ctx.lineTo(p2coord.x, p2coord.y); ctx.moveTo(0, 0); ctx.arc(0, 0, polarNode.rho, begin, end, false); ctx.fill(); }, //Create a new node type that renders an entire RGraph visualization //as node 'piechart': function(node, canvas, animating) { var ctx = canvas.getCtx(), pos = node.pos.getc(true); ctx.save(); ctx.translate(pos.x, pos.y); pie.plot(); ctx.restore(); } }); //end //init pie //This RGraph instance will be used as the node for //another RGraph instance. var pie = new RGraph(canvas, { //Add node/edge styles and set //overridable=true if you want your //styles to be individually overriden Node: { 'overridable': true, 'type':'nodepie' }, Edge: { 'type':'none' }, //Parent-children distance levelDistance: 30, //Don't create labels in this visualization withLabels: false, //Don't clear the entire canvas when plotting //this visualization clearCanvas: false }); //load graph. pie.loadJSON(jsonpie); pie.compute(); //end //init rgraph var rgraph = new RGraph(canvas, { //Add node/edge styles and set //overridable=true if you want your //styles to be individually overriden Node: { //set the RGraph rendering function //as node type 'type': 'piechart' }, Edge: { color: '#772277' }, //Parent-children distance levelDistance: 100, //Add styles to node labels on label creation onCreateLabel: function(domElement, node){ domElement.innerHTML = node.name; var style = domElement.style; style.fontSize = "0.8em"; style.color = "#fff"; style.cursor = "pointer"; domElement.onclick = function() { rgraph.onClick(node.id, { hideLabels: false }); }; }, onPlaceLabel: function(domElement, node){ var style = domElement.style; var left = parseInt(style.left); var w = domElement.offsetWidth; style.left = (left - w / 2) + 'px'; style.display = ''; } }); //load graph. rgraph.loadJSON(json); rgraph.refresh(); //end }