为什么我的ammap区域颜色不起作用

时间:2017-08-08 03:11:28

标签: angularjs amcharts ammap

  

1.图片

This is the code running result

This is debug message

  

2.question

由于控制台调试图片,我在dataProvider.areas中的颜色设置是成功的,但colorReal不等于颜色,地图显示颜色是colorReal。

我该如何解决?

  

3.code

这是我的代码

(function () {
  'use strict';

  angular.module('dashboard')
      .controller('DashboardMapCtrl', DashboardMapCtrl);

  function DashboardMapCtrl(baConfig, layoutPaths, $http) {
    var layoutColors = baConfig.colors;
    var areaTb = [{
      "id":"CN-34",
      "title":"中国安徽"
    },
     .......more data......
      {
        "id":"CN-33",
        "title":"中国浙江"
      }
    ];
    var http = $http({
      method:'POST',
      url:'http://******'
    });

    function getAreas(http, areaTb) {
      http.then(function successCallback(response) {
        angular.forEach(areaTb,function (obj,key) {
          if(typeof(response.data[obj.title]) != 'undefined') {
            obj.customData = response.data[obj.title];
            obj.color = obj.customData < 100 ? layoutColors.colorLevel1 :
                (100 <= obj.customData && obj.customData< 500) ? layoutColors.colorLevel2 :
                (500 <= obj.customData && obj.customData< 1000) ? layoutColors.colorLevel3 :
                (1000 <= obj.customData && obj.customData< 3000) ? layoutColors.colorLevel4 :
                (3000 <= obj.customData && obj.customData< 5000) ? layoutColors.colorLevel5 :
                (5000 <= obj.customData && obj.customData< 7000) ? layoutColors.colorLevel6 :
                (7000 <= obj.customData && obj.customData< 10000) ? layoutColors.colorLevel7 :
                (10000 <= obj.customData && obj.customData< 30000) ? layoutColors.colorLevel8 :
                (30000 <= obj.customData && obj.customData< 50000) ? layoutColors.colorLevel9 :
                (50000 <= obj.customData && obj.customData< 70000) ? layoutColors.colorLevel10 :
                (70000 <= obj.customData && obj.customData< 100000) ? layoutColors.colorLevel11 : layoutColors.colorLevel12;
          }
        });
      }, function errorCallback(response) {
      });
    }
      getAreas(http, areaTb);
      console.log(areaTb);
    var map = AmCharts.makeChart('amChartMap', {
      type: 'map',
      theme: 'blur',
      zoomControl: { zoomControlEnabled: false, panControlEnabled: false },
      dataProvider: {
        map: 'chinaLow',
        zoomLevel: 1,
        areas: areaTb
      },
    });
  }
})();

1 个答案:

答案 0 :(得分:0)

问题在于,您在创建地图后设置地图区域,因为getAreas是异步的。要在加载地图后更新地图,您必须在地图对象上调用validateData。一个简单的解决方法是将map对象传递给你的getAreas调用,直接为它分配区域,然后使用validateData重绘,即

var map = AmCharts.makeChart('amChartMap', {
  type: 'map',
  theme: 'blur',
  zoomControl: { zoomControlEnabled: false, panControlEnabled: false },
  dataProvider: {
    map: 'chinaLow',
    zoomLevel: 1,
    areas: []
  },
});

function getAreas(http, areaTb, map) {
  http.then(function successCallback(response) {
    angular.forEach(areaTb,function (obj,key) {
      if(typeof(response.data[obj.title]) != 'undefined') {
        obj.customData = response.data[obj.title];
        obj.color = obj.customData < 100 ? layoutColors.colorLevel1 :
            (100 <= obj.customData && obj.customData< 500) ? layoutColors.colorLevel2 :
            (500 <= obj.customData && obj.customData< 1000) ? layoutColors.colorLevel3 :
            (1000 <= obj.customData && obj.customData< 3000) ? layoutColors.colorLevel4 :
            (3000 <= obj.customData && obj.customData< 5000) ? layoutColors.colorLevel5 :
            (5000 <= obj.customData && obj.customData< 7000) ? layoutColors.colorLevel6 :
            (7000 <= obj.customData && obj.customData< 10000) ? layoutColors.colorLevel7 :
            (10000 <= obj.customData && obj.customData< 30000) ? layoutColors.colorLevel8 :
            (30000 <= obj.customData && obj.customData< 50000) ? layoutColors.colorLevel9 :
            (50000 <= obj.customData && obj.customData< 70000) ? layoutColors.colorLevel10 :
            (70000 <= obj.customData && obj.customData< 100000) ? layoutColors.colorLevel11 : layoutColors.colorLevel12;
      }
    });
    //assign the areas object to the dataProvider, then redraw
    map.dataProvider.areas = areaTb;
    map.validateData();
  }, function errorCallback(response) {
  });
}
getAreas(http, areaTb, map);

或者,您可以在成功回调期间将makeChart调用放在getAreas方法中。