How can I remove the white border from Chart.js pi

2020-02-13 07:44发布

问题:

I am using Chart.js pie chart and I'd like to remove white lines between slices. Could someone tell me way to do this ? Thanks in advance

I didn't see anything in the documenation.

    <div class="pie-chart">
         <div id="canvas-holder">
              <canvas id="chart-area" width="250" height="250"/>
         </div>
    </div>

回答1:

UPDATE

For newer versions of Chart.js (i.e. 2.2.2 and higher), see @grebenyuksv's answer.

This answer was added for an older version of Chart.js (i.e. 1.0.2)


Original answer

Just configure the options for the chart to hide the line

segmentShowStroke: false

Something like this:

//create chart
var ctx = document.getElementById("myChart").getContext("2d");

var data = [{
  value: 300,
  color: "#F7464A",
  highlight: "#FF5A5E",
  label: "Red"
}, {
  value: 50,
  color: "#46BFBD",
  highlight: "#5AD3D1",
  label: "Green"
}, {
  value: 100,
  color: "#FDB45C",
  highlight: "#FFC870",
  label: "Yellow"
}];

var options = {
  //Boolean - Whether we should show a stroke on each segment
  // set to false to hide the space/line between segments
  segmentShowStroke: false
};

// For a pie chart
var myPieChart = new Chart(ctx).Pie(data, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
<canvas id="myChart" width="200" height="200"></canvas>



回答2:

In chart.js@2.2.2 (haven't tested for chart.js@2.x.x):

const options = {
    elements: {
        arc: {
            borderWidth: 0
        }
    }
};


回答3:

For newest Chartjs like 2.7.2 just put: borderWidth: 0 in the data

var ctx = $('#progress-chart');
    	var data = {
		    datasets: [{
		        data: [25, 50, 25],
		        backgroundColor: ['red', 'green', 'blue'],
		        borderWidth: 0, //this will hide border
		    }],

		    // These labels appear in the legend and in the tooltips when hovering different arcs
		    labels: [
		        'Red',
		        'Green',
		        'Blue'
		 	]
		};
        var progressChart = new Chart(ctx,{
		    type: 'pie',
		    data: data,
		    options: Chart.defaults.pie
		});
<div>
  <canvas id="progress-chart" width="500" height="250">   </canvas>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js">
    </script>



回答4:

datasets: [
            {
                label: "TeamB Score",
                data: [20, 35, 40, 60, 50],
                backgroundColor: [
                    "#FAEBD7",
                    "#DCDCDC",
                    "#E9967A",
                    "#F5DEB3",
                    "#9ACD32"
                ],
                borderColor: [
                    "#E9DAC6",
                    "#CBCBCB",
                    "#D88569",
                    "#E4CDA2",
                    "#89BC21"
                ],
                borderWidth: [1, 1, 1, 1, 1]
            }
        ]


回答5:

Chart.defaults.global.elements.arc.borderWidth = 0;

Place it at the beginning of your javascript code.