Learn Gapminder

Jump to: navigation, search


Gapminder visualizations bring your data to life. Enhance your analysis within OpenEI by creating Gapminder visualizations that can easily be displayed and shared from any web browser.

  • View other Gapminder visualizations: Gapminder
  • Visit Gapminder to see some example datasets and visualizations.

Formatting your data

To get your visualization to work, you must be sure your data is formatted correctly:

Here is a great Quick Guide to getting your Motion Chart, or Gapminder, to work properly.

Java Coding your Visualization

This java program will create the body of a Gapminder visualization code for OpenEI. For example, http://en.openei.org/w/index.php?title=Widget:Motion_Chart_Visualizations/generalunitedstates.js. Just copy-paste the code into a program such as Eclipse and hit run once the variables are changed accordingly (more instructions under “Notes:”. Questions - wsz6@cornell.edu

Java Code:

import java.io.*; import java.util.StringTokenizer;


* Replace the $$ at the end with a comma - can just do a search replace in notepad, and delete last instance of $$
* replace the first 6 global variables - although outputFileName is optional
* This follows the format of tables in the SED data table, ordered by state alphabetically
* Will only do the body of the code - add the beginning and end parts yourself

public class Gapminder {

   public static int columns = 4; //number of parameters excluding object and date
   public static int years = 38;
   public static int initialYear= 1971;
   public static int objects= 43;
   public static String inputFileName = "C:\\Users\\wzeng\\Documents\\gapminder3redo.csv"; //follow this format
   public static String outputFileName = "C:\\Users\\wzeng\\Documents\\gapminderResult.csv";
   public static String stateList[] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
  	 "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho",
  	 "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
  	 "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
  	 "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
  	 "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
  	 "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};
   public static String stateAbb[] = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL",
  	 "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM",
  	 "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"
   public static int resultCol = columns+2;
   public static int resultRow = objects*years; //number of objects times number of years
   public static int inputRow = objects*columns+1;
   public static int inputCol = years+2;
   public static Object input[][] = new Object[inputRow][inputCol];
   public static Object result[][] = new Object[resultRow][resultCol];
   public static int counter= 1;
   public static int counter2 = 0;
   public static void createmyArray() {
  	 for(int i=0; i!=inputRow; i++) {
  		 for(int j=0; j!=stateList.length; j++) {
  			 if (input[i][0].equals(stateAbb[j])) input[i][0]=stateList[j];
  	 for(int i=0; i!=years; i++) { //for each year
  		 for(int k=0; k!=objects; k++) {
  			 result[counter2][0] = "      	['" + input[(k*columns+1)][0] + "'";
  			 result[counter2][1] = initialYear+i;
  			 for(int j=0; j!=columns; j++) { //for each state datapoint
  				 result[counter2][j+2] = input[counter][i+2];
  				 counter= counter+1;
  			 counter2 = counter2 + 1;
  		 counter = 1;
   public static void main(String[] args) throws IOException {
  	 int col=0;
  	 int row=0;
  	 BufferedReader bufRdr;
  	 bufRdr = new BufferedReader(new FileReader(inputFileName));
  	 String line = null;
  	 //read each line of text file
  	 while((line = bufRdr.readLine()) != null) {
  		 StringTokenizer st = new StringTokenizer(line,",");
  		 while (st.hasMoreTokens()) {
  			 //get next token and store it in the array
  			 input[row][col] = st.nextToken();
  	 //close the file
   	FileWriter fw = new FileWriter(outputFileName);
   	PrintWriter pw = new PrintWriter(fw);
   	//Write to file for the first row
   	for(int j=0; j!=resultRow; j++) {
  		 for(int i=0; i!=resultCol-1; i++) {
   	//Flush the output to the file
   	//Close the Print Writer
   	//Close the File Writer