duanshan1977 2017-04-25 12:06
浏览 45
已采纳

Chart.js,PHP和雷达图表标签

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 !

  • 写回答

1条回答 默认 最新

  • douyi8315 2017-04-27 09:51
    关注

    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>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度