如何在悬停时为图表图例符号制作动画?

时间:2019-01-17 08:01:35

标签: reactjs highcharts

我正在尝试添加相同的动画,该动画可以在图例的饼图上使用,但找不到任何答案

下面是我想要获得的图像

attachemt

2 个答案:

答案 0 :(得分:0)

您需要将mouseovermouseout事件添加到图例项,并在事件回调函数中的图例符号元素上使用aniamte方法:

var H = Highcharts,
    chart = Highcharts.chart('container', {
        series: [{
            type: 'pie',
            showInLegend: true,
            data: [12, 15, 25]
        }]
    }),

    legendItems = chart.legend.allItems;

legendItems.forEach(function(item) {
    H.addEvent(item.legendGroup.element, 'mouseover', function() {
        item.legendSymbol.animate({
            width: 24,
            height: 24,
            translateX: -6,
            translateY: -6
        });
    });

    H.addEvent(item.legendGroup.element, 'mouseout', function() {
        item.legendSymbol.animate({
            width: 12,
            height: 12,
            translateX: 0,
            translateY: 0
        });
    });
});

实时演示:http://jsfiddle.net/BlackLabel/a0s9yhtd/

API参考:

https://api.highcharts.com/class-reference/Highcharts#.animate

https://api.highcharts.com/class-reference/Highcharts#.addEvent

答案 1 :(得分:0)

您还可以将drawLegendSymbol方法包装在Pie原型中,并呈现任何所需的内容,而没有任何限制。您将需要@ppotaczek建议的类似事件和方法。

(function(H) {
  H.wrap(H.seriesTypes.pie.prototype, 'drawLegendSymbol', function(proceed, legend, item) {
var options = legend.options,
  symbolHeight = legend.symbolHeight,
  square = options.squareSymbol,
  symbolWidth = square ? symbolHeight : legend.symbolWidth;
var series = this;
var halo;

item.legendSymbol = series.chart.renderer.rect(
    square ? (legend.symbolWidth - symbolHeight) / 2 : 0,
    legend.baseline - symbolHeight + 1, // #3988
    symbolWidth,
    symbolHeight,
    H.pick(legend.options.symbolRadius, symbolHeight / 2)
  )
  .addClass('highcharts-point')
  .attr({
    zIndex: 4
  }).add(item.legendGroup);

H.addEvent(item.legendItem.element, 'mouseover', function() {

  halo = series.chart.renderer.rect(
      square ? (legend.symbolWidth - symbolHeight) / 2 : 0,
      legend.baseline - symbolHeight + 1, // #3988
      symbolWidth,
      symbolHeight,
      H.pick(legend.options.symbolRadius, symbolHeight / 2)
    )
    .addClass('highcharts-point')
    .attr({
      fill: item.color,
      'fill-opacity': 0.25,
      zIndex: 1
    })
    .add(item.legendGroup);

    halo.animate({
      width: symbolWidth * 1.8,
      height: symbolHeight * 1.8,
      translateX: -symbolWidth * 0.4,
      translateY: -symbolHeight * 0.4,
      r: H.pick(legend.options.symbolRadius, symbolHeight / 2 + symbolHeight * 0.4)
    }, 0);
  });

  H.addEvent(item.legendItem.element, 'mouseout', function() {
    halo.animate({
      width: symbolWidth,
      height: symbolHeight,
      translateX: 0,
      translateY: 0,
      r: 0
    });
  });
})
})(Highcharts)

jsFiddle:https://jsfiddle.net/BlackLabel/5beq3psn/