在Javascript中从JSON数据创建的饼图

时间:2018-05-22 10:56:25

标签: javascript php json infragistics

我是PHP和JavaScript的新手,我需要使用将从URL获取的JSON数据创建一个饼图。 JSON数据是:

[
  {"Domain":"Artificial Intelligence","Count":"46"}, 
  {"Domain":"Data Architecture","Count":"21"}, 
  {"Domain":"Data Science","Count":"50"}, 
]

代码:

<!DOCTYPE html>   
<html>
<head>
<title></title>
<link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet" />
<link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/structure/infragistics.css" rel="stylesheet" />

<script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"> 
</script>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
<script src="http://cdn- na.infragistics.com/igniteui/2018.1/latest/js/infragistics.core.js"></script>
<script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.dv.js"></script>

</head>
<body>
<?php
 # Reading JSN Data from URLS
 $jsn_data =    
 file_get_contents("http://localhost:9080/Badges/reporting/badge_json.php");
  <div id="chart"></div>
  <script type="text/javascript">

   $("#chart").igPieChart({
            width: "435px",
            height: "435px",
            dataSource: result, 
            dataValue: "count",
            dataLabel: "Domain",
            labelsPosition: "bestFit"
        });

      });
    <script>

 </body>
 s</html>

但是这段代码不起作用。请告诉我这是怎么做的?

2 个答案:

答案 0 :(得分:1)

dataLabel:&#34; Badge_SubDomain&#34;不存在于您的数据源中。

也许尝试类似......

   $("#chart").igPieChart({
            width: "435px",
            height: "435px",
            dataSource: result, 
            dataValue: "count",
            dataLabel: "Domain",
            labelsPosition: "bestFit"
        });
   });

答案 1 :(得分:1)

解决这些问题:

  1. JSON属性计数字符串,而不是整数

  2. 代码已损坏,需要修复

  3. 变量结果未在<script>标记

  4. 中的任何位置定义
  5. JavaScript代码段在<?php/>标记内,将其移到外面

  6. 这里是固定代码:

    <强>的index.php:

    <!DOCTYPE html>   
    <html>
        <head>
            <title></title>
            <link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet"></script>
            <link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/structure/infragistics.css" rel="stylesheet"></script>
    
            <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
            <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
            <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script>
            <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.core.js"></script>
            <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.dv.js"></script>
        </head>
        <body>
            <div id="chart"></div>
            <script type="text/javascript">
                $.ajax({type:"GET", url: "badge_json.php", success: function(data) {
                    $("#chart").igPieChart({
                        width: "435px",
                        height: "435px",
                        dataSource: data, 
                        dataValue: "Count",
                        dataLabel: "Domain",
                        labelsPosition: "bestFit"
                    });
                }});
            </script>
        </body>
    </html>
    

    badge_json.php:

    <?php
        header('Content-type: application/json');
    
        // your data goes here
        $data = array(
            ['Domain' => 'Artificial Intelligence', 'Count' => 46],
            ['Domain' => 'Data Architecture', 'Count' => 21],
            ['Domain' => 'Data Science', 'Count' => 50]
        );
    
        echo json_encode($data);
    ?>