npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-colored-maps

v1.1.0

Published

Tool to color svg maps based on data

Downloads

92

Readme

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.

| Parameter | Description | | --- | --- | | construct | Initiate a builderInstance with the necessary parameters. | | render | Render an svg map on the configured container element | | colorizeRdYlGn | Color pathElements with a Red-Yellow-Green pallete based on a 10-quantil, or decil. | | colorizeCategories | Color pathElements with a 10 colors pallete proper for categorical data types, non ordinal. | | togglePath | Add 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.

| Parameter | Type | Description | | --- | --- | --- | | builderInstance | BuilderInstance | States of the builder. | | configParams | ConfigParams | Base configurations. |

mapBuildingTools.colorizeRdYlGn

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

| Parameter | Type | Description | | --- | --- | --- | | pathElementsMap | HTMLElement[code: string] | Object with key-values of all path elements of the map. | | codesAndValues | Array<{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.

| Parameter | Type | Description | | --- | --- | --- | | pathElementsMap | HTMLElement[code: string] | Object with key-values of all path elements of the map. | | codesAndValues | Array<{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.

| Parameter | Type | Description | | --- | --- | --- | | builderInstance | BuilderInstance | States of the builder. | | code | string | Code of the selected path. |

mapBuildingTools Types

BuilderInstance

| Property | Type | Description | | --- | --- | --- | | rendered | boolean | Set to true if the map renders successfully. | | containerSelector | string | CSS selector where the svg element will be appended. | | selectedPathClass | string | CSS class to style a path element with a selected state. | | onPathClick | function(details: {code: string, pathElement: HTMLElement}) | Event triggered on click. The code is the key of the element. | | pathElementsMap | HTMLElement[code: string] | Object with key-values of all path elements of the map. | | currentData | Array<{code: string, value: any}> | Array of codes and values that will base the colors of the map. | | selectedCodes | Array | List of codes with selected state. | | codeAttribute | string | HTML element attribute that represents the code. Ex: 'id', 'citycode' | | svgResolver | string | 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

| Property | Type | Description | | --- | --- | --- | | containerSelector | string | CSS selector where the svg element will be appended. | | selectedPathClass | string | CSS class to style a path element with a selected state. | | onPathClick | function(details: {code: string, pathElement: HTMLElement}) | Event triggered on click. The code is the key of the element. |