Chartjs - 更改工具提示边界半径

时间:2017-06-20 23:05:18

标签: javascript jquery charts chart.js

我使用chartjs创建了一个简单的水平条形图。

var ctx = $('#myChart');
var myChart = new Chart(ctx, {
  type: 'horizontalBar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1
    }]
  },
});

现在我的要求是更改工具提示的边框半径以使其成为矩形。我尝试过更改边框半径属性,但它似乎不起作用。有谁知道如何更改工具提示的半径?

提前致谢。

1 个答案:

答案 0 :(得分:1)

这可以通过为图表选项中的工具提示设置cornerRadius属性为0 (或任意数字)来实现。

options: {
   tooltips: {
      cornerRadius: 0
   },
   ...
}

工作示例



var ctx = $('#myChart');
var myChart = new Chart(ctx, {
   type: 'horizontalBar',
   data: {
      labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
      datasets: [{
         label: '# of Votes',
         data: [12, 19, 3, 5, 2, 3],
         backgroundColor: [
            'rgba(255, 99, 132, 0.2)',
            'rgba(54, 162, 235, 0.2)',
            'rgba(255, 206, 86, 0.2)',
            'rgba(75, 192, 192, 0.2)',
            'rgba(153, 102, 255, 0.2)',
            'rgba(255, 159, 64, 0.2)'
         ],
         borderColor: [
            'rgba(255,99,132,1)',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
            'rgba(153, 102, 255, 1)',
            'rgba(255, 159, 64, 1)'
         ],
         borderWidth: 1
      }]
   },
   options: {
      tooltips: {
         cornerRadius: 0 //<- set this
      },
      scales: {
         xAxes: [{
            ticks: {
               beginAtZero: true
            }
         }]
      }
   }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="myChart"></canvas>
&#13;
&#13;
&#13;