Graphics Reference
In-Depth Information
function
sortCountries(countries) {
var
i;
var
row;
// for each country...
for
(i=0; i<countries.length; i++) {
row = countries[i];
// store clockwise direction from middle of
atlantic (40,-40)
row.ClockwiseDirection = Math.PI*0.5 -
Math.atan2(Number(row.Latitude)-40,
Number(row.Longitude)
+40);
// make sure angles are positive
if
(row.ClockwiseDirection < 0) {
row.ClockwiseDirection += Math.PI*2.0;
}
}
// finally sort countries by clockwise direction
from middle of ocean
countries.sort(
function
(a,b) {
return
a.ClockwiseDirection - b.ClockwiseDirection;
});
return
countries;
}
Now that you have functions to prepare the list of countries, it's time to
add a function to process the import records and put them in the right
form.
d3.js
requires that chord data be supplied in matrix form—a
stripped-down array of arrays of numbers. The matrix cannot include
anything but numbers, and no numbers can be missing. To ensure that the
matrix will be valid, write a function to create the matrix and fill it first with
zeros:
function
initMatrix(size) {
var
i,j;