How can I add a unit to the end of my Y Axis values in ChartJS?

时间:2017-04-06 17:20:39

标签: javascript jquery html charts chart.js

I would like to add a Celsius unit to the end of my temperatures values on the y axis. I did some research on other SO questions and attempted to write a function to append the unit to the end of the value but it did not work.

I tried replicating this JSFiddle example but could not get it to work for me even though I'm on the latest version of ChartJs

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

I am running ChartJS 2.5.0, could someone assist me on how to do this?

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>

3 个答案:

答案 0 :(得分:2)

我能够解决这个问题,我需要在我的ticks对象下添加回调,如此

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

我希望这可以帮助任何有类似问题的人。

答案 1 :(得分:1)

我在Angular 7中进行了尝试,对我有用:

options: {
  tooltips: {
    callbacks: {
       label: (tooltipItems, data) => {
            return data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index] + ' GB';
            }
        },
  }

答案 2 :(得分:0)

    var data = {
        labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"],
    datasets: [
        {
            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 options = {
        animation: false,
        scaleLabel:
        function(label){return label.value.toString() +' °C';}
        
    };

    //Get the context of the canvas element we want to select
    var ctx = document.getElementById("myChart").getContext("2d");
    /*************************************************************************/
    var myLineChart = new Chart(ctx).Line(data, options);
    var grd = ctx.createLinearGradient(170.000, 600.000, 150.000, 0.000);
        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)');
<script src="http://www.chartjs.org/assets/Chart.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>