1

This told me how to add data values to chart JS - Adding data values to chart JS

Now I want to add commas to these values e.g 5,000 6,000 instead of 5000 6000

JSFiddle for data values code

            var chartData = {
                labels: ["January", "February", "March", "April", "May", "June"],
                datasets: [
                    {
                        fillColor: "#79D1CF",
                        strokeColor: "#79D1CF",
                        data: [60, 80, 81, 56, 55, 40]
                    }
                ]
            };

            var ctx = document.getElementById("myChart1").getContext("2d");
            var myLine = new Chart(ctx).Line(chartData, {
                showTooltips: false,
                onAnimationComplete: function () {

                    var ctx = this.chart.ctx;
                    ctx.font = this.scale.font;
                    ctx.fillStyle = this.scale.textColor
                    ctx.textAlign = "center";
                    ctx.textBaseline = "bottom";

                    this.datasets.forEach(function (dataset) {
                        dataset.points.forEach(function (points) {
                            ctx.fillText(points.value, points.x, points.y - 10);
                        });
                    })
                }
            });

            var ctx = document.getElementById("myChart2").getContext("2d");
            var myBar = new Chart(ctx).Bar(chartData, {
                showTooltips: false,
                onAnimationComplete: function () {

                    var ctx = this.chart.ctx;
                    ctx.font = this.scale.font;
                    ctx.fillStyle = this.scale.textColor
                    ctx.textAlign = "center";
                    ctx.textBaseline = "bottom";

                    this.datasets.forEach(function (dataset) {
                        dataset.bars.forEach(function (bar) {
                            ctx.fillText(bar.value, bar.x, bar.y - 5);
                        });
                    })
                }
            });
Community
  • 1
  • 1
Tom Rudge
  • 3,188
  • 8
  • 52
  • 94
  • Unfortunately there's no simple cross-browser solution for this. Check out this answer: http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Matt O'Connell Feb 12 '16 at 15:41
  • Yes I have done this for the Y values - not in the code example. But I want to do the same for the data values that appear above the line/bars also. I used this: var numberWithCommas = function(x) {return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");}; with this for the Y axis - scaleLabel: "<%=numberWithCommas(value)%>", – Tom Rudge Feb 12 '16 at 15:55
  • Can you elaborate on what's going wrong? Are you getting an error or is it just not working? – DanM7 Feb 12 '16 at 16:18
  • If the values you're referring to in the code are `bar.value` and `points.value`, you can wrap those with your `numberWithCommas` function – Matt O'Connell Feb 12 '16 at 16:51
  • Hi @MattO'Connell, thanks for your input so far - I've updated my [Fiddle](http://jsfiddle.net/cmzvgdm6/) - I want commas in the data that resides above the bars, like I've done for the Y axis – Tom Rudge Feb 16 '16 at 10:06
  • @DanM7 http://jsfiddle.net/cmzvgdm6/ if you could help that would be brill! thanks – Tom Rudge Feb 16 '16 at 10:31
  • 2
    Add `bar.value = numberWithCommas(bar.value);` within the `this.datasets.forEach` function on line 36 – Matt O'Connell Feb 16 '16 at 16:57
  • @MattO'Connell can you answer this so I can accept it? This works a treat, but it annoys me that it was this easy - I'm such an amateur! – Tom Rudge Feb 17 '16 at 12:16

1 Answers1

1

Unfortunately, there's no built-in cross browser solution for this answer in JavaScript:

How to print a number with commas as thousands separators in JavaScript

You can use the numbersWithCommas you included in your fiddle:

var numberWithCommas = function(x) {
    return x
        .toString()
        .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};

Just include bar.value = numberWithCommas(bar.value); within the this.datasets.forEach function

This will format the numbers with commas prior to appending them to the chart.

Community
  • 1
  • 1
Matt O'Connell
  • 287
  • 3
  • 14