2016-05-13 97 views
31

我有以下的代碼創建一個使用chart.js之V2.1.3圖:chart.js之V2隱藏數據集標籤

var ctx = $('#gold_chart'); 
var goldChart = new Chart(ctx, { 
    type: 'line', 
    data: { 
     labels: dates, 
     datasets: [{ 
      label: 'I want to remove this Label', 
      data: prices, 
      pointRadius: 0, 
      borderWidth: 1 
     }] 
    } 
}); 

的代碼看起來很簡單,但我不能從圖中刪除的標籤。我嘗試了很多我在網上找到的解決方案,但其中大多數都使用Chart.js v1.x.

如何刪除數據集標籤?

回答

66

只需設定labeltooltip選項,像這樣

... 
options: { 
    legend: { 
     display: false 
    }, 
    tooltips: { 
     callbacks: { 
      label: function(tooltipItem) { 
        return tooltipItem.yLabel; 
      } 
     } 
    } 
} 

小提琴 - http://jsfiddle.net/g19220r6/

+0

作品般的魅力,謝謝。順便說一句,如何改變折線圖的漸變顏色? – Raptor

+1

你的意思是,如何使用漸變作爲borderColor/backgroundColor。只需在上下文中創建一個,並在初始化時使用它 - 請參閱http://jsfiddle.net/g9h6gtvx/ – potatopeelings

+0

很好,謝謝 – Raptor

6

地址:

Chart.defaults.global.legend.display = false; 
在你的腳本代碼開始

;

0

你也可以把工具提示到一個行通過刪除 「標題」:

this.chart = new Chart(ctx, { 
    type: this.props.horizontal ? 'horizontalBar' : 'bar', 
    options: { 
     legend: { 
      display: false, 
     }, 
     tooltips: { 
      callbacks: { 
       label: tooltipItem => `${tooltipItem.yLabel}: ${tooltipItem.xLabel}`, 
       title:() => null, 
      } 
     }, 
    }, 
}); 

enter image description here