How to draw arrows on a line-chart with Highcharts?

后端 未结 5 1298
逝去的感伤
逝去的感伤 2020-12-12 02:16

I\'m trying to add an arrow to the end of my Highcharts line-chart. I\'ve already looked through similar questions but the solutions don\'t seem to work:

The result

5条回答
  •  一向
    一向 (楼主)
    2020-12-12 02:52

    Linted /cleaned up version of jmfeland's answer with two series

    http://jsfiddle.net/vyfsf1ft/18/

    $(function() {
    
      (function(H) {
        var arrowCheck = false
        var pathTag
    
        H.wrap(H.Series.prototype, 'drawGraph', function(proceed) {
          // Apply the original function with the original arguments,
          // which are sliced off this function's arguments
          var series = this
          proceed.apply(series, Array.prototype.slice.call(arguments, 1))
    
          var arrowName = 'arrow' + series.name.replace(/\W/g, '_').toLowerCase()
          var arrowLength = 15
          var arrowWidth = 7
          var data = series.data
          var len = data.length
          var lastSeg = data[len - 1]
          var path = []
          var lastPoint = null
          var nextLastPoint = null
    
          if (lastSeg.y == 0) {
            lastPoint = data[len - 2]
            nextLastPoint = data[len - 1]
          } else {
            lastPoint = data[len - 1]
            nextLastPoint = data[len - 2]
          }
    
          var angle = Math.atan((lastPoint.plotX - nextLastPoint.plotX) /
            (lastPoint.plotY - nextLastPoint.plotY))
    
          if (angle < 0) angle = Math.PI + angle
    
          path.push('M', lastPoint.plotX, lastPoint.plotY)
    
          if (lastPoint.plotX > nextLastPoint.plotX) {
            if (arrowCheck === true) {
              pathTag = document.getElementById(arrowName)
              if (pathTag != null) {
                pathTag.remove(pathTag)
              }
            }
    
            path.push(
              'L',
              lastPoint.plotX + arrowWidth * Math.cos(angle),
              lastPoint.plotY - arrowWidth * Math.sin(angle)
            )
            path.push(
              lastPoint.plotX + arrowLength * Math.sin(angle),
              lastPoint.plotY + arrowLength * Math.cos(angle)
            )
            path.push(
              lastPoint.plotX - arrowWidth * Math.cos(angle),
              lastPoint.plotY + arrowWidth * Math.sin(angle),
              'Z'
            )
          } else {
            if (arrowCheck === true) {
              pathTag = document.getElementById(arrowName)
              if (pathTag != null) {
                pathTag.remove(pathTag)
              }
            }
    
            path.push(
              'L',
              lastPoint.plotX - arrowWidth * Math.cos(angle),
              lastPoint.plotY + arrowWidth * Math.sin(angle)
            )
            path.push(
              lastPoint.plotX - arrowLength * Math.sin(angle),
              lastPoint.plotY - arrowLength * Math.cos(angle)
            )
            path.push(
              lastPoint.plotX + arrowWidth * Math.cos(angle),
              lastPoint.plotY - arrowWidth * Math.sin(angle),
              'Z'
            )
          }
    
          series.chart.renderer.path(path)
            .attr({
              fill: series.color,
              id: arrowName,
            })
            .add(series.group)
    
          arrowCheck = true
        })
      }(Highcharts))
    
      var chart = new Highcharts.Chart({
        chart: {
          renderTo: 'container',
          defaultSeriesType: 'scatter'
        },
        xAxis: {
          min: 0,
          max: 10
        },
        plotOptions: {
          series: {
            animation: {
              duration: 2000
            },
            lineWidth: 2,
            marker: {
              enabled: false
            }
          },
          states: {
            hover: {
              enabled: true,
              lineWidth: 2
            },
          }
        },
        series: [{
          name: 'main',
          id: 'main',
          data: [
            [0, 0],
            [3, 4]
          ]
        }, {
          name: 'secondary',
          id: 'secondary',
          data: [
            [1, 0],
            [5, 8]
          ]
        }]
      })
    })
    

提交回复
热议问题