MapChart.js 2.7 KB
import React, { memo } from "react";
import {
  ComposableMap,
  Geographies,
  Geography
} from "react-simple-maps";
import { useTranslation, Trans } from 'react-i18next';

const geoUrl =
// "http://arreport.test/world-110m.json";
  "https://yearinreview.cbvinstitute.com/world-110m.json";


const MapChart = ({ setTooltipContent, countries }) => {

  const { t } = useTranslation();

  return (
    <>
      <ComposableMap data-tip="" projection="geoMercator" >
          <Geographies geography={geoUrl}>
            {({ geographies }) =>
              geographies.map(geo => { 
                var found = false;
                for(var i = 0;i < countries.length;++i) {
                  for(const cprop in geo.properties) {
                    if(typeof geo.properties[cprop].indexOf == 'function' && geo.properties[cprop].indexOf(countries[i].name) !== -1) {
                        geo.properties.NICENAME = countries[i].name 
                        found = true;
                        break;
                    }
                  }
                }

                var style = {};

                if(found) {
                    style = {
                        default: {
                          fill: "#5CBFBC",
                          outline: "#0F1647",
                          stroke: "#0F1647",
                          strokeWidth: "0.25px"
                        },
                        hover: {
                          fill: "white",
                          outline: "none"
                        }
                      }
                } else {
                    style = {
                        default: {
                          fill: "#594D78",
                          outline: "none",
                        },
                        hover: {
                          fill: "#594D78",
                          outline: "none"
                        },
                        pressed: {
                            fill: "#594D78",
                            outline: "none"
                        }
                      }
                }
                
                return (
                <Geography
                  key={geo.rsmKey}
                  geography={geo}
                  onMouseEnter={() => {
                      if(found) {
                        const { NICENAME } = geo.properties;
                        setTooltipContent(`${NICENAME}`);
                      }
                  }}
                  onMouseLeave={() => {
                    setTooltipContent("");
                  }}
                  style={style}
                />
              ) } )
            }
          </Geographies>
      </ComposableMap>
    </>
  );
};

export default memo(MapChart);