[英]Chart.js : how I can adjust Pie chart radius?
I've got a Piechart with two different Datasets: 我有一个带有两个不同数据集的Piechart:
How I can make internal pie chart bigger (or external thinner)? 如何使内部饼图更大(或更薄)? Is there any option how I can adjust it?
有什么选项可以调整吗?
Here is my code: 这是我的代码:
var ctx = $('#open_chart'); var chart = new Chart(ctx, { type: 'pie', data: { datasets: [{ data: [1, 5], backgroundColor: ['red', 'blue'], }], labels: ['Minor', 'Other'], }, options: { responsive: true, title: { display: true, text: 'Title', position: 'bottom', fontSize: 15, fontColor: '#000000' }, events: ['mousemove'], // cursor: pointer on hover onHover: function (event, chartElement) { event.target.style.cursor = chartElement[0] ? 'pointer' : 'default'; }, legend: { display: false } }, }); var newDataset = { data: [1, 3], backgroundColor: ['red', 'blue'], }; var config = { type: 'pie', data: { datasets: [{ data: [1, 3], backgroundColor: ['red', 'blue'], }], labels: ['Red', 'Blue'] }, options: { responsive: true } }; chart.data.datasets.push(newDataset); chart.update();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <div class="chart-container" style="position: relative; height:500px; width:300px"> <canvas id="open_chart" style="position:absolute;top:150px;" width="200" height="200"></canvas> </div>
I've tried to combine different pie charts: link , but it doesn't work. 我试图结合不同的饼图: link ,但是不起作用。
From the styling section of the pie chart documentation of the chart.js library available here , you will see that there is a weight
property. 从此处提供的chart.js库的饼图文档的样式部分,您将看到有一个
weight
属性。 This property is used exactly for making the circles bigger/smaller. 此属性恰好用于使圆圈变大/变小。
Basically, by playing with the weight of the two data sets you can adjust the sizes of the circles. 基本上,通过使用两个数据集的权重,您可以调整圆圈的大小。 In the code below, I set the weight of the first dataset to
1
and for the second dataset to 4
. 在下面的代码中,我将第一个数据集的权重设置为
1
,第二个数据集的权重设置为4
。
By running the snippet you will see the desired output. 通过运行代码片段,您将看到所需的输出。
Full code sample 完整代码示例
var ctx = $('#open_chart'); var chart = new Chart(ctx, { type: 'pie', data: { datasets: [{ data: [1, 5], backgroundColor: ['red', 'blue'], weight: 1 }], labels: ['Minor', 'Other'] }, options: { responsive: true, title: { display: true, text: 'Title', position: 'bottom', fontSize: 15, fontColor: '#000000' }, events: ['mousemove'], // cursor: pointer on hover onHover: function (event, chartElement) { event.target.style.cursor = chartElement[0] ? 'pointer' : 'default'; }, legend: { display: false }, }, }); var newDataset = { data: [1, 3], backgroundColor: ['red', 'blue'], weight: 4 }; var config = { type: 'pie', data: { datasets: [{ data: [1, 3], backgroundColor: ['red', 'blue'], }], labels: ['Red', 'Blue'] }, options: { responsive: true } }; chart.data.datasets.push(newDataset); chart.update();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <div class="chart-container" style="position: relative; height:500px; width:300px"> <canvas id="open_chart" style="position:absolute;top:150px;" width="200" height="200"></canvas> </div>
您是否尝试过在options { }
使用cutoutPercentage
进行试验?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.