Kent Wong
Kent Wong

Reputation: 591

Change Javascript Object Array data

How can I input my own data or hardcode the data instead of using the forEach() function in this example?

For example, I have my own data and it's just Hawaii, Alaska, and Montana with the same temperatures (High: 60, Mid: 45, Low: 40).

Ideally, I'm going to input a simple excel/csv file of state data.

Here is the forEach() from the source:

var sampleData ={}; /* Sample random data. */   
["HI", "AK", "FL", "SC", "GA", "AL", "NC", "TN", "RI", "CT", "MA",
"ME", "NH", "VT", "NY", "NJ", "PA", "DE", "MD", "WV", "KY", "OH", 
"MI", "WY", "MT", "ID", "WA", "DC", "TX", "CA", "AZ", "NV", "UT", 
"CO", "NM", "OR", "ND", "SD", "NE", "IA", "MS", "IN", "IL", "MN", 
"WI", "MO", "AR", "OK", "KS", "LS", "VA"]
    .forEach(function(d){ 
        var low=Math.round(100*Math.random()), 
            mid=Math.round(100*Math.random()), 
            high=Math.round(100*Math.random());
        sampleData[d]={low:d3.min([low,mid,high]), high:d3.max([low,mid,high]), 
                avg:Math.round((low+mid+high)/3), color:d3.interpolate("#ffffcc", "#800026")(low/100)}; 
    });

Upvotes: 1

Views: 60

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102219

This is the structure you need if you want just those three states and no forEach():

var sampleData = {
  HI: {
    low: 40,
    avg: 45,
    high: 60
  },
  AL: {
    low: 40,
    avg: 45,
    high: 60
  },
  MT: {
    low: 40,
    avg: 45,
    high: 60
  },
}

Also, there is a color property (that I'm not showing here) and a fill attribute, for which I wrote a ternary (since you have just three states in your data).

Here is the updated code: http://bl.ocks.org/anonymous/60b445b86494aac36bf04283e674190e/a1e9043087aa27312f17fdf9575b2f0654330c22

Upvotes: 1

Related Questions