0

我对 highchart 有疑问。我必须从数据库中输入一些日期,但我无法可视化正确的图表。我向您展示了我的完整代码和我现在拥有的可视化的 img。请帮助我,我在这件事上被阻止了 3 天。

那么我初始化图形并调用ajax函数的js是:

$(function () {
    var cs = document.getElementById('alunni').firstChild.nodeValue;


    var rend = $('#alunni')

    var options = {
        chart: {
            plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                renderTo: rend[0]
            },
            title: {
                text: 'Numero di studenti iscritti per ogni anno di corso'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Percentuale di alunni: ',
            }]
        };

        $.ajax({
            type: 'POST',
            url: 'alunnixclasse.php',
            async: false,
            data: {'cs': cs},
            dataType: 'json',
            success: function(data) {
                options.series[0].data = data;
            },
            error: function(data){ 
                alert("Chiamata fallita, si prega di riprovare...");
            }
        });

        console.log(options.series);

        var chart = new Highcharts.Chart(options);
});

这是我用来记录日期的 php:

<?php

    include "connect.php";

    $codscu = $_POST['cs'];

    $query = "SELECT * FROM alunni WHERE alunni.codscu = $codscu ";
    $query2 = "SELECT total.TotAlun FROM total WHERE total.codscu = $codscu ";

    $risultato = mysql_query($query);
    $risultato2 = mysql_query($query2);

    $data = "";

    $elem = mysql_fetch_array($risultato);
    $tot = mysql_fetch_array($risultato2);

    for ($i = 0; $i <5; $i++) {
        $anno = "";
        switch($i) {
            case 0: { $anno = "Primo anno"; $value = ($elem['anno_1'] * 100) / $tot['TotAlun']; break; }
            case 1: { $anno = "Secondo anno"; $value = ($elem['anno_2'] * 100) / $tot['TotAlun']; break; }
            case 2: { $anno = "Terzo anno"; $value = ($elem['anno_3'] * 100) / $tot['TotAlun']; break; }
            case 3: { $anno = "Quarto anno"; $value = ($elem['anno_4'] * 100) / $tot['TotAlun']; break; }
            case 4: { $anno = "Quinto anno"; $value = ($elem['anno_5'] * 100) / $tot['TotAlun']; break; }
        }

        if ($i != 4) {
            $data = $data."['".$anno."', ".number_format($value, 1,".","")."], ";
        } else {
            $data = $data."['".$anno."', ".number_format($value, 1,".","")."]";
        }
    }

    print json_encode($data);
?>

你能帮我吗...我不知道我的错误在哪里!

4

0 回答 0