1.0.5 • Published 5 months ago

svg-colored-maps v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

svg-colored-maps

A toolkit to color svg maps based on data.

Install

npm install svg-colored-maps

Examples

Built-in maps

This lib include some built-in maps of Brazil, BrazilMunicipalitiesBuilder, BrazilStatesBuilder, SaoPauloMunicipalitiesBuilder, cosuming the main toolkit 'mapBuildingTools'.

<div class="map-container">
  <div id="municipalities-map"></div>
</div>
import { BrazilMunicipalitiesBuilder } from 'svg-colored-maps'

const buildParameters = {
  containerSelector: '#municipalities-map', 
  selectedPathClass: 'path--selected',
  onPathClick: (details) => {
    console.log('custom click details:', details)
  }, 
}

const mapBuilder = new BrazilMunicipalitiesBuilder(buildParameters);
mapBuilder
  .render()
  .then(() => colorWithGdp())

const colorWithGdp = async () => {
  // Fetch data and map to code and value.
  // In this built-in map the code is the IBGE city code.
  const sampleData = (await fetch('./sample-data/gdp-per-capita-2019.json')
    .then(res => res.json()))
    .map(d => ({
      code: d.code,
      value: d.gdpPerCapitaBrl2019
    }))
  // Color svg paths with a Red-Yellow-Green pallete.
  mapBuilder.colorizeRdYlGn(sampleData)
}

mapBuildingTools Functions

The functions support implementing a builder class without restrictions of a base class. For that reason the functions often receive a builderInstance parameter.

ParameterDescription
constructInitiate a builderInstance with the necessary parameters.
renderRender an svg map on the configured container element
colorizeRdYlGnColor pathElements with a Red-Yellow-Green pallete based on a 10-quantil, or decil.
colorizeCategoriesColor pathElements with a 10 colors pallete proper for categorical data types, non ordinal.
togglePathAdd the selected class or remove it if already selected.
clearAllSelectedPaths(builderInstance)Remove the selected class from all pathElements.
clearSelectedPaths(builderInstance, codes)Remove the selected class from a list of pathElements.
selectPaths(builderInstance, codes)Add the selected class to a list of pathElements.

mapBuildingTools.construct

Initialize an object with the states of the builder.

ParameterTypeDescription
builderInstanceBuilderInstanceStates of the builder.
configParamsConfigParamsBase configurations.

mapBuildingTools.colorizeRdYlGn

Colorize pathElements with a Red-Yellow-Green pallete based on a 10-quantil, or decil.

ParameterTypeDescription
pathElementsMapHTMLElementcode: stringObject with key-values of all path elements of the map.
codesAndValuesArray<{code: string, value: any}>Values that will base colors.

mapBuildingTools.colorizeCategories

Color pathElements with a 10 colors pallete proper for categorical data types, non ordinal.

ParameterTypeDescription
pathElementsMapHTMLElementcode: stringObject with key-values of all path elements of the map.
codesAndValuesArray<{code: string, value: any}>Values that will base colors.
optionalConfig{ customPallete: string[] }Accepts a custom list of colors.

mapBuildingTools.togglePath

Add the selected class or remove it if already selected.

ParameterTypeDescription
builderInstanceBuilderInstanceStates of the builder.
codestringCode of the selected path.

mapBuildingTools Types

BuilderInstance

PropertyTypeDescription
renderedbooleanSet to true if the map renders successfully.
containerSelectorstringCSS selector where the svg element will be appended.
selectedPathClassstringCSS class to style a path element with a selected state.
onPathClickfunction(details: {code: string, pathElement: HTMLElement})Event triggered on click. The code is the key of the element.
pathElementsMapHTMLElementcode: stringObject with key-values of all path elements of the map.
currentDataArray<{code: string, value: any}>Array of codes and values that will base the colors of the map.
selectedCodesArrayList of codes with selected state.
codeAttributestringHTML element attribute that represents the code. Ex: 'id', 'citycode'
svgResolverstring | Promise | function(): Promise | string>String type is read as an URL, Promises will be resolved as innerHTML, same for functions returning Promises and strings.

ConfigParams

PropertyTypeDescription
containerSelectorstringCSS selector where the svg element will be appended.
selectedPathClassstringCSS class to style a path element with a selected state.
onPathClickfunction(details: {code: string, pathElement: HTMLElement})Event triggered on click. The code is the key of the element.
1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago