简体   繁体   中英

How to apply dollar sign in Y- axis in chart js?

I am working on chart js, I want to apply doller sign in Y axis, I tried options for that but it is not working, can anyone please help me for that, here is my code

function earning_one_time_purchase() {
        var monthsData = {
            labels: <?php echo $t->one_time_purchase_month_chart; ?>,
            datasets: [
                {
                    fillColor: "<?php echo $graph_color_code; ?>", //"rgba(172,194,132,0.4)",
                    strokeColor: "<?php echo $graph_line_color_code; ?>",
                    pointColor: "#fff",
                    pointStrokeColor: "#9DB86D",
                    data: <?php echo $t->one_time_purchase_amount_chart; ?>
                } 
            ],
        options: {
        scales: {
            yAxes: [{
                ticks: {
                // Include a dollar sign in the ticks
                callback: function(value, index, values) {
                    alert(value);
                    return '$' + value;
                }
                }
            }]   
            }
        }
        };
        var months = document.getElementById("eChart_1").getContext("2d");
        new Chart(months).Line(monthsData);
    }

If you are using version 1.x of Chart.js, you can customize the ticks using the scaleLabel key.

var options = {
   scaleLabel: function(label){return  '$' + label.value}
};

Note that when creating the chart, you should pass the options object as the second argument instead of including it in your monthsData .

 const labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']; const initialData = [{'x':'Apr', 'y':40},{'x':'July', 'y':70},{'x':'Dec', 'y':120}]; const filledMonths = initialData.map((month) => month.x); const dataSet = labels.map(month => { const indexOfFilledData = filledMonths.indexOf(month); if( indexOfFilledData !== -1) return initialData[indexOfFilledData].y; return 0; }); var monthsData = { labels: labels, datasets: [ { label: "My First dataset", fillColor: "rgba(220,220,220,0.2)", strokeColor: "rgba(220,220,220,1)", pointColor: "rgba(220,220,220,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(220,220,220,1)", data: dataSet } ] }; var options = { scaleLabel: function(label){return '$' + label.value} }; var ctx = document.getElementById("myChart").getContext("2d"); var chart = new Chart(ctx).Line(monthsData, options); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.1.1/Chart.min.js"></script> <canvas id="myChart" width="300" height="300"></canvas> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM