javascript - 在 Highcharts 中,如何在 solidgauge 中创建发光/阴影效果?

标签 javascript css svg highcharts

我试图在仪表内部实现某种发光效果,但只能在外部实现。我还尝试在仪表内的圆圈/背景上添加发光效果,但它隐藏在绿色/橙色区域下方。

非常感谢任何帮助。

enter image description here

Highcharts.chart('container', {

  chart: {
    type: 'solidgauge',
    marginTop: 0
  },

  title: {
    text: false,
    style: {
      fontSize: '24px'
    }
  },

  tooltip: {
    borderWidth: 0,
    backgroundColor: 'none',
    shadow: false,
    style: {
      fontSize: '16px'
    },
    pointFormat: '{series.name}<br><span style="font-size:2em; color: {point.color}; font-weight: bold">{point.y}%</span>',
    positioner: function(labelWidth) {
      return {
        x: 200 - labelWidth / 2,
        y: 180
      };
    }
  },

  pane: {
    startAngle: 0,
    endAngle: 360,
    background: [{
      outerRadius: '88%',
      innerRadius: '70%',
      backgroundColor: 'white',
      borderWidth: 0
    }]
  },

  plotOptions: {
    solidgauge: {
      animation: false,
      dataLabels: {
        enabled: false
      },
      linecap: 'round',
      stickyTracking: false,
      rounded: true
    }
  },

  yAxis: {
    min: 0,
    max: 100,
    lineWidth: 0,
    tickPositions: []
  },

  series: [{
    name: 'Something',
    data: [{
      color: '#e76a0b',
      radius: '112%',
      innerRadius: '88%',
      y: 100,
      borderColor: '#e76a0b'
    }, {
      color: '#007272',
      radius: '112%',
      innerRadius: '88%',
      y: 40,
      borderColor: '#007272'
    }]
  }]
});
.highcharts-series-group {
  filter: url(#glow)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/solid-gauge.js"></script>

<div id="container" style="width: 400px; height: 400px; margin: 0 auto">
</div>
<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0" viewBox="0 0 300 300">
  <defs>
    <filter id="glow" x="-5000%" y="-5000%" width="10000%" height="10000%">
      <feFlood result="flood" flood-color="#000" flood-opacity=".2"></feFlood>
      <feComposite in="flood" result="mask" in2="SourceGraphic" operator="in"></feComposite>
      <feMorphology in="mask" result="dilated" operator="dilate" radius="3"></feMorphology>
      <feGaussianBlur in="dilated" result="blurred" stdDeviation="5"></feGaussianBlur>
      <feMerge>
        <feMergeNode in="blurred"></feMergeNode>
        <feMergeNode in="SourceGraphic"></feMergeNode>
      </feMerge>
    </filter>
  </defs>
</svg>

演示:http://jsfiddle.net/remisture/f2srey6g/4/

最佳答案

基于@GrzegorzBlachliński 的回答的完整解决方案。

为了创建可缩放的阴影,您可以在重绘和加载事件中根据图表的大小计算其大小。

const chart = Highcharts.chart('container', {
  chart: {
    type: 'solidgauge',
    marginTop: 50,
    events: {
        load () {
        const chart = this
        const el = document.querySelector('.highcharts-tracker > path.highcharts-point')
        const {x, y, width, height } = el.getBBox()

        chart.shadow = chart
        .renderer.image('http://i.imgur.com/frVkXOh.png', x+10, y+50, width, height)
        .attr({ zIndex: 10 })
        .add()
      },
        redraw () {
        const chart = this
        const el = document.querySelector('.highcharts-tracker > path.highcharts-point')
                const {x, y, width, height } = el.getBBox()

        chart.shadow.attr({ x: x+10, y: y + 50, width, height })
      }
    }
  },

  title: {
    text: 'Activity',
    style: {
      fontSize: '24px'
    }
  },

  tooltip: {
    borderWidth: 0,
    backgroundColor: 'none',
    shadow: false,
    style: {
      fontSize: '16px'
    },
    pointFormat: '{series.name}<br><span style="font-size:2em; color: {point.color}; font-weight: bold">{point.y}%</span>',
    positioner: function(labelWidth) {
      return {
        x: 200 - labelWidth / 2,
        y: 180
      };
    }
  },

  pane: {
    startAngle: 0,
    endAngle: 360,
    background: []
  },

  plotOptions: {
    solidgauge: {
      animation: false,
      dataLabels: {
        enabled: false
      },
      linecap: 'round',
      stickyTracking: false,
      rounded: true
    }
  },

  yAxis: {
    min: 0,
    max: 100,
    lineWidth: 0,
    tickPositions: []
  },

  series: [{
    name: 'Something',
    data: [{
      color: '#e76a0b',
      radius: '112%',
      innerRadius: '88%',
      y: 100,
      borderColor: '#e76a0b'
    }, {
      color: '#007272',
      radius: '112%',
      innerRadius: '88%',
      y: 40,
      borderColor: '#007272'
    }]
  }]
})

实例: http://jsfiddle.net/kumrruhq/

关于javascript - 在 Highcharts 中,如何在 solidgauge 中创建发光/阴影效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44648087/

相关文章:

css - SVG 填充仅在指定至少 2 个边时适用

javascript - 当汉堡菜单打开时,如何更改背景内容的不透明度?

javascript - 我怎样才能真正捕获 JavaScript 中花括号之间的内容?

javascript - transitionEnd 在 Firefox 中不起作用

javascript - c3js d3 dash-stroke 仅在 rect 元素的一侧

javascript - 如何在 2 个 SVG 之间交替转换

javascript - 问卷的正确数据结构是什么

html - 这种页面布局的名称是什么

html - 修复了 DIV 两侧的按钮高度

svg 符号标签中路径的 css3 关键帧动画