javascript - 如何在 ChartJS 中的 Y 轴值末尾添加单位?

标签 javascript jquery html charts chart.js

我想在 y 轴温度值的末尾添加一个摄氏度单位。我对其他 SO 问题做了一些研究,并尝试编写一个函数将单位附加到值的末尾,但它不起作用。

我尝试复制这个 JSFiddle 示例,但无法让它为我工作,即使我使用的是最新版本的 ChartJs

http://jsfiddle.net/vy0yhd6m/80/

我正在运行 ChartJS 2.5.0,有人可以帮助我如何执行此操作吗?

JS

var ctx = document.getElementById("myChart").getContext("2d");

    // Create gradient
    grd = ctx.createLinearGradient(170.000, 600.000, 150.000, 0.000);

    // Add colors
    grd.addColorStop(0.000, 'rgba(0, 255, 0, 1.000)');
    grd.addColorStop(0.200, 'rgba(191, 255, 0, 1.000)');
    grd.addColorStop(0.400, 'rgba(221, 255, 0, 1.000)');
    grd.addColorStop(0.600, 'rgba(255, 229, 0, 1.000)');
    grd.addColorStop(0.800, 'rgba(255, 144, 0, 1.000)');
    grd.addColorStop(1.000, 'rgba(255, 50, 0, 1.000)');




    var data = {
        labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"],
        datasets: [
            {
                label: "My First dataset",
                lineTension: 0.1,
                backgroundColor: grd,
                borderCapStyle: 'butt',
                borderDash: [],
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                pointBorderColor: "rgba(75,192,192,1)",
                pointBackgroundColor: "#fff",
                pointBorderWidth: 1,
                pointHoverRadius: 5,
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                pointHoverBorderColor: "rgba(220,220,220,1)",
                pointHoverBorderWidth: 2,
                pointRadius: 1,
                pointHitRadius: 10,
                data: [100, 250, 250, 400, 400, 400, 500, 700, 900, 1000, 1000, 1300, 1300, 1100, 900, 700, 500, 300, 100, 0],
                spanGaps: false,
            }
        ]
    };


    var myChart = new Chart(ctx, {
        type: 'line',
        data: data,
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero:true
                    },
                    scaleLabel:
                        function(label){return  ' $' + label.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");},
                    gridLines: {
                        display: false
                    },
                }],
                xAxes: [{
                    display: false,
                    gridLines: {
                        display: false
                    }
                }]
            },

        },
    });

HTML

<canvas id="myChart" width="400" height="400"></canvas>

最佳答案

我能够弄清楚这一点,我需要在我的刻度对象下添加一个回调,如下所示

                    ticks: {
                        beginAtZero:true,
                        callback: function(value, index, values) {
                                return value + '°';
                        }
                    },

希望这对遇到类似问题的人有所帮助。

关于javascript - 如何在 ChartJS 中的 Y 轴值末尾添加单位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43261885/

相关文章:

javascript - 具有透明背景的 Google 自定义搜索

javascript - 强制客户端重新加载我的bundle.js

javascript - 是否可以检查用户是否启用了 JS 声音?

javascript - ie9 上此 css3 动画的 jQuery 回退

javascript - 计算只读输入的动态值总和。 Jquery 和 AJAX

html - 用乌尔都语制作网站

javascript - 无法永远卸载nodejs

javascript - Zepto.js 只能用于移动网络应用程序还是可以像 jQuery 一样用于桌面应用程序?

jQuery Safari/Chrome 与可拖动包含属性不兼容

JQuery 解决方案将特定标题从一个项目复制到 div 中的另一个项目