javascript - Chartjs 雷达索引标签

标签 javascript jquery css chart.js radar-chart

我正在使用 chartjs 绘制雷达图。

该值显示在悬停在图表的点上,但我想始终显示该值。我需要更改 View 以在打印页面时显示数据。

这是我当前的图表。悬停时显示标签

This is my current chart. The label is shown on hover

我想一直显示值,如下图所示 I want to show the value always, like in the following image

最佳答案

以下答案仅适用于 Chart.js v2。
如果您想要 v1 解决方案,请检查 pritishvaidya's .


您想使用图表选项的 animation 属性:

options : {
    animation: {
        duration: 500,
        onComplete: function () {
            // The code here will be executed at the end of the animation 
            // (after 500ms here)

            // You can get the canvas context using the following :
            var ctx = this.chart.ctx;
            // `this` being the chart instance
        }
    }
}

现在你想在它上面添加点的值,这可以使用数据模型来完成,你可以在数据集属性中找到它:

onComplete: function() {
    // You get the canvas context, to help you writing what you want
    var ctx = this.chart.ctx;

    // Here you set the context with what you need (font, size, color ...)
    ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontFamily, 'normal', Chart.defaults.global.defaultFontFamily);
    ctx.textAlign = 'center';
    ctx.fillStyle = 'black';

    // For every dataset ...
    this.data.datasets.forEach(function(dataset) {

        // For every data in the dataset ...
        for (var i = 0; i < dataset.data.length; i++) {

            // We get its model
            var model = dataset._meta[0].data[i]._model;

            // And write the data above it
            ctx.fillText(dataset.data[i], model.x, model.y - 2);
            // You can edit the last two arguments according to what you need
        }
    });
}

按照上面代码的结果,你可以找到on this jsFiddle :

enter image description here

关于javascript - Chartjs 雷达索引标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39974482/

相关文章:

javascript动态填充关联数组并获取值

javascript - .bind 的替代方案

javascript - 模糊整个页面 - 鼠标悬停显示内部非模糊的圆圈

jquery - History.pushstate 不能返回多次

javascript - 如何给跳跃系统添加限制?

JavaFX 选择框文本颜色

javascript - 如何在 JavaScript 中使用对象数组

javascript - ASP MVC 基本 AJAX Json 请求返回 null

javascript - Animate.css 与 Bootstrap 模态的交互,关闭动画的问题

css - 如何使用自动供应商前缀将 SCSS 转换为 CSS?