1

I am working on ordering and displaying users with most activities . received from server, i want to display all data as a graph using charts.js. The server data i.e. "clara , 4 activities" , "joe, 7 activities". etc...

1 Answer 1

1

Hope this helps

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

var data = [4, 7];
var labels =  ["clara", "joe"]    

var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: labels,
        datasets: [{
            label: 'Activities',
            data: data,
        }]
    },
});

Using ajax

$.ajax({
    url: "Your url",
    success: function (result) {
        var data = [];
        var myChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: result.labels,
                datasets: [{
                        label: 'Activities',
                        data: result.data,
                    }]
            },
        });
    }
});

You have to pass a dataset like this from backend,

{
    "chartData": {
        "labels": [
            "clara",
            "joe"
        ],
        "data": [
            4,
            7
        ]
    }
}

Also here is a nice Tutorial about this.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you but i try what you send abd it works , but now the "var data = [4,7] " : is what i want dynamically from server
For that you may have to call $.ajax and call var myChart = new Chart(ctx,.. inside success method.
send me a screen please, one of how you transform data in JSON and another to show me how you call it in the chart
The tutorial I linked have a complete description on how to do that. Think it will more descriptive and helpful
Sorry I was busy earlier. Added a solution. Please add desired url instead of Your url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.