【问题标题】:Amchart map triggering on "hit" instead "hover"Amchart 地图触发“命中”而不是“悬停”
【发布时间】:2021-01-30 21:48:17
【问题描述】:

我有一张地图,我想在其中更改悬停颜色,然后单击固定颜色。它以某种方式出现故障,即使仅在悬停时(黑色而不是蓝色),它有时也会开始激活“命中”颜色。

当您单击一个区域然后将鼠标悬停在邻居上时,就会发生这种情况。

仅供参考,您可能会在全屏时看到更好。

你能告诉我我的代码有什么问题吗? 谢谢

    var map = am4core.create("chartdiv", am4maps.MapChart);

    map.seriesContainer.events.disableType("doublehit");
    map.chartContainer.background.events.disableType("doublehit");
    map.seriesContainer.draggable = false;
    map.seriesContainer.resizable = false;

    map.geodata = am4geodata_slovakiaLow;
    var polygonSeries = map.series.push(new am4maps.MapPolygonSeries());
    polygonSeries.useGeodata = true;

    map.projection = new am4maps.projections.Mercator();

    var polygonTemplate = polygonSeries.mapPolygons.template;
    polygonTemplate.tooltipText = "{name}";
    polygonTemplate.fill = am4core.color("white");
    polygonTemplate.stroke = am4core.color("black");


    var myEvent = polygonTemplate.events.on("hit", (ev) => {
        console.log("You clicked on :" + ev.target.dataItem.dataContext.name);

    }, this);

    // Create active state
   var activeState = polygonTemplate.states.create("active");
    activeState.properties.fill = am4core.color("black");

    // Create an event to toggle "active" state
    polygonTemplate.events.on("hit", function(ev) {
        polygonSeries.mapPolygons.each(function(polygon){
            polygon.setState("default");
        });

        ev.target.isActive = !ev.target.isActive;
    })


    var hs = polygonTemplate.states.create("hover");
    hs.properties.fill = am4core.color("#7660E6");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.amcharts.com/lib/4/core.js"></script>
<script src="//cdn.amcharts.com/lib/4/charts.js"></script>
<script src="//cdn.amcharts.com/lib/4/maps.js"></script>
<script src="https://cdn.amcharts.com/lib/4/geodata/slovakiaLow.js"></script>
<div id="chartdiv" style="width: 900px; height: 800px;"></div>

【问题讨论】:

  • 修复是什么意思?您是指将颜色从一种颜色更改为另一种颜色吗?
  • 是的,我想强调它不仅在悬停时保持固定。

标签: javascript jquery amcharts4


【解决方案1】:

您有两个 hit 事件处理程序。即使这不是问题,最好只有一个……为了代码的可读性。

问题是关于polygon.setState("default"); 老实说,我真的不知道它的作用......哈哈

但是你想要做的是删除所有多边形上的active“状态”......所以为什么不这样做:

polygonSeries.mapPolygons.each(function (polygon) {
  //polygon.setState("default");
  polygon.isActive = false
});

有效。

var map = am4core.create("chartdiv", am4maps.MapChart);

map.seriesContainer.events.disableType("doublehit");
map.chartContainer.background.events.disableType("doublehit");
map.seriesContainer.draggable = false;
map.seriesContainer.resizable = false;

map.geodata = am4geodata_slovakiaLow;
var polygonSeries = map.series.push(new am4maps.MapPolygonSeries());
polygonSeries.useGeodata = true;

map.projection = new am4maps.projections.Mercator();

var polygonTemplate = polygonSeries.mapPolygons.template;
polygonTemplate.tooltipText = "{name}";
polygonTemplate.fill = am4core.color("white");
polygonTemplate.stroke = am4core.color("black");

// Create active state
var activeState = polygonTemplate.states.create("active");
activeState.properties.fill = am4core.color("black");

// Create an event to toggle "active" state
polygonTemplate.events.on("hit", function (ev) {
  console.log("You clicked on :" + ev.target.dataItem.dataContext.name);
  polygonSeries.mapPolygons.each(function (polygon) {
    //polygon.setState("default");
    polygon.isActive = false
  });

  // Set active on the clicked one
  ev.target.isActive = true;
});

var hs = polygonTemplate.states.create("hover");
hs.properties.fill = am4core.color("#7660E6");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.amcharts.com/lib/4/core.js"></script>
<script src="//cdn.amcharts.com/lib/4/charts.js"></script>
<script src="//cdn.amcharts.com/lib/4/maps.js"></script>
<script src="https://cdn.amcharts.com/lib/4/geodata/slovakiaLow.js"></script>
<div id="chartdiv" style="width: 900px; height: 800px;"></div>

【讨论】:

  • 谢谢,它有效。正如文档所说的“默认”状态:“当我们需要重置被其他状态覆盖的元素属性时应用,例如当元素不再悬停时”。所以我认为这是我在创建活动状态时应该做的,我应该将其设置为默认值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多