2017-03-09 5 views
0

Chartjs-2를 사용하여 reactjs에서 일부 데이터를 표시하려고합니다. 내 데이터를 표시하려면 도넛 형 원형 차트가 필요합니다. 내가 특정 파이 차트를 사용하는 경우는 같은 날을 보여줍니다 enter image description hereChartjs- 2 도넛 차트 맞춤형 툴팁

그러나 원하는 출력 이런 식으로 뭔가해야한다 : 나는이 문서에서 읽고 enter image description here

내가 옵션에서 툴팁을 수정해야하지만, 나는 이것을 할 길을 찾을 수 없다. 어떻게 아십니까?

답변

1

차트 옵션에 사용자 지정 도구 설명을 구현해야합니다. html 요소 스타일과 크기를 원하는 것으로 바꾸어야합니다.

var에 myPieChart = 새로운 차트 (CTX, { 유형 : '파이', 데이터 : 데이터, 옵션 : { 도구 설명 : { 정의 : 기능 (tooltipModel) { // 툴팁 요소 var에 tooltipEl = document.getElementById ('chartjs-tooltip');

  // Create element on first render 
      if (!tooltipEl) { 
       tooltipEl = document.createElement('div'); 
       tooltipEl.id = 'chartjs-tooltip'; 
       tooltipEl.innerHTML = "<table></table>" 
       document.body.appendChild(tooltipEl); 
      } 

      // Hide if no tooltip 
      if (tooltipModel.opacity === 0) { 
       tooltipEl.style.opacity = 0; 
       return; 
      } 

      // Set caret Position 
      tooltipEl.classList.remove('above', 'below', 'no-transform'); 
      if (tooltipModel.yAlign) { 
       tooltipEl.classList.add(tooltipModel.yAlign); 
      } else { 
       tooltipEl.classList.add('no-transform'); 
      } 

      function getBody(bodyItem) { 
       return bodyItem.lines; 
      } 

      // Set Text 
      if (tooltipModel.body) { 
       var titleLines = tooltipModel.title || []; 
       var bodyLines = tooltipModel.body.map(getBody); 

       var innerHtml = '<thead>'; 

       titleLines.forEach(function(title) { 
        innerHtml += '<tr><th>' + title + '</th></tr>'; 
       }); 
       innerHtml += '</thead><tbody>'; 

       bodyLines.forEach(function(body, i) { 
        var colors = tooltipModel.labelColors[i]; 
        var style = 'background:' + colors.backgroundColor; 
        style += '; border-color:' + colors.borderColor; 
        style += '; border-width: 2px'; 
        var span = '<span class="chartjs-tooltip-key" style="' + style + '"></span>'; 
        innerHtml += '<tr><td>' + span + body + '</td></tr>'; 
       }); 
       innerHtml += '</tbody>'; 

       var tableRoot = tooltipEl.querySelector('table'); 
       tableRoot.innerHTML = innerHtml; 
      } 

      // `this` will be the overall tooltip 
      var position = this._chart.canvas.getBoundingClientRect(); 

      // Display, position, and set styles for font 
      tooltipEl.style.opacity = 1; 
      tooltipEl.style.left = position.left + tooltipModel.caretX + 'px'; 
      tooltipEl.style.top = position.top + tooltipModel.caretY + 'px'; 
      tooltipEl.style.fontFamily = tooltipModel._fontFamily; 
      tooltipEl.style.fontSize = tooltipModel.fontSize; 
      tooltipEl.style.fontStyle = tooltipModel._fontStyle; 
      tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px'; 
     } 
    } 
} 

});