简体   繁体   English

ChartJS – 有没有办法去除饼图周围的空白?

[英]ChartJS – is there any way to remove blank space around pie charts?

饼形图

I am dealing with a chart that has unwanted spacing on left and right side.我正在处理一个在左侧和右侧有不需要的间距的图表。 I've been trying to remove it with no luck, and I don't know what else to do now.我一直在尝试删除它,但没有运气,现在我不知道还能做什么。 I've read the documentation thoroughly, but can't seem to find a solution.我已经彻底阅读了文档,但似乎找不到解决方案。 Is this possible to do?这是可能的吗? Let me know if more info is necessary, and I'll supply it.如果需要更多信息,请告诉我,我会提供。

Edit:编辑:

<div>
<canvas id="chart-gender"></canvas>  
</div>


<script>
var gender_data = [10, 35];

var graph_gender_preset = {
    labels: ["Female", "Male"],
    datasets: [
        {
            data: gender_data,
            backgroundColor: ["#0fa0e3", "#ff3549"]
        }
    ]
};

var ctx3 = $("#chart-gender");

var chart_gender = new Chart(ctx3, {
type: 'doughnut',
data: graph_gender_preset,
options: {
        responsive: true,
        title: {
            display: false,
            position: "top",
            fontStyle: "bold",
            fontSize: 0,
            fullWidth: false,
            padding: 0
        },
        legend: {
            display: false,
            position: "top",
            fullWidth: false,
            labels: { display: false, usePointStyle: true, fontSize: 15, fontStyle: "bold" }

        }
    }
});
</script>

The problem is not the doughnut, it is the canvas in which it is used.问题不在于甜甜圈,而在于使用它的画布。

The doughnut has to use a quadratic box, otherwise it would look like an ellipsis.甜甜圈必须使用二次框,否则它看起来像一个省略号。 So if you change the size of the canvas and make it quadratic you won't have any borders anymore.因此,如果您更改画布的大小并使其成为二次方,您将不再有任何边框。

Here is an JS Fiddle example .这是一个JS Fiddle 示例

<table border="1">
  <tr>
    <td>
      First
    </td>
    <td>
      <canvas width="100%" height="100%" id="myChart"></canvas>
    </td>
    <td>
      Third
    </td>
  </tr>
</table>

I've been having the same issue recently.我最近遇到了同样的问题。 My solution was to change the aspect ratio of the chart with我的解决方案是改变图表的纵横比

options: { aspectRatio: 1 }

According to the docs here the default is set to 2. If you change it to 1, the chart canvas will be square and there won't be all that whitespace around your doughnut / pie.根据此处的文档,默认设置为 2。如果您将其更改为 1,则图表画布将是方形的,并且您的甜甜圈/饼图周围不会有所有空白。

Hope this helps someone!希望这可以帮助某人!

After ton of research, I found that setting width and height will remove that space.经过大量研究,我发现设置宽度和高度将删除该空间。

<div>
<canvas id="chart-gender" width="300" height="300"></canvas>  
</div>

I think responsive needs to be set to false, then the height and width properties work like this:我认为responsive需要设置为false,那么height和width属性的工作方式是这样的:

const options= {
 responsive: false
} 
<div>
  <canvas id="chart-gender" width="300" options={options} height="300"></canvas>  
</div>

You have to set options for your Chart您必须为图表设置选项

JS : JS:

options = { aspectRatio: 1 }

HTML : HTML :

<canvas baseChart [options]="options"></canvas>

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

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