The VisualScript Cookbook

9/18 Page

Making a Decision Tree with VisualScript

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();
  var vsJSON = myDocument.toJSON(); //turn the VisualScript object constructed using the API into a JSON string
  vsCompleteCallback(vsJSON); //pass the JSON string into the callback to trigger the creation of a document.
}
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();
  var vsJSON = myDocument.toJSON(); //turn the VisualScript object constructed using the API into a JSON string
  vsCompleteCallback(vsJSON); //pass the JSON string into the callback to trigger the creation of a document.
}
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();
  var vsJSON = myDocument.toJSON(); //turn the VisualScript object constructed using the API into a JSON string
  vsCompleteCallback(vsJSON); //pass the JSON string into the callback to trigger the creation of a document.
}
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();
  var vsJSON = myDocument.toJSON(); //turn the VisualScript object constructed using the API into a JSON string
  vsCompleteCallback(vsJSON); //pass the JSON string into the callback to trigger the creation of a document.
}
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");
  var vsJSON = myDocument.toJSON(); //turn the VisualScript object constructed using the API into a JSON string
  vsCompleteCallback(vsJSON); //pass the JSON string into the callback to trigger the creation of a document.
}
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