【问题标题】:How to Increase the label font size and decrease the size of my pie using chart.js?如何使用 chart.js 增加标签字体大小并减小我的饼图大小?
【发布时间】:2020-12-18 01:17:15
【问题描述】:

这是我使用的代码:

var ctx = document.getElementById("pages_breakdown").getContext('2d');
    ctx.width = 400;
    ctx.height = 300;
    var myChart = new Chart(ctx, {
        type: 'pie',
        data: {
            labels: ['Primary pages {{ $array_pages_breakdown[0] }}',
                     'Dupliacte pages {{ $array_pages_breakdown[1] }}',
                     'Paginated 2+ {{ $array_pages_breakdown[2] }}',
                     'Mobile alternates {{ $array_pages_breakdown[3] }}',
                     'Non-indexable pages {{ $array_pages_breakdown[4] }}',
                     'Non-200 pages {{ $array_pages_breakdown[5] }}',
                     'Failed URL {{ $array_pages_breakdown[6] }}'],
            datasets: [{

                data: [ {{ $array_pages_breakdown[0] }}, {{ $array_pages_breakdown[1] }}, {{ $array_pages_breakdown[2] }}, {{ $array_pages_breakdown[3] }}, {{ $array_pages_breakdown[4] }}, {{ $array_pages_breakdown[5] }}, {{ $array_pages_breakdown[6] }}],
                backgroundColor: [
                                    'rgba(59, 153, 129, 1.0)',
                                    'rgba(232, 179, 58, 1.0)',
                                    'rgba(214, 212, 209, 1.0)',
                                    'rgba(75, 192, 192, 1.0)',
                                    'rgba(122, 120, 117, 1.0)',
                                    'rgba(224, 33, 81, 1.0)',
                                    'rgba(244, 90, 39, 1.0)'
                                ]

                }]
        },
        options: {
            rotation: (0.5 * Math.PI) - (95/180 * Math.PI),
            responsive: false,
            legend: {
                    position: 'left'
            },
            layout: {
                padding:{
                    left: 30,
                    right: 0,
                    top: 0,
                    bottom: 0
                }
            } 
        } 

    });

这是输出:

预期的输出是这样的:

如何修复我的标签以使其更大一点并减小我的馅饼的大小?我试图将fontSize 放在我的选项中,但它没有改变任何东西。我也想拥有这些 %,我尝试了this,但是当我在图表上使用数据点时它崩溃了。

【问题讨论】:

  • 你需要创建自己的图例,否则使用chart.js图例看起来很困难。
  • @KunalKhivensara 是吗?谢谢你的建议。

标签: javascript charts chart.js


【解决方案1】:

我不知道你是否看过chart.js 上的文档,无论如何我在这里发布一些你正在寻找的想法。

  options: {
        legend: {
            display: true,
            labels: {
                fontColor: 'rgb(255, 99, 132)'
            }
        }
}

如需增强,请查看this

【讨论】:

  • 谢谢。我去看看。
【解决方案2】:

你可以添加这个:

options: {
    **maintainAspectRatio : true**, // make the pie small
     legend: {
        display: true,
        labels: {
              **fontSize: 15** //change the size of the labels
        }
    }

}

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多