2014-03-02 67 views
0

我想在我的Highcharts图表上绘制箭头,并且到目前为止提出了this。看起来不错,但有问题:Highcharts路径上的SVG标记

  • 较高的笔划宽度会给出较长的箭头。
  • 旋转箭头将需要复杂的计算,如here

如果我可以使用在Highcharts路径like in this SVG tutorial绘制箭头的SVG标记将变得更加容易

我的代码:

renderer.path(['M', 200, 0, 'L', 200, 200,'L', 225, 200,'L',200,250,'L', 175, 200,'L', 200, 200]) 
    .attr({ 
     'stroke-width': 5, 
     stroke: 'red',fill:'red' 
    }) 
    .add(); 
renderer.path(['M', 400, 0, 'L', 400, 200,'L', 425, 200,'L',400,250,'L', 375, 200,'L', 400, 200]) 
    .attr({ 
     'stroke-width': 50, 
     stroke: 'red',fill:'red' 
    }) 
    .add(); 
+0

不知道这是真的有用,但是可能你周围的箭头中心旋转,所以这将是有点像这个.. http://jsfiddle.net/ZS9wj/1 / – Ian

回答

3

我设法画箭头,而无需使用SVG标记。无论旋转如何,箭头都指向正确的位置。它甚至可以考虑起点和终点的半径。

enter image description here

fiddle

function drawArrow(startX, startY, startRadius, endX, endY, endRadius, width) { 

    var angle = Math.PI + Math.atan((endX - startX)/(endY - startY)), 
     arrowLength = 3 * width, 
     arrowWidth = 1.5 * width, 
     path = [], 
     startArrowX, 
     startArrowY, 
     margin = 5; 

    if (endY >= startY) { 
     //correct for circle radius 
     startX -= ((startRadius + margin) * Math.sin(angle)); 
     startY -= ((startRadius + margin) * Math.cos(angle)); 
     endX += ((endRadius + margin) * Math.sin(angle)); 
     endY += ((endRadius + margin) * Math.cos(angle)); 

     //correct for arrow head length 
     endX += (arrowLength * Math.sin(angle)); 
     endY += (arrowLength * Math.cos(angle)); 

     //draw arrow head 
     path.push('M', endX, endY); 
     path.push(
      'L', 
     endX - arrowWidth * Math.cos(angle), 
     endY + arrowWidth * Math.sin(angle)); 
     path.push(
     endX - arrowLength * Math.sin(angle), 
     endY - arrowLength * Math.cos(angle)); 
     path.push(
     endX + arrowWidth * Math.cos(angle), 
     endY - arrowWidth * Math.sin(angle), 'Z'); 
    } else { 
     //correct for circle radius 
     startX += ((startRadius + margin) * Math.sin(angle)); 
     startY += ((startRadius + margin) * Math.cos(angle)); 
     endX -= ((endRadius + margin) * Math.sin(angle)); 
     endY -= ((endRadius + margin) * Math.cos(angle)); 

     //correct for arrow head length 
     endX -= (arrowLength * Math.sin(angle)); 
     endY -= (arrowLength * Math.cos(angle)); 

     //draw arrow head 
     path.push('M', endX, endY); 
     path.push(
      'L', 
     endX + arrowWidth * Math.cos(angle), 
     endY - arrowWidth * Math.sin(angle)); 
     path.push(
     endX + arrowLength * Math.sin(angle), 
     endY + arrowLength * Math.cos(angle)); 
     path.push(
     endX - arrowWidth * Math.cos(angle), 
     endY + arrowWidth * Math.sin(angle), 'Z'); 

    } 

    renderer.path(path) 
     .attr({ 
     'stroke-width': 1, 
     stroke: '#989898', 
     fill: '#989898' 
    }).add(); 
    renderer.path(['M', startX, startY, 'L', endX, endY]) 
     .attr({ 
     'stroke-width': width, 
     stroke: '#989898' 
    }).add();