Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bar chart not displayed in ASP.NET MVC using AJAX call #978

Closed
etairi opened this issue Mar 8, 2015 · 4 comments
Closed

Bar chart not displayed in ASP.NET MVC using AJAX call #978

etairi opened this issue Mar 8, 2015 · 4 comments

Comments

@etairi
Copy link

etairi commented Mar 8, 2015

I have the following code segment:

(function () {
    var randomScalingFactor = function () { return Math.round(Math.random() * 100) };

    var barData = {
        labels: [],
        datasets: [
            {
                label: "Test",
                fillColor: "rgba(151,187,205,0.5)",
                strokeColor: "rgba(151,187,205,0.8)",
                highlightFill: "rgba(151,187,205,0.75)",
                highlightStroke: "rgba(151,187,205,1)",
                data: []
            }
        ]
    };

    $.ajax({
        url: "/GetTeams",
        dataType: "json",
        success: function (data) {
            console.log("Length is: " + data.length);
            for (i = 0; i < data.length; i++) {
                barData.labels.push(data[i].FullName);
                barData.datasets[0].data.push(randomScalingFactor());
            }
            console.log(barData.labels);
            console.log(barData.datasets[0].data);
        }
    });

    var ctx = document.getElementById("canvas").getContext("2d");
    window.myBar = new Chart(ctx).Bar(barData, {
        responsive: true
    });

}());

When I check the console, for the arrays, I get the following values:

"Length is: 10"
Array [ "Team #1", "Team #2", "Team #3", "Team #4", "Team #5", "Team #6", "Team #7", "Team #8", "Team #9", "Team #10" ]
Array [ 74, 53, 38, 37, 92, 47, 40, 5, 22, 16 ]

This seems all fine to me. But, the graph is not displayed. It shows just this: http://prntscr.com/6edcyt. But if I enter manually the labels and the values, it works all fine. But, when I use an AJAX call it is not working. Any idea how to solve it?

@etairi
Copy link
Author

etairi commented Mar 13, 2015

Including something like the line below in the success part of the AJAX call should solve the problem:

window.myBar.update();

@tiesont
Copy link
Contributor

tiesont commented Mar 13, 2015

$.ajax (and by extension $.get, $.post, and $.load) are non-blocking operations, so you're creating the chart before the data request has completed. Use the .done handler (or the success callback, like you're already using) to create the chart when you've received a 200 response:

    $.ajax({
        url: "/GetTeams",
        dataType: "json",
        success: function (data) {
            console.log("Length is: " + data.length);
            for (i = 0; i < data.length; i++) {
                barData.labels.push(data[i].FullName);
                barData.datasets[0].data.push(randomScalingFactor());
            }
            console.log(barData.labels);
            console.log(barData.datasets[0].data);

            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx).Bar(barData, {
                responsive: true
            });
        }
    });

Calling update() would work, too, since the dataset would then be populated.

It's worth noting that this isn't ASP.NET MVC specific. The same thing would happen with RoR, PHP, etc.

@fulldecent
Copy link
Contributor

@etairi Could you please let us know if this is working for you?

@etairi
Copy link
Author

etairi commented Mar 17, 2015

Yes, it is working. I already noticed the problem and fixed it. Also thanks to @tiesont for the good answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants