We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Changing Bubble dataSource on Zoom

Hi,
I'm trying to dynamically change the dataSource of the bubbleSettings from my layer, this should happen when the scale of zoom goes over 3.
Below you can find the code I'm using (which is not working). Could you help me figure our why it is not working?

```
import { Component, OnInit } from '@angular/core';
import { Maps, Bubble, MapsTooltip, IBubbleRenderingEventArgs, Zoom, IMapZoomEventArgs } from '@syncfusion/ej2-angular-maps';
Maps.Inject(Bubble, MapsTooltip, Zoom);

let worldMap: object[] = require('./world_map.json');
@Component({
  selector: 'app-search-location-in-map',
  templateUrl: './search-location-in-map.component.html',
  styleUrls: ['./search-location-in-map.component.css', '../reports.component.css']
})
export class SearchLocationInMapComponent implements OnInit {

internetUsers = [
    { 'rank': 1, 'name': 'China', 'value': this.bubblesize(746662194), 'color': '#7F38A0', 'population': 746662194},
    { 'rank': 2, 'name': 'India', 'value': this.bubblesize(391292635), 'color': '#7F38A0 ', 'population': 391292635 },
    { 'rank': 3, 'name': 'United States', 'value': this.bubblesize(245436423), 'color': '#99295D ','population': 245436423 },
    { 'rank': 4, 'name': 'Brazil', 'value': this.bubblesize(123927230), 'color': '#364A98','population': 123927230 },
    { 'rank': 5, 'name': 'Japan', 'value': this.bubblesize(117528631), 'color': '#7F38A0 ','population': 117528631 },
    { 'rank': 6, 'name': 'Russia', 'value': this.bubblesize(110003284), 'color': '#2E769F','population': 110003284 },
    { 'rank': 7, 'name': 'Mexico', 'value': this.bubblesize(75937568), 'color': '#99295D ','population': 75937568 },
    { 'rank': 8, 'name': 'Germany', 'value': this.bubblesize(73436503), 'color': '#2E769F', 'population': 73436503 },
    { 'rank': 9, 'name': 'Indonesia', 'value': this.bubblesize(66244991), 'color': '#7F38A0 ', 'population': 66244991 },
    { 'rank': 10, 'name': 'United Kingdom', 'value': this.bubblesize(62354410), 'color': '#2E769F', 'population': 62354410 },
    { 'rank': 11, 'name': 'Philippines', 'value': this.bubblesize(57342723), 'color': '#7F38A0 ', 'population': 57342723 },
    { 'rank': 12, 'name': 'France', 'value': this.bubblesize(55413854), 'color': '#2E769F', 'population': 55413854 },
    { 'rank': 13, 'name': 'Nigeria', 'value': this.bubblesize(47743541), 'color': '#816F28', 'population': 47743541 },
    { 'rank': 14, 'name': 'South Africa', 'value': this.bubblesize(47094267), 'color': '#816F28', 'population': 47094267 },
    { 'rank': 15, 'name': 'Turkey', 'value': this.bubblesize(46395500), 'color': '#2E769F', 'population': 46395500 },
];
internetUsers2 = [
    { 'rank': 16, 'name': 'Vietnam', 'value': this.bubblesize(43974618), 'color': '#7F38A0 ', 'population': 43974618 },
    { 'rank': 17, 'name': 'Iran', 'value': this.bubblesize(42731675), 'color': '#7F38A0 ', 'population': 42731675 },
    { 'rank': 18, 'name': 'Egypt', 'value': this.bubblesize(37519531), 'color': '#816F28', 'population': 37519531 },
    { 'rank': 19, 'name': 'Spain', 'value': this.bubblesize(37337607), 'color': '#2E769F', 'population': 37337607 },
    { 'rank': 20, 'name': 'Italy', 'value': this.bubblesize(36442438), 'color': '#2E769F', 'population': 36442438 },
    { 'rank': 21, 'name': 'Thailand', 'value': this.bubblesize(32710169), 'color': '#7F38A0 ', 'population': 32710169 },
    { 'rank': 22, 'name': 'Canada', 'value': this.bubblesize(32602776), 'color': '#99295D ', 'population': 32602776 },
    { 'rank': 23, 'name': 'Argentina', 'value': this.bubblesize(30758972), 'color': '#364A98', 'population': 30758972 },
    { 'rank': 24, 'name': 'South Africa', 'value': this.bubblesize(30248355), 'color': '#816F28', 'population': 30248355 },
    { 'rank': 25, 'name': 'Pakistan', 'value': this.bubblesize(29965859), 'color': '#7F38A0 ', 'population': 29965859 },
    { 'rank': 26, 'name': 'Bangladesh', 'value': this.bubblesize(29738660), 'color': '#7F38A0 ', 'population': 29738660 },
    { 'rank': 27, 'name': 'Colombia', 'value': this.bubblesize(28287098), 'color': '#364A98', 'population': 28287098 },
    { 'rank': 28, 'name': 'Poland', 'value': this.bubblesize(28018492), 'color': '#2E769F', 'population': 28018492 },
    { 'rank': 29, 'name': 'Malaysia', 'value': this.bubblesize(24572446), 'color': '#7F38A0 ', 'population': 24572446 },
    { 'rank': 30, 'name': 'Saudi Arabia', 'value': this.bubblesize(23803319), 'color': '#7F38A0 ', 'population': 23803319 },
];
 bubblesize(value: number): number  {
    let max: number = 1347565324;
    let min: number = 324366;
    let maxBox: number = 70 * 70 * 2 * Math.PI;
    let minBox: number = 3 * 3 * 2 * Math.PI;
    let box: number = (value - min) / (max - min) * (maxBox - minBox) + minBox;
    if (box < minBox) {
        box = minBox;
    }
    return Math.sqrt(box / (Math.PI * 2)) / 2;
}
public maps: Maps;
public layers: object[] =  [
        {shapeDataPath: 'name',
            shapePropertyPath: 'name',
            shapeData:  worldMap,
            shapeSettings: {
                fill: '#E5E5E5'
            },
            bubbleSettings: [
                {
                    visible: true,
                    valuePath: 'value',
                    colorValuePath: 'color',
                    minRadius: 3,
                    maxRadius: 70,
                    opacity: 0.8,
                    dataSource: this.internetUsers,
                    tooltipSettings: {
                        visible: true,
                        valuePath: 'population',
                        template: '<div id="template"> <div class="toolback"> <div class="listing2"> <center> ${name} </center> </div> <hr style="margin-top: 2px;margin-bottom:5px;border:0.5px solid #DDDDDD"> <div> <span class="listing1">Rank : </span><span class="listing2">${rank}</span> </div> <div> <span class="listing1">Population : </span><span class="listing2">${population}</span> </div> </div> </div>'
                    },
                }
            ]
        }
    ]
  constructor() { }

  ngOnInit() {
     
  }

    // custom code end
    public zoomSettings: object= {
        enable: true,
        horizontalAlignment: 'Near',
        toolBarOrientation: 'Vertical',
        pinchZooming: true
    }
   
    public titleSettings : object =  {
        text: 'Top 30 countries with highest Internet users',
        titleStyle: {
            size: '16px'
        }
    }

   
    onZoom(args: IMapZoomEventArgs){
        if (args.scale["current"] > 2){
            this.layers[0]['bubbleSettings'][0].dataSource = this.internetUsers2;
            console.log(this.layers);
        } else {
            this.layers[0]['bubbleSettings'][0].dataSource = this.internetUsers;
        }
        this.maps.refresh();
       
    }

    bubbleRendering(args: IBubbleRenderingEventArgs){
        this.maps = args.maps;
    }
}
```



5 Replies

CA Camila Alvarez August 21, 2019 07:03 PM UTC

This is template I'm using:

<mat-card class="no-shadow">
  <mat-card-title class="card-title"> Searches by geolocation</mat-card-title>
   <ejs-maps id='container' style="display:block;" format='n' useGroupingSeparator=t rue [zoomSettings]='zoomSettings' [(layers)]='layers'
            [titleSettings]="titleSettings" (zoom)='onZoom($event)' (bubbleRendering)='bubbleRendering($event)'
            format='n' useGroupingSeparator="true">
    </ejs-maps>
</mat-card>



BP Baby Palanidurai Syncfusion Team August 22, 2019 06:21 AM UTC

Hi Camila, 

Greetings from Syncfusion. 

We have analyzed your query and your code snippet. In that, we have noticed that you are changing the bubble data source in the layer options instead of layers from maps instance. So that’s why changes not reflected in maps. We have changed your code snippet to resolve the reported scenario. Please find the below code snippet to achieve your requirement.  

Code snippet: 
onZoom(args: IMapZoomEventArgs) { 
    if (args.scale["current"] > 2) { 
      this.maps.layers[0]['bubbleSettings'][0].dataSource = this.internetUsers2; 
    } else { 
      this.maps.layers[0]['bubbleSettings'][0].dataSource = this.internetUsers; 
    } 
   // this.maps.refresh(); 
 
  } 



Kindly revert us, if you have any concerns. 

Regards, 
Baby. 



CA Camila Alvarez August 22, 2019 01:59 PM UTC

That works!
Thanks


CA Camila Alvarez August 22, 2019 02:47 PM UTC

Now something weird is happening. If I get to the point in which the datasource changes and move the map or zoom a little bit more, then the locations change in a wrong manner. I've attached a screenshot which displays this behavior.

Attachment: Screenshot_from_20190822_104403_1127a768.zip


BP Baby Palanidurai Syncfusion Team August 23, 2019 06:58 AM UTC

Hi Camila, 

Sorry for the inconvenience caused. 

We have checked the reported scenario at our end. When changing the data source, we need to refresh the maps. Now, we have changed the code snippet to resolve this issue. 

Code snippet: 
onZoom(args: IMapZoomEventArgs) { 
    if (args.scale["current"] > 2) { 
      this.maps.layers[0]['bubbleSettings'][0].dataSource = this.internetUsers2; 
      console.log(this.layers); 
    } else { 
      this.maps.layers[0]['bubbleSettings'][0].dataSource = this.internetUsers; 
    } 
    this.maps.refresh(); 
 
  } 



Kindly revert us, if you have any concerns. 

Regards, 
Baby. 


Loader.
Live Chat Icon For mobile
Up arrow icon