Why specify the number of rows at all? You can just add as many rows of
data as you have, one at a time (or put your data into an array of arrays
and add it all at once). This makes your code more robust in that it can
handle arbitrarily sized data sets without over-allocating (or
under-allocating) resources in the DataTable:
/* This function draws all the maps for the States
*
* parameters
* dataArray = a 2d Array with the data for each state or county
*/
function drawMaps(dataArray, counties)
{
google.visualization.GeoChart.setMapsSource('http://chrisdblumberg.com/maps');
mapCount = 0;
while( mapCount < 4 )
{
var data = new google.visualization.DataTable();
var totalUsNumber = 0;
if( !counties )
{
data.addColumn('string', 'State');
}else
{
data.addColumn('string', 'County');
}
if( mapCount == 0 )
data.addColumn('number', 'Change in workforce');
else if( mapCount == 1 )
data.addColumn('number', 'Jobs created');
else if( mapCount == 2 )
data.addColumn('number', 'Change in # of people without a job');
else if( mapCount == 3 )
data.addColumn('number', 'Change in unemployment rate');
data.addColumn('string', 'Display');
minValue = 0;
maxValue = 0;
for( entityNumber = 0; entityNumber < dataArray.length;
entityNumber++ )
{
stateLatestData = dataArray[entityNumber].length - 1;
if( mapCount == 0)
{
lastValue = Number(
dataArray[entityNumber][stateLatestData].total_workers );
firstValue = Number(
dataArray[entityNumber][0].total_workers );
}else if( mapCount == 1 )
{
lastValue = Number(
dataArray[entityNumber][stateLatestData].total_employed );
firstValue = Number(
dataArray[entityNumber][0].total_employed );
}else if( mapCount == 2 )
{
lastValue = Number(
dataArray[entityNumber][stateLatestData].total_unemployed );
firstValue = Number(
dataArray[entityNumber][0].total_unemployed );
}else if( mapCount == 3 )
{
lastValue = Number(
dataArray[entityNumber][stateLatestData].unemployment_per );
firstValue = Number(
dataArray[entityNumber][0].unemployment_per );
}
finalValue = lastValue - firstValue;
if( mapCount == 3 )
finalValue = Math.round((lastValue - firstValue)*100)/100;
totalUsNumber += finalValue; //this gets the total change
across the nation
data.addRow([dataArray[entityNumber][0].mapping_code,
finalValue, dataArray[entityNumber][0]['name']]);
minOrMax(finalValue);
}
if( mapCount == 0 )
{
$('#changeUSWorkforce').html( totalUsNumber );
$('#changeUSWorkforce').parseNumber({format:'#,###',
locale:'us'});
$('#changeUSWorkforce').formatNumber({format:'#,###',
locale:'us'});
var container = document.getElementById('totalWorkers');
setColorsArray( minValue, maxValue );
}else if( mapCount == 1 )
{
$('#changeUSJobs').html( totalUsNumber );
$('#changeUSJobs').parseNumber({format:'#,###', locale:'us'});
$('#changeUSJobs').formatNumber({format:'#,###', locale:'us'});
var container = document.getElementById('totalEmployed');
setColorsArray( minValue, maxValue );
}else if( mapCount == 2 )
{
$('#changeUSNoJobs').html( totalUsNumber );
$('#changeUSNoJobs').parseNumber({format:'#,###', locale:'us'});
$('#changeUSNoJobs').formatNumber({format:'#,###',
locale:'us'});
var container = document.getElementById('totalUnemployed');
setColorsArrayReverse( minValue, maxValue );
}else if( mapCount == 3 )
{
totalUsNumber = totalUsNumber / 50;
$('#changeUSPercent').html( totalUsNumber.toFixed(2) );
$('#changeUSPercent').parseNumber({format:'#,###.00',
locale:'us'});
$('#changeUSPercent').formatNumber({format:'#,###.00',
locale:'us'});
var container =
document.getElementById('unemploymentPercentage');
setColorsArrayReverse( minValue, maxValue );
}
var geoChart = new google.visualization.GeoChart(container);
var formatter = new google.visualization.PatternFormat('{1}');
formatter.format(data, [0, 2]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1]);
google.visualization.events.addListener(geoChart, 'regionClick',
function(r)
{
if( !counties )
{
$('#countiesShown').val('true');
$('#stateCode').val(r.region);
options.region = r.region;
}else
{
$('#countiesShown').val('false');
$('#stateCode').val('');
options.region = 'US';
}
setURL();
});
geoChart.draw(view, options);
mapCount++;
}
}
As an aside, I was checking out your maps, and saw that your counties are
sometimes mislabeled (using my home state of MA as an example, almost every
county has the wrong label, compare with
http://geology.com/state-map/massachusetts.shtml). The county polygons
could also use some work, but that might prove to be a labor- or
compute-expensive task that isn't worth it.
All in all, an impressive result. Bravo!
On Thursday, July 25, 2013 11:17:43 AM UTC-4, Bluestreak2k5 wrote:
>
> Hey asgallant, been a few years.
>
> Thanks that has fixed the race condition:
>
> The issue with the combined method called DrawMaps() was that to have 1
> method draw both states and counties I had to do data.addRows(256); This
> created an error on drawing the states which only needs 50 rows and the
> error would say cannot get length of null.
>
> I do not encounter the same issue with counties, even though some states
> only have a few counties.
>
> I have fixed the issue by just putting the addRows() within a if statement
> that checks if I am drawing counties or states. But I am still open to a
> better suggestion then what I do currently:
>
> data.addRows(50 or 255)
> data.addColumn(State or county mapping code)
> data.addColumn( Hover text for data)
> data.addColumn( Hover Text for State/County Name)
> foreach( stateOrCounty )
> data.setValue()
> data.setValue()
> data.setValue()
>
>
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.