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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化