javascript - Highcharts:如何缩小图例元素悬停时的气泡半径?

标签 javascript css highcharts

我想完成什么:

我试图在图例中表示气泡大小(z 值)。当该图例元素悬停在上方或关闭时,气泡应缩小到相同大小,就像正常的散点图一样。

到目前为止我尝试过的:

通过添加没有数据的新系列,我能够获得一个图例项来表示尺寸。我将有数据的 2 个系列链接到这个新系列。然后,我覆盖了 Highcharts 图例悬停功能,以便当“大小”图例元素悬停时,链接的系列保持完全可见。

像这样:

series: [
  {
    type: "bubble",
    name: HotSpotResources.Positive,
    color: "#2699FB",
    data: points[0], //example data like -> {x: 10, y: 12, z: 150, id: "some_id"}
    linkedTo: "nsize",
    showInLegend: true
  },
  {
    type: "bubble",
    name: HotSpotResources.Negative,
    color: "#F8A6A6",
    data: points[1],
    linkedTo: "nsize",
    showInLegend: true
  },
  {
    id: "nsize",
    type: "bubble",
    name: HotSpotsResources.nSize,
    color: "#4A4A4A",
    marker: {
      symbol: `url(${LayoutResources.AppRoot}/assets/images/nsize-icon.svg)`
    },
  }
],

和图例悬停覆盖:

(function (H) {
  H.Legend.prototype.setItemEvents = function (item, legendItem, useHTML) {
    const legend = this,
      boxWrapper = legend.chart.renderer.boxWrapper,
      activeClass = 'highcharts-legend-' + (item.series ? 'point' : 'series') + '-active',
      hasLinkedSeries = item.linkedSeries && item.linkedSeries.length ? true : false,
      setLinkedSeriesState = (item, state) => {
        item.linkedSeries.forEach((elem) => (elem.setState(state)));
      };

      // Set the events on the item group, or in case of useHTML, the item itself (#1249)
      (useHTML ? legendItem : item.legendGroup).on('mouseover', () => {
        if (item.visible) {
          item.setState('hover');
          // Add hover state to linked series
          if (hasLinkedSeries) {
            setLinkedSeriesState(item, 'hover');
          }
          // A CSS class to dim or hide other than the hovered series
          boxWrapper.addClass(activeClass);

          legendItem.css(legend.options.itemHoverStyle);
        }
      }).on('mouseout', () => {
        legendItem.css(H.merge(item.visible ? legend.itemStyle : legend.itemHiddenStyle));

        // A CSS class to dim or hide other than the hovered series
        boxWrapper.removeClass(activeClass);

        item.setState();
      }).on('click',(event) => {
        const strLegendItemClick = 'legendItemClick',
          fnLegendItemClick = () => {
            item.setVisible ? item.setVisible() : "";
          };

        // Pass over the click/touch event. #4.
        event = {
          browserEvent: event
        };

        // click the name or symbol
        if (item.firePointEvent) { // point
          item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
        } 
        else {
          H.fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
        }
    });
  };
})(Highcharts)

到目前为止一切顺利。

我尝试仅使用 css 缩小大小,但作为 svg 这是不可能的/它产生了不良结果。缩放它们也改变了它们的位置,所以这是不可能的。

.highcharts-series-hover {
  transform: scale(1.5);
  transition: transform 250ms;
}

通过更改标记状态,我能够缩小悬停时的气泡(气泡本身而不是图例项):

plotOptions: {
  bubble: {
    marker: {
      states: {
        hover: {
          radius: 4
        }
      }
    }
  }
}

但我希望在将鼠标悬停在尺寸图例元素上时得到相同的结果,而不是在将鼠标悬停在气泡上时得到相同的结果。

最后,这是我创建的图表的屏幕截图以供引用:

highcharts bubble example

任何人都可以找到一种方法来操纵 Highcharts 来完成这项任务或帮助我指明正确的方向吗?

最佳答案

您可以在 renderItem 方法的包装中添加 onmouseoveronmouseout 事件,并将未悬停的系列类型更改为 分散

(function(H) {
  H.wrap(H.Legend.prototype, 'renderItem', function(proceed, item) {
    proceed.call(this, item);

    var chart = this.chart,
      series = chart.series,
      element = item.legendGroup.element;

    element.onmouseover = function() {
      series.forEach(function(s) {
        if (s !== item) {
          s.update({
            type: 'scatter'
          }, false);
        }
      });

      chart.redraw();
    }
    element.onmouseout = function() {
      series.forEach(function(s) {
        if (s !== item) {
          s.update({
            type: 'bubble'
          }, false);
        }
      });

      chart.redraw();
    }
  });
}(Highcharts));
<小时/>

现场演示: https://jsfiddle.net/BlackLabel/r64f3w5n/

API引用: https://api.highcharts.com/class-reference/Highcharts.Series#update

文档: https://www.highcharts.com/docs/extending-highcharts/extending-highcharts

关于javascript - Highcharts:如何缩小图例元素悬停时的气泡半径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61331944/

相关文章:

javascript - Highchart 不显示带有 Ajax 数据的饼图

javascript - KeyboardEvent 的 Angular 4 单元测试

Javascript 添加和删除类不起作用

html - 在 Bootstrap 4 中重新着色 <abbr> 标签标题

javascript - 调整窗口大小时,如何将HTML页面上的所有元素保持在同一位置?

javascript - 使用 Highcharts 创建 marimekko 图表

javascript - 单击按钮时如何创建带有文本字段的表单

javascript - for...in 在 JavaScript 中是如何实现的?

html - 限制 html 页面滚动/摆脱非重复背景图像下方的多余部分

javascript - Highchart.js 在纯 IE8 中无法正常工作