Skip to main content

Percentage of Ball Possession and a Bar to Represent It

Example Used:

In this example we will show how to create a bar chart to display the percentage of the ball possession of each team.

Commands Used

  • $this_button
  • Length Instances
  • Round
  • Button Color
  • If (Statements)
  • @"button_id"


  1. Create two inactive buttons assign the ID to each button: “aposs” and “bposs”.

  2. Give these buttons the following script to calculate each team's possession:

    $home = length instances where row = #'aposs'
    This variable is where we will store the total possession of team A. We use the length command to get a total time value for the row #”aposs”. This row is determined using the button ID of the button with name “A POSS” which ID = aposs.

    $away = length instances where row = #'bposs'
    We follow the same logic as for the $home variable, but this time to get the possession of team B.

    $total = $home + $away
    We use this variable to get the total length for both rows.

    $percent = $away / $total
    In this variable we divide one team’s possession by the total in order to be able to produce the percentage later.

    $percentname = ROUND ($percent*100,0)
    The round command will allow us to get the first number inside the parenthesis, in this case our $percent multiplied by 100 to get the %, with 0 decimals to have a final rounded number.

    show $percentname
    Finally we show the output of the number calculated in the variable $percentname that will be the percentage.

  3. Create two buttons. Assign each a color using the palette of the inspector and give each a button ID: “color1” and “color2”. We will use these buttons later to assign the colors to the bar graph.

  4. Create 50 inactive buttons for the bar graph (each % increment will be a 2% increment). Assign a name to each button from 1 to 99 with a difference of 2 between each: 1, 3, 5, etc...

  5. Give the following script to each of the 50 buttons:

    $a = number ($this_button)
    We use the $THIS_BUTTON command to pull in the button’s name, but we are also telling the button it is a number.

    $b = $a +1
    Here we get the number of the $a variable and add 1 to it.

    $c = @"aposs"
    With the @”aposs” we get the output of the button that has ID = aposs.

    $d = @"bposs"
    This gets the result of the button that has ID = bposs.

    if($b <= $c, button color #"color1", button color #"color2")
    With the IF statement, we check if $b is equal or less than the result of $c. If this is true the button will change to the same color as the button we created at the beginning that has ID = color1. In any other case it will change the color to the same color of the button with ID = color2.

  6. To approve the appearance of the bar, adjust the button stroke width to zero by highlighting all buttons and mass changing to zero.