2017-09-17 1 views
0

그래프 막대 값을 절반으로 나누었지만 제대로 작동하지 않았습니다!Chart.js의 가로 막대에서 텍스트 센터 수를 정렬하는 방법은 무엇입니까?

Code link

enter image description here

Chart.helpers.each(
 
      meta.data.forEach(function(bar, index) { 
 
       data = dataset.data[index]; 
 
       if (i == 0) { 
 
       ctx.fillText(data, bar._model.x/1.6, bar._model.y + 4); 
 
       } else { 
 
       ctx.fillText(data, bar._model.x-25, bar._model.y + 4); 
 
       } 
 
      }),

다음과 같이
+0

유 나를 –

+0

당신의 가치 중심을 보여주기 위해 노력하고 도울 수 있다면 알려 주시기 @GRUNT 각 바 .. [this] (https://i.stack.imgur.com/LBuOw.png)와 같은가요? –

+0

@ GRUNT 예! –

답변

1

당신은을 (meta.data 루프 내부) 각 수평 막대 의 중심 위치를 얻을 수 있습니다 :

var barWidth = bar._model.x - bar._model.base; 
var centerX = bar._model.base + barWidth/2; 

및 위치를받은 후, 그에 따라 카운트 텍스트를 그릴 ...

Chart.helpers.each(this.data.datasets.forEach(function(dataset, i) { 
    var meta = chartInstance.controller.getDatasetMeta(i); 
    Chart.helpers.each(meta.data.forEach(function(bar, index) { 
     var data = dataset.data[index]; 
     var barWidth = bar._model.x - bar._model.base; 
     var centerX = bar._model.base + barWidth/2; 
     if (i == 0) { 
     ctx.fillText(data, centerX, bar._model.y + 4); 
     } else { 
     ctx.fillText(data, centerX, bar._model.y + 4); 
     } 
    }), this); 
}), this); 

참조 - live demo

관련 문제