简体   繁体   English

更改图例框大纲Chart.js 2

[英]Change Legend Box Outline Chart.js 2

I'm trying to remove the stroke color from the legend generated by ChartJS, but I can't quite seem to understand what the documentation is telling me. 我正试图从ChartJS生成的图例中删除笔触颜色,但我似乎无法理解文档告诉我的内容。

My current code looks like this: 我当前的代码如下所示:

legend: {
  position: 'bottom',
    labels: {
      generateLabels: function() {
        return {
          strokeStyle: "black",
        }
      }
    }
  }

Though I have tried this: 虽然我试过这个:

generateLabels: {
  strokeStyle: "black",
}

I'm setting the stroke style to "black" to see if I'm having any effect, but I'm not even sure if that's what I'm supposed to be doing. 我将笔画样式设置为“黑色”,看看我是否有任何效果,但我甚至不确定这是否是我应该做的。 I would think I could set it to none. 我想我可以把它设置为无。

Edit 编辑

Here is the code for my full object: 这是我的完整对象的代码:

  var ctx = $('#chart'); var data = { labels: ["1","2","3","4","5","6","7"], datasets: [{ data: [22,19,17,15,10,9,8], backgroundColor: [ '#df4344', '#fc9627', '#fcd949', '#d4d81e', '#6dc7ba', '#24a38e', '#263a7e', '#5050b2', '#4f7ec1', '#96afe2', ], borderColor: ["black"], borderWidth: 2 }] }; var wheel = new Chart(ctx, { type: "doughnut", data: data, options: { maintainAspectRatio: false, responsive: true, tooltips: false, cutoutPercentage: 60, } }); 
 <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="chart"></canvas> 

But now that I run it here, it seems to be working. 但是现在我在这里运行它似乎正在工作。 So I'm not sure what's wrong anymore. 所以我不知道什么是错的。

To remove the stroke from legend, you need to set borderWidth property to 0 for your dataset, like so ... 要从图例中删除笔划,您需要为数据集将borderWidth属性设置为0 ,如此...

datasets: [{
   borderWidth: 0,
   ...
}]

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩ ᴡᴏʀᴋɪɴɢᴡᴏʀᴋɪɴɢxᴀᴍᴘʟᴇ⧩

 var ctx = c.getContext('2d'); var chart = new Chart(ctx, { type: 'line', data: { labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'], datasets: [{ label: 'Statistics', data: [3, 1, 2, 5, 4], backgroundColor: 'rgba(0, 119, 204, 0.1)', borderColor: 'rgba(0, 119, 204, 0.8)', borderWidth: 0 //<-- set this }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script> <canvas id="c"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM