Metalbreath
Metalbreath

Reputation: 119

Chart js PHP and JSON wont diplsay

I am trying to get JSON through PHP. This is successful since the console display the json objects. But the values will not display on the chart.

Is there something wrong with the way I translate JSON?

// Getting PHP - JSON file
$.get('chartBar.php', {type: "bar"}, function(dataBar){
    console.log(dataBar);

//Translate JSON
    var chartjsDataBar = [];
    for (var i = 0; i < dataBar.length; i++) {
    chartjsDataBar.push(dataBar[i].data); 
    };

    var BarChartData = {
        labels : labelTags,
        datasets: [
        {        
        label: "Net Comp",
        fillColor : "#F02626",
        strokeColor : "#F02626",
        pointColor : "#F02626",
        pointStrokeColor : "#fff",
        pointHighlightFill : "#fff",
        pointHighlightStroke : "#F02626",
        data : chartjsDataBar[0]},
        {
        label: "AnalyzerHR",
        fillColor : "#26F041",
        strokeColor : "#26F041",
        pointColor : "#26F041",
        pointStrokeColor : "#fff",
        pointHighlightFill : "#fff",
        pointHighlightStroke : "#26F041",
        data : chartjsDataBar[1]},
        {
        label: "Question Right",
        fillColor : "#20AEFA",
        strokeColor : "#20AEFA",
        pointColor : "#20AEFA",
        pointStrokeColor : "#fff",
        pointHighlightFill : "#fff",
        pointHighlightStroke : "#20AEFA",
        data : chartjsDataBar[2]}

    ]};

If I add the data straight like this data : dataBar it displays but of course the results are all mixed in one line.

PHP :

    <?php

    $array = [
        [  
    "label"=> "Net Comp",
    "color"=> "#F02626",
    "data"=> [0, 6, 7, 8, 9, 3]
], [
    "label"=> "AnalyzerHR",
    "color"=> "#26F041",
    "data"=> [ 0, 4, 3, 5, 7,  2 ]
], [
    "label"=> "Question Right",
    "color"=> "#20AEFA",
    "data"=> [ 0, 3, 1,  3,  3, 1] 
]
    ];

    if($_GET['type'] == "bar"){
        echo json_encode($array);   
    }
?>

Thank you in advance

Upvotes: 1

Views: 90

Answers (1)

camelCase
camelCase

Reputation: 5598

You need to set the $.get to receive json so it knows the dataType coming back.

$.get('chartBar.php', {type: "bar"}, function(dataBar){},'json');

Upvotes: 1

Related Questions