dotted‑world‑map

How do I highlight some countries on a dotted map?

Pass countryColors to getSVG(), such as { FRA: '#e8590c' }: the dots of each country you list take its colour, and every other dot keeps the map's color. For a map inline in a page, CSS works too, because each country's dots are in a group of their own: [data-country='FRA'] { color: #e8590c; }.

npm install @setkernel/dotted-map-next

With countryColors

The keys are the countries' ISO 3166-1 alpha-3 codes, and the values are CSS colours. The colours are written into the SVG, so they stay when the SVG is a file, an <img>, an email or a social image, where page CSS cannot reach it.

country-colors.mjs
import { DottedMap } from '@setkernel/dotted-map-next';

const map = new DottedMap({ height: 40 });
const svg = map.getSVG({
  color: '#8c959f',
  countryColors: { FRA: '#e8590c', DEU: '#e8590c', JPN: '#0969da' },
  title: 'France and Germany in orange, Japan in blue',
});
France and Germany in orange, Japan in blue
Drawn by country-colors.mjs when this page was built.

With CSS

Each country's dots are in a <g data-country="BRA">, and with the default color, currentColor, each dot takes the colour of its group. So CSS can colour a map that is inline in a page, follow the page's theme, and change on hover, with no new SVG.

css-map.mjs
import { DottedMap } from '@setkernel/dotted-map-next';

const map = new DottedMap({ height: 40 });
const svg = map.getSVG({
  className: 'markets',
  title: 'Brazil, India and Kenya highlighted',
});
markets.css
.markets {
  color: #8c959f;
}

.markets [data-country='BRA'],
.markets [data-country='IND'],
.markets [data-country='KEN'] {
  color: #e8590c;
}
Brazil, India and Kenya highlighted
Drawn by css-map.mjs, with the CSS above, when this page was built.

A color passed to getSVG() is the dots' fill: set fill in place of color in these rules to override it. To know which country is under the pointer, one listener on the map reads event.target.closest('[data-country]')?.dataset.country.