2016-02-14 28 views

回答

1

您可以擴展Chart.js來執行此操作。初始化圖表後,只需覆蓋showTooltip方法即可。


預覽

enter image description here


腳本

Chart.types.Line.extend({ 
    name: "LineAlt", 
    initialize: function() { 
     Chart.types.Line.prototype.initialize.apply(this, arguments); 

     var originalShowTooltip = this.showTooltip; 
     this.showTooltip = function (activePoints) { 

      if (activePoints.length) { 
       var ctx = this.chart.ctx; 
       var scale = this.scale; 
       ctx.save(); 
       ctx.strokeStyle = '#aaa'; 
       ctx.beginPath(); 
       ctx.moveTo(activePoints[0].x, scale.startPoint); 
       ctx.lineTo(activePoints[0].x, scale.endPoint); 
       ctx.stroke(); 
       ctx.restore(); 
      } 

      return originalShowTooltip.apply(this, arguments); 
     } 
    } 
}); 

然後

new Chart(ctx).LineAlt(data); 

小提琴 - http://jsfiddle.net/98gz1fhw/

+0

這是夢幻般的,用chart.js之1.x的工作完美想知道是否可以使用Chart.js版本2完成同樣的事情(仍處於測試階段) –

+0

我很肯定答案是肯定的(Chart.js 2也有擴展名 - https://github.com/nnnick/ chart.js之/ BLOB/2.0-dev /目錄文檔/ 07-Advanced.md)。不幸的是,我對代碼還不熟悉。抱歉!雖然會在週末給出一個鏡頭。 – potatopeelings