首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chart js页脚

Chart js页脚
EN

Stack Overflow用户
提问于 2020-08-18 15:17:53
回答 1查看 167关注 0票数 0

我正在尝试添加一个带有图表描述的页脚到chart js。到目前为止,我得到了将文本添加到画布的插件。现在的问题是我不能扩展画布高度来适应描述。在不改变图表本身高度的情况下,在图表下增加一些空格进行描述。我的插件是:

代码语言:javascript
复制
    beforeDraw: function (chart) {
    var height = chart.chart.height,
    ctx = chart.chart.ctx;
    ctx.restore();
    ctx.font = "1em sans-serif";
    ctx.fillText("chart description chart description chart description", 20, height -10); //-10 added so decription is visible at all
    ctx.save();
}

看起来是这样的:

所以你可以看到,它的描述和x标签在同一行。如果我添加到高度,描述是完全不可见的。如何更改画布的高度,以便为描述添加空间?如果我只改变画布的高度,它也会扩展图表。

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-20 17:58:01

好了,我想通了。基本上,我是在BeforeInit中添加填充,然后在BeforeDraw中添加描述(这里只是本质):

代码语言:javascript
复制
beforeInit: function (chart, options) {
    let isPieOrDoughnut = chart.config.type === 'pie' || chart.config.type === 'doughnut';
    if (isPieOrDoughnut) {
        chart.options.scales.xAxes[0].gridLines.lineWidth = 0;
        chart.options.scales.xAxes[0].display = true;
    }

    chart.options.scales.xAxes[0].scaleLabel.padding.bottom = 50;
},
beforeDraw: function (chart, options) {
    ctx = chart.chart.ctx;
    ctx.restore();
    ctx.font = "1em sans-serif";
    let height = chart.chart.height;
    ctx.fillText('description', 30, height - 35);
    ctx.save();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63463619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档