-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "embed this chart" modal to map tiles #4782
base: master
Are you sure you want to change the base?
Changes from all commits
e02449e
7481b6c
e275585
3265c3f
9fddcfc
5d81758
7c2046e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ import { getPointWithin, getSeriesWithin } from "../../utils/data_fetch_utils"; | |
import { getDateRange } from "../../utils/string_utils"; | ||
import { | ||
clearContainer, | ||
getChartTitle, | ||
getDenomInfo, | ||
getNoDataErrorMsg, | ||
getStatFormat, | ||
|
@@ -353,6 +354,7 @@ export function MapTile(props: MapTilePropType): JSX.Element { | |
? [props.dataSpecs[0].variable] | ||
: [props.statVarSpec] | ||
} | ||
sourceCode={getWebComponentSourceCode(mapChartData)} | ||
> | ||
{showZoomButtons && !mapChartData.errorMsg && ( | ||
<div className="map-zoom-button-section"> | ||
|
@@ -749,3 +751,94 @@ function getExploreLink(props: MapTilePropType): { | |
url: `${props.apiRoot || ""}${URL_PATH}#${hash}`, | ||
}; | ||
} | ||
|
||
/** | ||
* Get the HTML that can be used to embed this tile as a web component | ||
* @param props this tile's props | ||
* @returns HTML used to embed this tile as a web component | ||
*/ | ||
export function getWebComponentSourceCode(mapChartData: MapChartData): string { | ||
if (!mapChartData) { | ||
return ""; | ||
} | ||
|
||
const props = mapChartData.props; | ||
|
||
// Get all places and types in the chart | ||
const places = mapChartData.layerData | ||
.map((layer) => layer.place.dcid) | ||
.join(" "); | ||
const enclosedPlaceTypes = mapChartData.layerData | ||
.map((layer) => layer.enclosedPlaceType) | ||
.join(" "); | ||
|
||
// Get unique variables | ||
const variables = _.uniq( | ||
mapChartData.layerData.map((layer) => { | ||
return layer.variable.statVar; | ||
}) | ||
).join(" "); | ||
|
||
// Get list of variables that are per capita | ||
const perCapitaVariables = mapChartData.layerData | ||
.map((layer) => { | ||
return layer.variable; | ||
}) | ||
.filter((variable) => { | ||
return variable?.denom == "Count_Person"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use DEFAULT_PER_CAPITA_DENOM here |
||
}) | ||
.map((variable) => { | ||
return variable?.statVar; | ||
}) | ||
.join(" "); | ||
|
||
// Check if a specific date is being used for all variables | ||
let date = ""; | ||
if ( | ||
mapChartData?.layerData.length > 0 && | ||
mapChartData.layerData.every((layer) => layer.variable.date) | ||
) { | ||
date = mapChartData.layerData[0]?.variable?.date || ""; | ||
} | ||
|
||
// Generate title with replacement strings filled in | ||
const replacementStrings = | ||
mapChartData && getReplacementStrings(props, mapChartData); | ||
const header = getChartTitle(props.title, replacementStrings); | ||
|
||
let sourceCode = `<script src="https://datacommons.org/datacommons.js"></script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the apiRoot value here for the hostname if set. This way the functionality will work on https://unstats.un.org/UNSDWebsite/undatacommons/sdgs |
||
<datacommons-map | ||
\theader="${header}" | ||
\tchildPlaceTypes="${enclosedPlaceTypes}" | ||
\tparentPlaces="${places}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use childPlaceType & parentPlace (singular) if there's only a single parent place? Right now i dont think we actually use multiple parent places anywhere, and using multiple places isn't documented in the web component map api |
||
\tvariables="${variables}"`; | ||
if (date) { | ||
sourceCode += `\n\tdate="${date}"`; | ||
} | ||
if (props.allowZoom) { | ||
sourceCode += "\n\tallowZoom"; | ||
} | ||
if (props.apiRoot) { | ||
sourceCode += `\n\tapiRoot="${props.apiRoot}"`; | ||
} | ||
if (props.colors) { | ||
sourceCode += `\n\tcolors="${props.colors.join(" ")}"`; | ||
} | ||
if (props.footnote) { | ||
sourceCode += `\n\tfootnote="${props.footnote}"`; | ||
} | ||
if (props.geoJsonProp) { | ||
sourceCode += `\n\tgeoJsonProp="${props.geoJsonProp}"`; | ||
} | ||
if (perCapitaVariables) { | ||
sourceCode += `\n\tperCapita="${perCapitaVariables}"`; | ||
} | ||
if (props.placeNameProp) { | ||
sourceCode += `\n\tplaceNameProp="${props.placeNameProp}"`; | ||
} | ||
if (props.sources) { | ||
sourceCode += `\n\tplaceNameProp="${props.sources.join(" ")}"`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
} | ||
sourceCode += `\n></datacommons-map>`; | ||
return sourceCode; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use css variable here - might need to verify which one, but maybe
font-family: var(--dc-font-family, base.$font-family-sans-serif);