简体   繁体   中英

Chart.js, PHP and radar chart labels

I want to create dynamic graphics directly linked to a PostgreSQL DB.

For the moment, I succeeded on bar diagrams, but it is complicated on other types of diagram including the radar.

My goal is to get an IRIS (iri_code), a profile on 3 relative variables (txact, txchom, pop_txevol) as in the image below

what I want

First of all, here is my PHP (data_radar.php)

<?php

$dbconn = pg_connect("host='' dbname='' user='' password=''")
or die('Erreur de connexion'.pg_last_error());

$query = "SELECT iri_code, iri_pop_txevol, iri_txact, iri_txchom FROM demo_geo.demo_iris_view WHERE iri_code = '352380801'";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());

$array = array();
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    $array[] = $row;
}

$data=json_encode($array);
echo $data;

pg_free_result($result);

pg_close($dbconn);

?>

It works, here is the json output

[{"iri_code":"352380801","iri_pop_txevol":"3.1","iri_txact":"69.5","iri_txchom":"9.8"}]

But I don't understand how to set the graphic on the JS part. Is the structure of the json good?

$(document).ready(function() {

    $.ajax({
        url : "http://localhost/test_a/data_radar.php",
        type : "GET",
        dataType: 'json',
        success : function(data){
            console.log(data);

            var irisA = [];
            var txact = [];
            var txchom = [];
            var txevol = [];

            for(var i in data) {
                irisA.push(data[i].iri_code);
                txact.push(data[i].iri_txact);
                txchom.push(data[i].iri_txchom);
                txevol.push(data[i].iri_pop_txevol);
            }

            var ctx1 = $("#canvas-radar");

            var data1 = {
                labels : [txact, txchom, txevol],
                datasets : [
                    {
                        label : "IRIS",
                        data : irisA,
                        backgroundColor: "rgba(179,181,198,0.2)",
                        borderColor: "rgba(179,181,198,1)",
                        pointBackgroundColor: "rgba(179,181,198,1)",
                        pointBorderColor: "#fff",
                        pointHoverBackgroundColor: "#fff",
                        pointHoverBorderColor: "rgba(179,181,198,1)"
                    }
                ]
            };

            var options = {
                title : {
                    display : true,
                    position : "top",
                    text : "Radar test",
                    fontSize : 14,
                    fontColor : "#111"
                },
                legend : {
                    display : true,
                    position : "bottom"
                }
            };


            var chart1 = new Chart( ctx1, {
                type : "radar",
                data : data1,
                options : options
            });

        },
        error : function(data) {
            console.log(data);
        }
    });

});

Here is what it gives

radar chart bug

I have searched forums but I confess that I am not yet comfortable, if someone can enlighten me to accelerate my learning it would be very very nice

Thank you in advance and good day !

Here's how you could accomplish that ...

 let json = [{ "iri_code": "352380801", "iri_pop_txevol": "3.1", "iri_txact": "69.5", "iri_txchom": "9.8" }, { "iri_code": "352380703", "iri_pop_txevol": "23.0", "iri_txact": "15.3", "iri_txchom": "29.8" }]; let label = []; let data = []; // generate label and data dynamically json.forEach(e => { label.push(e.iri_code); data.push([+e.iri_pop_txevol, +e.iri_txact, +e.iri_txchom]); }); let ctx = document.querySelector('#canvas').getContext('2d'); let chart = new Chart(ctx, { type: 'radar', data: { labels: ['txevol', 'txact', 'txchom'], datasets: [{ label: label[0], data: data[0], backgroundColor: 'rgba(0,119,204,0.2)', borderColor: 'rgba(0,119,204, 0.5)', borderWidth: 1, pointBackgroundColor: 'rgba(0, 0, 0, 0.5)' }, { label: label[1], data: data[1], backgroundColor: 'rgba(255, 0, 0 ,0.15)', borderColor: 'rgba(255, 0, 0 ,0.45)', borderWidth: 1, pointBackgroundColor: 'rgba(0, 0, 0, 0.5)' }] }, options: { title: { display: true, position: "top", text: "Radar test", fontSize: 14, fontColor: "#111" }, legend: { display: true, position: "bottom" }, scale: { pointLabels: { fontSize: 11 } } } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> <canvas id="canvas"></canvas> 

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