简体   繁体   English

Chart.js 饼图,只有圆弧外侧有边框

[英]Chart.js piechart, only have border on the outside of arc

I have a pie chart made with chart.js and currently i'm wondering if its possible to only have a border for the outside of the circle / arc like so:我有一个用 chart.js 制作的饼图,目前我想知道是否可能只有圆/弧的外部有一个边框,如下所示:

在此处输入图片说明

I tried the following configurations but it applies border the to the whole segment.我尝试了以下配置,但它将边界应用于整个段。

        options: {
          elements: {
              arc: {
                  borderWidth: 0
              }
          }
        }

and also:还有:

    data: {
        datasets: [{
            data: [male_data , female_data],
            backgroundColor: pieColors1,
            borderWidth: [10, 0, 0, 0, 0]
        }]
    },

but this is what im getting:但这就是我得到的:

在此处输入图片说明

I guess you have to create your own color image (Solid color with white strip on top) and pass it in as a canvas image.我想您必须创建自己的彩色图像(顶部带有白色条纹的纯色)并将其作为画布图像传递。

Below is a modified example I've made based on the sample given from the chart.js documentation and a random picture found on the net.下面是我根据 chart.js 文档中给出的示例和在网上找到的随机图片所做的修改示例。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.0.0/Chart.js"></script> <canvas id="myChart" width="400" height="400"></canvas> <script> var img = new Image(); img.src = 'http://www.worktop-express.co.uk/media/gbu0/metro-tiles-white-sparkle-worktops-100717.jpg'; img.onload = function() { var ctx = document.getElementById("myChart").getContext('2d'); var fillPattern = ctx.createPattern(img, 'repeat'); var myChart = new Chart(ctx, { type: 'pie', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes', data: [12, 19, 3, 5, 2, 3], backgroundColor: [ 'rgba(255, 99, 132, 10)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', fillPattern ], borderColor: [ 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)' ], borderWidth: 0 }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } } }); } </script>

 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.0.0/Chart.js"></script> <canvas id="myChart" width="400" height="400"></canvas> <script> var img = new Image(); img.src = 'http://www.worktop-express.co.uk/media/gbu0/metro-tiles-white-sparkle-worktops-100717.jpg'; img.onload = function() { var ctx = document.getElementById("myChart").getContext('2d'); var fillPattern = ctx.createPattern(img, 'repeat'); var myChart = new Chart(ctx, { type: 'pie', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes', data: [12, 19, 3, 5, 2, 3], backgroundColor: [ 'rgba(255, 99, 132, 10)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', fillPattern ], borderColor: [ 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)', 'rgba(0,0,0,100)' ], borderWidth: 1 }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } } }); } </script>

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

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