themustafaomar / jsvectormap

A lightweight JavaScript library for creating interactive maps and pretty data visualization.
https://jvm-docs.vercel.app
MIT License
398 stars 80 forks source link

Support for add/remove Regions individually? #131

Closed freezingjune closed 1 year ago

freezingjune commented 1 year ago

Can you please add support for adding and removing regions individually after the map is initialized?

Currently, the only option is to call clearSelectedRegions() and then add all needed regions again, which causes flickering in the map and scrolls the page as well.

themustafaomar commented 1 year ago

Thanks for the suggestion, I'll be considering this on the next release as soon as possible.

freezingjune commented 1 year ago

What's the expected timeline for next release?

themustafaomar commented 1 year ago

Here is a quick workaround https://codepen.io/themustafaomar/pen/oNQLXQY

freezingjune commented 1 year ago

Thanks that works! Can you also add a function to add Region (addRegion)?

freezingjune commented 1 year ago

For anyone looking to add this feature: Use the following code, it'll add two functions addSelectedRegion and removeSelectedRegion.

map.extend("removeSelectedRegion", function (code) {
      for (const key in this.regions) {
        if (this.regions[key].element.isSelected) {
          if (key === code) {
            this.regions[key]
              .element.select(false)
          }
        }
      }
    })

map.extend("addSelectedRegion", function (code) {
      for (const key in this.regions) {        
          if (key === code) {
            this.regions[key]
              .element.select(true)
          }        
      }
    })