0

我希望如果您单击 Datamaps (D3) 中的链接,您将获得一个特殊的链接,但如果变量 blogentries 大于 0 或已设置,这应该是可能的。

我的代码:

<script>
	var map = new Datamap({
		element: document.getElementById('worldmap'),
		responsive: true,
		geographyConfig: {
			highlightOnHover: false,
			popupOnHover: false
		},
		fills: {
			'VISITED': '#13304F',
			defaultFill: '#d6e5f5'
		},
		data: {
			'FIN': {
				fillKey: 'VISITED',
				blogentries: 1
			},
			'AUT': {
				fillKey: 'VISITED',
				blogentries: 1
			},
		},
		done: function(datamap) {
			datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
        if (data.blogentries > 0) {
				  window.location = "https://www.link.com/" + (geography.properties.name);
        }
			});
		}
	});
	// Pure JavaScript
	window.addEventListener('resize', function() {
		map.resize();
	});

	// Alternatively with d3
	d3.select(window).on('resize', function() {
		map.resize();
	});
	// Alternatively with jQuery
	$(window).on('resize', function() {
		map.resize();
	});
</script>

谢谢你的帮助

4

1 回答 1

0

尝试

if (d3.select(geography).datum().blogentries > 0) {
    // ....
}

编辑

您必须将 map-fill-data 放在一个单独的变量中并geography.id使用blogentries

var mapData = {
  'FIN': {
    fillKey: 'VISITED',
    blogentries: 1
  },
  'AUT': {
    fillKey: 'VISITED',
    blogentries: 1
  },
};
var map = new Datamap({
  element: document.getElementById('worldmap'),
  responsive: true,
  geographyConfig: {
    highlightOnHover: false,
    popupOnHover: false
  },
  fills: {
    'VISITED': '#13304F',
    defaultFill: '#d6e5f5'
  },
  data: mapData,
  done: function(datamap) {
    datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
      if (mapData[geography.id] && mapData[geography.id].blogentries > 0) {
        window.location = "https://www.test.com/" + (geography.properties.name);
      }
    });
  }
});
于 2018-11-18T16:22:50.390 回答