The VisualScript Cookbook

9/18 Page

Making a Decision Tree with VisualScript

You can change the type of diagram that ShapeConnectors will build by using a "ShapeConnectorType" property on the parent shape of the ShapeConnector. The following values are supported:

  • "Hierarchy" - A normal tree
  • "Decisiontree" - Decision tree
  • "Mindmap" - A tree with left and right branches
  • "Flowchart" - A flowchart

You can make a decision tree by adding a ShapeConnector to the root object of the document and set the ShapeConnectorType method to DecisionTree.

{
  var myDocument=new VS.Document();
  var rootShape=myDocument.GetTheShape();
  var myConnector=rootShape.AddShapeConnector(VS.ShapeConnectorTypes.DecisionTree);
  myConnector.AddShape();
  myConnector.AddShape();
}
VisualScript simple decision tree

More than two children added to the decision tree will create a multiple-way split:

{
  var myDocument=new VS.Document();
  var rootShape=myDocument.GetTheShape();
  var myConnector=rootShape.AddShapeConnector(VS.ShapeConnectorTypes.DecisionTree);
  myConnector.AddShape();
  myConnector.AddShape();
  myConnector.AddShape();
}
VisualScript decision tree with labels

By default, decision trees will grow from left to right. This is the default "Direction"="Right" setting. You can change this to any of the three other directions "Up","Down","Left".

{
  var myDocument=new VS.Document();
  var rootShape=myDocument.GetTheShape();
  var myConnector=rootShape.AddShapeConnector(VS.ShapeConnectorTypes.DecisionTree);
  myConnector.SetDirection(VS.Directions.Down);
  myConnector.AddShape();
  myConnector.AddShape();
  myConnector.AddShape();
}
VisualScript downward decision tree

Adding ShapeConnectors to the child shapes recursively creates a deeper tree:

{
  var myDocument=new VS.Document();
  var rootShape=myDocument.GetTheShape();
  var myConnector=rootShape.AddShapeConnector(VS.ShapeConnectorTypes.DecisionTree);
  myConnector.SetDirection(VS.Directions.Down);
  var myConnectorShape1=myConnector.AddShape();
  var myConnectorShape2=myConnector.AddShape();
  var myConnectorShape3=myConnector.AddShape();
  var myChildConnector1=myConnectorShape1.AddShapeConnector();
  myChildConnector1.AddShape();
  myChildConnector1.AddShape();
  var myChildConnector2=myConnectorShape2.AddShapeConnector();
  myChildConnector2.AddShape();
  myChildConnector2.AddShape();
  var myChildConnector3=myConnectorShape3.AddShapeConnector();
  myChildConnector3.AddShape();
  myChildConnector3.AddShape();
}
VisualScript decision tree children

Like other trees, you can use "Collapse" as a property of a ShapeConnector to collapse a branch.

Changing the Line Labels

By default, the first two shapes in a Decision Tree ShapeConnector have the labels "Yes" and "No" on the lines that connect them to their parent. Additonal shapes have the label "Label".

You can control the text that appears on the line going into a shape by using the LineLabel property of the Shape.

{
  var myDocument=new VS.Document();
  var rootShape=myDocument.GetTheShape();
  var myConnector=rootShape.AddShapeConnector(VS.ShapeConnectorTypes.DecisionTree);
  var myChildShape1=myConnector.AddShape();
  myChildShape1.SetLineLabel("<25");
  var myChildShape2=myConnector.AddShape();
  myChildShape1.SetLineLabel("25-75");
  var myChildShape3=myConnector.AddShape();
  myChildShape1.SetLineLabel(">75");
}
VisualScript decision tree collapsed

Using More than One ShapeConnector in a Decision Tree

Like regular trees, you should only use one ShapeConnector per shape in a Decision Tree.

9/18 Page