Click here to Skip to main content
14,983,551 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have been trying to prepare a graph using the values that I have but somehow all I could do is just getting data to JSON.

What I have tried:

print_r(json_encode($graph, JSON_NUMERIC_CHECK)); // This is in my controller

Below code is in my view page.

JavaScript
$(document).ready(function (res) {
    var chart = new CanvasJS.Chart("chartContainer", {
        title:{
            text: "Financial Year Wise Data"              
        },
        data: [ {
            type: "line",
            dataPoints: res
        } ]
    });

HTML
<div id="chartContainer" style="height: 300px; width: 100%"></div>
Posted
Updated 17-Feb-17 5:10am
v2

1 solution

The function you pass to $(document).ready(...) will receive a single parameter, which is a reference to the jQuery object:

When $.noConflict() is used to avoid namespace conflicts, the $ shortcut is no longer available. However, the .ready() handler is passed a reference to the jQuery object that called the method. This allows the handler to use a jQuery object, for example as $, without knowing its aliased name.

This object is clearly not a valid set of data points for your chart.

You will either need to load the chart data using an AJAX request, or echo the data into a Javascript variable and use that to initialize your chart.
   
Comments
Member 13007173 17-Feb-17 12:01pm
   
I just changed the above code to this.

$("#displayChart").click(function(res) {
e.preventDefault();
$.ajax({
type: "JSON",
url: "http://localhost:8080/supply_chain/historical_analysis",
success: function(res) {
console.log(res);
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Financial Year Wise Data"
},
data: [{
type: "line",
dataPoints: res
}]

});
},
error: function(err) {
console.log(err);
}
});
});
Nelek 17-Feb-17 15:52pm
   
Then you should use the "improve question" and change or add the code above... it will improve readability with proper format and indentation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900