为AmCharts点击自定义标记点击Javascript功能

时间:2017-01-17 19:27:48

标签: javascript onclick click addeventlistener amcharts

我正在尝试使用具有自定义HTML标记的AmChart世界地图(就像这个示例:https://www.amcharts.com/demos/custom-html-elements-map-markers/

我的代码如下所示,我已根据AmChart网站上的另一个示例添加了我希望的有效点击监听器。

var map;
map = AmCharts.makeChart( "chartdiv", {
  "type": "map",
  "theme": "black",
  "projection": "miller",

  "imagesSettings": {
    "rollOverColor": "#42adca",
    "rollOverScale": 3,
    "selectedScale": 3,
    "selectedColor": "#42adca",
    "color": "#42adca"
  },

  "areasSettings": {
    "unlistedAreasColor": "#42adca"
  },

  "dataProvider": {
    "map": "worldLow",
    "images": [ {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "San Jose, CA",
      "latitude": 37.3382,
      "longitude": -121.8863,
       "click": 'test()'
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Dulles, VA",
      "latitude": 38.9559,
      "longitude": -77.4478
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "London, UK",
      "latitude": 51.5074,
      "longitude": 0.1278
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Denver, CO",
      "latitude": 39.7392,
      "longitude": -104.9903
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Miami, FL",
      "latitude": 25.7617,
      "longitude": -80.1918
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Los Angeles, CA",
      "latitude": 34.0522,
      "longitude": -118.2437
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "New York, NY",
      "latitude": 40.7128,
      "longitude": -74.0059
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Chicago, IL",
      "latitude": 41.8781,
      "longitude": -87.6298
    } ]
  },
  "listeners": [{
    "event": "clickMapObject",
    "method": function(event) {
        console.log(event.mapObject.title);
    }
  }]
} );

但是,console.log并不会触发。点击地图不会做任何事情。我可以设置URL,但我想指定一个JS函数。

任何指针都会很棒。

1 个答案:

答案 0 :(得分:1)

发布后不久,我找到了答案。

'url'参数的作用类似于常规href,因此,这是有效的:

"dataProvider": {
    "map": "worldLow",
    "images": [ {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "San Jose, CA",
      "latitude": 37.3382,
      "longitude": -121.8863,
       "url": 'javascript:test()'
    }, {
      "zoomLevel": 5,
      "scale": 0.5,
      "title": "Dulles, VA",
      "latitude": 38.9559,
      "longitude": -77.4478
    }, {

注意“url”:'javascript:test()'