2016-05-17 66 views
0

我需要添加一個屬性到數據集/標籤以在點擊時識別它。 我已經試過這樣的事情:chart.js 2.0將新屬性添加到數據集

Chart.controllers.customBar = Chart.controllers.bar.extend({ 
     code: null 
    }); 

和它差不多的作品,因爲我可以看到新屬性「代碼」,數據與之相關聯,圖表背景是可見的,但空的,我的意思是無論從數據集數據可見。

這是我的初始化:

var ctx = document.getElementById("roads-chart").getContext("2d"); 
    window.roadsBar = new Chart(ctx, { 
     type: 'bar', 
     data: roadsChartData, 
     options: { 
      title: { 
       display: true, 
       text: "Stan dróg" 
      }, 
      tooltips: { 
       mode: 'label' 
      }, 
      responsive: true, 
      scales: { 
       xAxes: [{ 
         stacked: true, 
        }], 
       yAxes: [{ 
         stacked: true 
        }] 
      }, 
      onClick: function(e, d) { 
       console.log(d); 
      } 
     } 
    }); 

的事情是,我不覺得chart.js之,有人可以幫我處理這件事,並顯示如何延長工作的例子嗎? :)

感謝

回答

0

雖然你可以擴展圖表要做到這一點,如果你不介意依靠內部特性(即可在chart.js之更高版本可能改變),你可以只使用_datasetIndex_index性質從圖表配置檢索您額外的屬性,像這樣

options: { 
    onClick: function(e, d) { 
     if (d.length > 0) { 
     console.log(this.config.data.datasets[d[0]._datasetIndex].code[d[0]._index]); 
     } 
    } 
    ... 

... 
    datasets: [{ 
    code: ['a', 'b', 'c', 'd', 'e', 'f', 'g'], 
    ... 

小提琴 - http://jsfiddle.net/55cdvb20/