From 68f8fcb5f19e6b3c2cd4b81897c7e71dc530c7b3 Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Mon, 6 Dec 2021 08:25:37 -0800 Subject: [PATCH] support multiple short_code formats (#6782) * support multiple short_code formats * automatically determine shortcode key --- .../on-demand/simple-chart/simple-chart.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cfgov/unprocessed/js/routes/on-demand/simple-chart/simple-chart.js b/cfgov/unprocessed/js/routes/on-demand/simple-chart/simple-chart.js index a3ba9a90d0f..d4489319d5e 100644 --- a/cfgov/unprocessed/js/routes/on-demand/simple-chart/simple-chart.js +++ b/cfgov/unprocessed/js/routes/on-demand/simple-chart/simple-chart.js @@ -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 @@ -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 }; } ); @@ -358,6 +373,7 @@ function makeChartOptions( data, target ) { } const formattedSeries = formatSeries( data ); + if ( chartType === 'tilemap' && formattedSeries.length === 1 ) { defaultObj = { ...defaultObj,