amcharts interactive map: making selected states a clickable link

我怕爱的太早我们不能终老 提交于 2019-12-13 03:38:17

问题


I am creating a personal blog site. I stumbled upon an interactive visited states map in which I wanted to implement in one of my html page. I was able to successfully put it on my website with the generated html they provided. However, I wanted to tweak it a little bit but I'm not all familiar with javascript.

There are two things I want to add:

1st: Make the selected states link to a specific html page. 2nd (optional): Disable the zoom and color change when clicking on states that are not highlighted(visited).

Here is the code I have currently:

<script src="https://www.amcharts.com/lib/3/ammap.js" type="text/javascript"></script>
<script src="https://www.amcharts.com/lib/3/maps/js/usaHigh.js" type="text/javascript"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js" type="text/javascript"></script>
<div id="mapdiv" style="width: 1000px; height: 450px; display: block; margin-left: auto; margin-right: auto; margin-top: 100px; cursor: default;"></div>
<script type="text/javascript">
var map = AmCharts.makeChart("mapdiv", {
type: "map",
theme: "light",
backgroundColor : "#FFFFFF",
backgroundAlpha : 1,
zoomControl: {
zoomControlEnabled : false
},
dataProvider : {
map : "usaHigh",
getAreasFromMap : true,
areas :
[
	{
		"id": "US-AZ",
		"showAsSelected": true
	},
	{
		"id": "US-CA",
		"showAsSelected": true
	},
	{
		"id": "US-DC",
		"showAsSelected": true
	},
	{
		"id": "US-ID",
		"showAsSelected": true
	},
	{
		"id": "US-MA",
		"showAsSelected": true
	},
	{
		"id": "US-MT",
		"showAsSelected": true
	},
	{
		"id": "US-NJ",
		"showAsSelected": true
	},
	{
		"id": "US-NV",
		"showAsSelected": true
	},
	{
		"id": "US-NY",
		"showAsSelected": true
	},
	{
		"id": "US-OR",
		"showAsSelected": true
	},
	{
		"id": "US-PA",
		"showAsSelected": true
	},
	{
		"id": "US-WA",
		"showAsSelected": true
	},
	{
		"id": "US-WY",
		"showAsSelected": true
	}
]
},
areasSettings : {
autoZoom : true,
color : "#B4B4B7",
colorSolid : "#DB4646",
selectedColor : "#DB4646",
outlineColor : "#666666",
rollOverColor : "#9EC2F7",
rollOverOutlineColor : "#000000"
}
});
</script>

回答1:


You can add a url property to the states you want a link to. You can also set urlTarget to "_blank" if you want to make the link open in a new tab/window:

areas: [{
    "id": "US-AZ",
    "showAsSelected": true,
    "url": "http://az.gov",
    "urlTarget": "_blank"
  },
  {
    "id": "US-CA",
    "showAsSelected": true,
    "url": "http://ca.gov/",
    "urlTarget": "_blank"
  },
  // ... etc

I also recommend setting autoZoom to false and selectable to true in areasSettings so that the map doesn't try to zoom before triggering the URL:

  areasSettings: {
    autoZoom: false,
    selectable: true,

To disable the zoom and color change on the other states, simply remove getAreasFromMap: true from your dataProvider.

var map = AmCharts.makeChart("mapdiv", {
  type: "map",
  theme: "light",
  backgroundColor: "#FFFFFF",
  backgroundAlpha: 1,
  zoomControl: {
    zoomControlEnabled: false
  },
  dataProvider: {
    map: "usaHigh",
    areas: [{
        "id": "US-AZ",
        "showAsSelected": true,
        "url": "http://az.gov",
        "urlTarget": "_blank"
      },
      {
        "id": "US-CA",
        "showAsSelected": true,
        "url": "http://ca.gov/",
        "urlTarget": "_blank"
      },
      {
        "id": "US-DC",
        "showAsSelected": true
      },
      {
        "id": "US-ID",
        "showAsSelected": true
      },
      {
        "id": "US-MA",
        "showAsSelected": true
      },
      {
        "id": "US-MT",
        "showAsSelected": true
      },
      {
        "id": "US-NJ",
        "showAsSelected": true
      },
      {
        "id": "US-NV",
        "showAsSelected": true
      },
      {
        "id": "US-NY",
        "showAsSelected": true
      },
      {
        "id": "US-OR",
        "showAsSelected": true
      },
      {
        "id": "US-PA",
        "showAsSelected": true
      },
      {
        "id": "US-WA",
        "showAsSelected": true
      },
      {
        "id": "US-WY",
        "showAsSelected": true
      }
    ]
  },
  areasSettings: {
    autoZoom: false,
    selectable: true,
    color: "#B4B4B7",
    colorSolid: "#DB4646",
    selectedColor: "#DB4646",
    outlineColor: "#666666",
    rollOverColor: "#9EC2F7",
    rollOverOutlineColor: "#000000"
  }
});
<script src="https://www.amcharts.com/lib/3/ammap.js" type="text/javascript"></script>
<script src="https://www.amcharts.com/lib/3/maps/js/usaHigh.js" type="text/javascript"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js" type="text/javascript"></script>
<div id="mapdiv" style="width: 1000px; height: 450px; display: block; margin-left: auto; margin-right: auto; margin-top: 100px; cursor: default;"></div>


来源:https://stackoverflow.com/questions/49307377/amcharts-interactive-map-making-selected-states-a-clickable-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!