amcharts交互式地图:使选定状态成为可点击链接

时间:2018-03-15 19:03:10

标签: javascript css html5 blogs amcharts

我正在创建personal blog site。我偶然发现了一个互动访问状态地图,我想在其中一个html页面中实现。我能够使用他们提供的生成的html成功地将它放在我的网站上。但是,我想稍微调整一下,但我并不熟悉javascript。

我想添加两件事:

1st:将所选状态链接到特定的html页面。 第2(可选):单击未突出显示(已访问)的状态时,禁用缩放和颜色更改。

以下是我目前的代码:

<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 个答案:

答案 0 :(得分:1)

您可以将url属性添加到要链接的状态。如果您想在新标签/窗口中打开链接,也可以将urlTarget设置为"_blank"

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

我还建议在areasSettings中将autoZoom设置为false并将selectable设置为true,以便地图在触发网址之前不会尝试缩放:

  areasSettings: {
    autoZoom: false,
    selectable: true,

要在其他状态下禁用缩放和颜色更改,只需从getAreasFromMap: true中删除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>