User Tools

Site Tools


vincent_bocquel:solution_homework1

Préambule

Pour ce premier devoir, nous avons choisi d'implémenter l'algorithme d'icicle plot.

Présentation

Notre algorithme ajoute une propriété au graphe permettant de calculer le nombre de fils de chacun des noeufs de l'arbre. Grâce à cela, nous pouvons ainsi calculer facilement la taille ainsi que la position des rectangles représentant les sommets.

Par exemple sur un arbre tel que :

Notre script donnera au final :

Code Source

# Powered by Python 2.7

# To cancel the modifications performed by the script
# on the current graph, click on the undo button.

# Some useful keyboards shortcuts : 
#   * Ctrl + D : comment selected lines.
#   * Ctrl + Shift + D  : uncomment selected lines.
#   * Ctrl + I : indent selected lines.
#   * Ctrl + Shift + I  : unindent selected lines.
#   * Ctrl + Return  : run script.
#   * Ctrl + F  : find selected text.
#   * Ctrl + R  : replace selected text.
#   * Ctrl + Space  : show auto-completion dialog.

from tulip import *
from time import sleep

# the updateVisualization(centerViews = True) function can be called
# during script execution to update the opened views

# the pauseScript() function can be called to pause the script execution.
# To resume the script execution, you will have to click on the "Run script " button.

# the runGraphScript(scriptFile, graph) function can be called to launch another edited script on a tlp.Graph object.
# The scriptFile parameter defines the script name to call (in the form [a-zA-Z0-9_]+.py)

# the main(graph) function must be defined 
# to run the script on the current graph

def main(graph): 
	viewBorderColor =  graph.getColorProperty("viewBorderColor")
	viewBorderWidth =  graph.getDoubleProperty("viewBorderWidth")
	viewColor =  graph.getColorProperty("viewColor")
	viewFont =  graph.getStringProperty("viewFont")
	viewFontSize =  graph.getIntegerProperty("viewFontSize")
	viewLabel =  graph.getStringProperty("viewLabel")
	viewLabelBorderColor =  graph.getColorProperty("viewLabelBorderColor")
	viewLabelBorderWidth =  graph.getDoubleProperty("viewLabelBorderWidth")
	viewLabelColor =  graph.getColorProperty("viewLabelColor")
	viewLabelPosition =  graph.getIntegerProperty("viewLabelPosition")
	viewLayout =  graph.getLayoutProperty("viewLayout")
	viewMetaGraph =  graph.getGraphProperty("viewMetaGraph")
	viewMetric =  graph.getDoubleProperty("viewMetric")
	viewRotation =  graph.getDoubleProperty("viewRotation")
	viewSelection =  graph.getBooleanProperty("viewSelection")
	viewShape =  graph.getIntegerProperty("viewShape")
	viewSize =  graph.getSizeProperty("viewSize")
	viewSrcAnchorShape =  graph.getIntegerProperty("viewSrcAnchorShape")
	viewSrcAnchorSize =  graph.getSizeProperty("viewSrcAnchorSize")
	viewTexture =  graph.getStringProperty("viewTexture")
	viewTgtAnchorShape =  graph.getIntegerProperty("viewTgtAnchorShape")
	viewTgtAnchorSize =  graph.getSizeProperty("viewTgtAnchorSize")

	# Ajout de bordure pour distinguer les freres et les differents étages
	viewBorderColor.setAllNodeValue(tlp.Color.Black)	
	viewBorderWidth.setAllNodeValue(1)

	# Définition d'une nouvelle propriété stockant l'ordre
	mySizeNode = graph.getIntegerProperty("mySizeNode")
	
	root = graph.getSource()
	setMySizeNode(root, mySizeNode)
	drawNode(viewSize, viewShape, viewLayout, mySizeNode, root, 0, 0)

def drawNode(viewSize, viewShape, viewLayout, mySizeNode, node, x, y):
#		Fonction récursive permettant de définir la taille du
#		rectangle selon le nombre de fils et de le positionner correctement
	viewLayout.setNodeValue(node, tlp.Coord(x+mySizeNode.getNodeValue(node), y-1, 0))
	viewShape.setNodeValue(node, tlp.NodeShape.Square)
	viewSize.setNodeValue(node, tlp.Size(mySizeNode.getNodeValue(node)*2,2,0))
	updateVisualization(True)
	sleep(0.5)
	posX = x
	for son in graph.getOutNodes(node):
		drawNode(viewSize, viewShape, viewLayout, mySizeNode, son, posX, y-2)
		posX += mySizeNode.getNodeValue(son)*2

def setMySizeNode(node,mySizeNode):
#		Fonction récursive permettant de compter l'ordre de l'arbre de racine <node>
#		et enregistre cette valeur via la propriété mySizeNode.
	n = 0
	for son in graph.getOutNodes(node):
		n += setMySizeNode(son, mySizeNode)
	n = max(1,n)
	mySizeNode.setNodeValue(node,n)
	return n
/net/html/perso/melancon/Visual_Analytics_Course/data/pages/vincent_bocquel/solution_homework1.txt · Last modified: 2014/10/22 14:02 by bocquel