简体   繁体   中英

Passing data from $ajax call to highcharts

I have an ajax call

$(function () {
    $.ajax({
        url: '../../getdaily.php',
        type:'POST',
        dataType: '',
        success: function(output_string){
           console.log(output_string);
        },
        error: function (xhr, ajaxOptions, thrownError){
            console.log(xhr.statusText);
            console.log(thrownError);
        }
    });
});

and the console.log will output...

[{"name":"Test-Cases","y":118},{"name":"White-Box","y":43},{"name":"Priority","y":44}]

I can easily paste this into my data : for highcharts and get the piechart I am looking for like so

...
series: [{
        type: 'pie',
        name: 'Browser share',
        data: [{"name":"Test-Cases","y":118},{"name":"White-Box","y":43},{"name":"Priority","y":44}]
    }]
...

在此处输入图片说明

My question is how do I get the output_string into the Highcharts data : <here> . I have tried various ways of passing in as a variable and starting to spin out on this, not sure why.

The actual code I am using for highcharts and passing into an id...

$('#container').highcharts({
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
    },
    title: {
        text: 'Browser market shares at a specific website, 2014'
    },
    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: [{
        type: 'pie',
        name: 'Browser share',
        data: [{"name":"Test-Cases","y":118},{"name":"White-Box","y":43},{"name":"Priority","y":44}]
    }]
});

And to cover the php file, here is a partial of the end

...
$mysqli->real_query("SELECT priority FROM daily");
$rows2 = array();
$numb2 = 0;
$res2 = $mysqli->use_result();
$rows2['name'] = 'Priority';
while($r2 = $res2->fetch_assoc()) {
   $numb2+= $r2['priority'];
   $rows2['y'] = $numb2;
}

$result = array();
array_push($result,$rows);
array_push($result,$rows1);
array_push($result,$rows2);

echo json_encode($result);

Just wrap your highcharts in a function, pass the ajax response as the function's argument and load the data (in argument) to your high charts , as:

function my_chart(response) {
    $('#container').highcharts({
        ...
        ...
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: response
        }]
    });
}

and call your function in ajax response, as:

    $(function () {
        $.ajax({
            url: '../../getdaily.php',
            type:'POST',
            dataType: '',
            success: function(output_string){
               //call my_chart function
               var parsed_response = jQuery.parseJSON(output_string);
               my_chart(parsed_response);
            },
            error: function (xhr, ajaxOptions, thrownError){
                console.log(xhr.statusText);
                console.log(thrownError);
            }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM