0

I am using below code for rendering a map when no data is available. The map is working fine when data comes. I tried serveral methods to add label but it's not working. The last two lines show what I've tried so far to add a label

// build map
AmCharts.ready(function () {
    AmCharts.theme = AmCharts.themes.dark;
    window.map = new AmCharts.AmMap();

    window.map.areasSettings = {
        unlistedAreasColor: "#000000",
        unlistedAreasAlpha: 0.1
    };
    window.map.imagesSettings.balloonText = "<span style='font-size:14px;'><b>[[title]]</b>: [[value]]</span>";

    var dataProvider = {
        mapVar: AmCharts.maps.worldLow,
        images: []
    }

    // create circle for each country


    // it's better to use circle square to show difference between values, not a radius
    var maxSquare = maxBulletSize * maxBulletSize * 2 * Math.PI;
    var minSquare = minBulletSize * minBulletSize * 2 * Math.PI;

    // create circle for each country
    for (var i = 0; i < mapData.length; i++) {
        var dataItem = mapData[i];
        var value = dataItem.value;
        // calculate size of a bubble
        var square = (value - min) / (max - min) * (maxSquare - minSquare) + minSquare;
        if (square < minSquare) {
            square = minSquare;
        }
        var size = Math.sqrt(square / (Math.PI * 2));
        var id = dataItem.code;

        dataProvider.images.push({
            type: "circle",
            width: size,
            height: size,
            color: "#ff9a00",
            longitude: latlong[id].longitude,
            latitude: latlong[id].latitude,
            title: dataItem.name,
            value: value
        });
    }

    window.map.dataProvider = dataProvider;
    window.map.export = {
        enabled: true
    }
    window.map.projection = "winkel3";
    window.map.write("dash_chart_world");
});

// add label
AmCharts.AmMap.mapdiv.addLabel(0, '50%', 'You don\'t have any sales yet', 'center');

// set opacity of the chart div
AmCharts.AmMap.mapdiv.style.opacity = 0.5;

Any help is appreciated. Thanks

4

1 回答 1

1

您错过的是,每当图表发生问题时都需要调用该函数。由于您的需求与数据耦合,我们可以添加一个事件侦听器dataUpdated并检查图像数组是否为空:

map.addListener("dataUpdated", function() {
    if (map.dataProvider.images.length == 0) {
        // add label
        map.addLabel(0, '50%', 'You don\'t have any sales yet', 'center');

        // set opacity of the chart div
        map.mapContainer.node.style.opacity = 0.1;
  }
});

关于我所做的其他更改:

  • 该方法addLabel属于一个AmChart对象而不 属于AmCharts.AmMap.mapdiv. (这甚至不存在。)
  • 更改div包含整个地图的不透明度(可以使用map.div[ AmChart.div] 获得)也会改变添加标签的不透明度。如果您只想更改地图本身的不透明度,则可以对map.mapContainer.node元素进行更改。(这是svg持有地图投影的部分。)

我准备了一个小演示,您可以在其中看到它处理动态数据。

于 2016-07-27T10:01:38.627 回答