1

I need to get graph generated from Mysql database with help of PHP. I got nice application from (http://canvasjs.com). So, I created my own JSON format in variable element from PHP SESSION. It gies me no error when debugged, but no result.

Line number 34 - 37 gives me graph if I don't comment them. When comment them and replace with my own JSON which is variable element and does not give me graph. Gives me blank page.

I am new to JSON. Help me out with this.

Following are my codes.

var array = <?php echo json_encode($_SESSION["r_array"]); ?>;

var count = 0;
var element = '';
var title = 'Malware lab, TCRC, CTA';

for( var i in array) {

    if ( count == 0 ) {
        element += ‘{y: ‘+array[i]+’,’+’ indexLabel: “‘+i+'”}';
    } else {
       element += ‘, {y: ‘+array[i]+’,’+’ indexLabel: “‘+i+'”}';
    }
    count++;
 }

 var chart = new CanvasJS.Chart(“chartContainer”,
 {
     title: {
        text: title
     },
 data: [
        { type: type,

            dataPoints: [
                   /*
                   34 {y: 2, indexLabel: “Kaka'”},
                   35 {y: 3, indexLabel: “Blackberry”},
                   36 {y: 1, indexLabel: “Windows Phone”},
                   37 {y: 5, indexLabel: “Others”},*/

                   element,
            ]
    }
   ]
});
chart.render();

Dumped array of $_SESSION["r_array"] as following
Array (
    [Trojan] => 1
    [Malware] => 3
    [Backdoor] => 6
    [Virus] => 2
    [Bot] => 5
    [Rootkit] => 7
    [Worm] => 5
)
2
  • Can you show the dump of $_SESSION["r_array"] . Commented May 20, 2015 at 6:14
  • try this --> var array = '<?php echo json_encode($_SESSION["r_array"]); ?>'; Commented May 20, 2015 at 6:20

2 Answers 2

1

element must be an array, you are trying to create it as a string(with syntax errors)

var array = <? php echo json_encode($_SESSION["r_array"]); ?> ;

var count = 0;
var element = [];
var title = 'Malware lab, TCRC, CTA';

for (var i in array) {
    element.push({
        y: array[i],
        indexLabel: i
    })
    count++;
}

var chart = new CanvasJS.Chart("chartContainer", {
    title: {
        text: title
    },
    data: [{
        type: type,

        dataPoints: element
    }]
});
chart.render();

Demo: Fiddle

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

2 Comments

Thanx it worked... too So array in javascript is equal to JSON?
@user3181614 json is an (almost) subset of javascript. The j in json stands for javascript. It was always the intention that you can just eval() a json string in javascript.
0

Yo need to pass array in correct Way

Use another variable datapoints = [];

and the push object into that array

    var datapoints = [];

    for( var i in array) {
            datapoints.push({ y : array[i] , indexLabel : i});
     }

and then use it as below

     var chart = new CanvasJS.Chart(“chartContainer”,
     {
         title: {
            text: title
         },
     data: [
            { type: type,

                dataPoints: datapoints
        }
       ]
    });
    chart.render();

Comments

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.