Visualising data - Griffith-ICT/1701ICT-Creative-Coding GitHub Wiki

In the previous topic we counted the number of extinct species in Qld. How can we visualise the number of extinct species in each state?

Firstly we will need to count the number of extinct species in each state.

There is a compact way of doing this by creating an array which contains all of the state names:

var states = ["ACT", "NSW", "NT", "QLD", "SA", "TAS", "VIC", "WA"];

We are going to have a separate array which counts the number of extinct species per state, which will be initialised to zero for each state:

var extinct = [0, 0, 0, 0, 0, 0, 0, 0];

We are going to use nested for loops. The outer loop will go through the states array, checking each state. The inner loop will go through the rows in the table, counting species which are extinct from that state.

for (var s = 0; s < states.length; s++) {
  for (var i = 0; i < table.getRowCount(); i++) {
    if (table.get(i, states[s]) == "Yes" && table.get(i, "Threatened status") == "Extinct") {
      extinct[s]++;
    }
  }
}

We can print out the extinct array to see how many species are extinct for each state:

for (var s = 0; s < states.length; s++) {
  print(states[s] + ": " + extinct[s]);
}

Which produces:

ACT: 0
NSW: 32
NT: 11
QLD: 26
SA: 20
TAS: 10
VIC: 12
WA: 26