inverted_index
inverted_index

Reputation: 2437

Pass data as a js variable to highchart

I have a associate php array (field_data) and i wanna pass it to JS using json_encode function: $jArray = json_encode($field_data); I'm recieving the array in JS: var irbrowser = <?php echo $jArray; ?>; But in js when i wanna set the value in jArray to pie chart data, It does not render anything.

 <?php
        $jArray = json_encode($field_data);
?>

<div dir="ltr" id="container"> </div>

<script>
    var irbrowser = <?php echo $jArray; ?>;

    $(function () {
    $('#container').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: 'Browser market shares'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            name: "Brands",
            colorByPoint: true,
            data: [{
                name: "IE",
                y: irbrowser.IE
            }, {
                name: "Chrome",
                y: irbrowser.Chrome,
                sliced: true,
                selected: true
            }, {
                name: "Proprietary or Undetectable",
                y: irbrowser.Other
            }]
        }]
    });
});

 </script>

Upvotes: 1

Views: 909

Answers (2)

inverted_index
inverted_index

Reputation: 2437

I found the problem. It should be converted to Float. so i use parseFloate(irbrowser) function. And set the variables to y. That code

series: [{
        name: "Brands",
        colorByPoint: true,
        data: [{
            name: "IE",
            y: parseFloat(irbrowser.IE)
        }, {
            name: "Chrome",
            y: parseFloat(irbrowser.Chrome),
            sliced: true,
            selected: true
        }, {
            name: "Proprietary or Undetectable",
            y: 0.5
        }]
    }]

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94662

Look at the first few lines of your script

Line 1 you stop the PHP interpreter

Line 2 you attempt a line of PHP ??

Line 3 you start the PHP interpreter

Line 4+ you try to output raw HTML while inside a PHP section

 ?>
        $jArray = json_encode($field_data);
<?php

<div dir="ltr" id="container"> </div>

<script>
    var irbrowser = <?php echo $jArray; ?>;

Try

        $jArray = json_encode($field_data);
?>

<div dir="ltr" id="container"> </div>

<script>
    var irbrowser = <?php echo $jArray; ?>;

Upvotes: 0

Related Questions