Pie chart that will show the different billionaires in Eastern Asia, looking for improvement

I am trying to improve this code and make it a more efficient or more effective program. I want to display the pie chart and develop a way to show the different countries based on how many billionaires they have and hopefully the number along with it. If anyone could lend me some insight or help out all is appreciated. Thanks!

Here is the dataset that I used... it is in the format of .csv and I included it in the folder of my data which is in p5.js https://drive.google.com/open?id=0B4D3N0Bg20n5SWV5bXFsQlFiWVk

//global variable of the billionaires csv
var countryTable;

var citizenshipData=[];

//array that the values are being pushed into
var countries = [];


function preload(){
  countryTable= loadTable("billionaires.csv","csv","header");
}

function setup() {
  createCanvas(1000,1000);
  background(125,125,125);

  //gets the data of the citizenship of different countries
  citizenshipData=countryTable.getColumn("citizenship");
  //println(citizenshipData);

  //only getting the billionaires that have a citizenship in the region of eastAsia
  var eastAsia = citizenshipData.slice(1,536);

  //Layout for what the lines of code mean

  var chineseBillionaires= eastAsia.slice(0,153);//number of billionaires in country sliced from the dataset
  var china=(chineseBillionaires.length);  //takes the number by looking at the length
  countries.push(china);  //adds it to the array of countries


  var japaneseBillionaires=eastAsia.slice(261,357);
  var japan=(japaneseBillionaires.length);
  countries.push(japan);

  var hongkongBillionaires=eastAsia.slice(153,230);
  var hongkong=(hongkongBillionaires.length);
  countries.push(hongkong);

  var taiwaneseBillionaires=eastAsia.slice(471,511);
  var taiwan=(taiwaneseBillionaires.length);
  countries.push(taiwan);

  var southKoreanBillionaires= eastAsia.slice(435,471);
  var southKorea=(southKoreanBillionaires.length);
  countries.push(southKorea);

  var indonesianBillionaires=eastAsia.slice(230,261);
  var indonesia=(indonesianBillionaires.length);
  countries.push(indonesia);

  var malaysianBillionaires= eastAsia.slice(359,387);
  var malaysia=(malaysianBillionaires.length);
  countries.push(malaysia);

  var singaporeanBillionaires= eastAsia.slice(409,435);
  var singapore= (singaporeanBillionaires.length);
  countries.push(singapore);

  var thaiBillionaires=eastAsia.slice(511,534);
  var thailand= (thaiBillionaires.length);
  countries.push(thailand);

  var filipinoBillionaires= eastAsia.slice(387,409);
  var phillipines=(filipinoBillionaires.length);
  countries.push(phillipines);

  var macaneseBillionaires= eastAsia.slice(357,359);
  var macau=(macaneseBillionaires.length);
  countries.push(macau);

  var vietnameseBillionaires=eastAsia.slice(534,535);
  var vietnam=(vietnameseBillionaires.length);
  countries.push(vietnam);
  println(countries);

}


function draw() {
  pieChart(650,countries);
  noLoop();
  textSize(32);
  fill(0);
  text("Multi-decade database of the Billionaires",width/5,height/7);
  text("in East Asia by the numbers",width/5,height/5.5);
}


function pieChart(diameter,data){
  var lastCountry=0;
  strokeWeight(6);
  for(var i=0; i < data.length; i++){
    fill(random(0,255),random(0,255),random(0,255));
    arc(width/2, height/1.7, diameter, diameter, lastCountry, lastCountry+radians(countries[i]));
    lastCountry += radians(countries[i]);
  }

Answers

  • Please edit your post, select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

  • Sorry about that, thank you for the format help.

  • You want ideas for improvement? Show the flags of the different countries followed by the number of billionaires from the country.

  • I will add that, thanks for the Idea.

  • @wrh013 -- one suggestion is to simplify all your setup code for readability. Each of these triple-line groups:

    var chineseBillionaires= eastAsia.slice(0,153);//number of billionaires in country sliced from the dataset
    var china=(chineseBillionaires.length);  //takes the number by looking at the length
    countries.push(china);  //adds it to the array of countries
    

    ...can be simplified to either two lines each:

    var chineseBillionaires= eastAsia.slice(0,153);
    countries.push(chineseBillionaires.length);
    

    ...or perhaps one line each with a comment (untested):

    // chinese billionares:
    countries.push(eastAsia.slice(0,153).length);
    
Sign In or Register to comment.