Skip to content

Commit

Permalink
support multiple short_code formats (#6782)
Browse files Browse the repository at this point in the history
* support multiple short_code formats

* automatically determine shortcode key
  • Loading branch information
wpears authored Dec 6, 2021
1 parent 5601225 commit 68f8fcb
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cfgov/unprocessed/js/routes/on-demand/simple-chart/simple-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ function updateTilemapLegend( node, data, legendTitle ) {
labels.forEach( v => legend.appendChild( v ) );
}

/**
* Intuits the correct object key for state short codes
* @param {object} data A row of data as an object with headers as keys
* @returns {string} The intuited shortcode
* */
function getShortCode( data ) {
const keys = Object.keys( data );
for ( let i = 0; i < keys.length; i++ ) {
if ( usLayout[data[keys[i]]] ) return keys[i];
}
/* eslint-disable-next-line */
console.error( 'Unable to determine state shortcode. Data is misformatted for simple-chart.' );
}


/**
* Adds generates a config object to be added to the chart config
Expand All @@ -305,14 +319,15 @@ function getMapConfig( series, date ) {
let min = Infinity;
let max = -Infinity;
const data = series[0].data;
const shortCode = getShortCode( data[0] );
if ( !date ) date = getTilemapDates( data )[0];
const added = data.map( v => {
const val = Math.round( Number( v[date] ) * 100 ) / 100;
if ( val <= min ) min = val;
if ( val >= max ) max = val;
return {
...usLayout[v.state_ab],
state: v.state_ab,
...usLayout[v[shortCode]],
state: v[shortCode],
value: val
};
} );
Expand Down Expand Up @@ -358,6 +373,7 @@ function makeChartOptions( data, target ) {
}

const formattedSeries = formatSeries( data );

if ( chartType === 'tilemap' && formattedSeries.length === 1 ) {
defaultObj = {
...defaultObj,
Expand Down

0 comments on commit 68f8fcb

Please sign in to comment.