使用来自网址

时间:2018-05-22 12:42:22

标签: javascript php json ajax

我是PHP和Javascript的新手。我需要从JSON创建pi图表,该图表应该从URL中读取。

我试图创建pi图表直接给出json数据并且它有效。 我的代码如下: -

<!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>
<style type="text/css">
     #chart { position: relative;
        float: left;
        margin-right: 10px;
        margin-bottom: 10px;
}   
</style>
<div id="chart"></div>
<script type="text/javascript">
$(function () {
        var data = [
            { "Badge_SubDomain": "Data Architecture", "count": "1333"},
            { "Badge_SubDomain": "Data Integration", "count": "849"},
            { "Badge_SubDomain": "Data Platform", "count": "250"},
                 ];

        $("#chart").igPieChart({
            width: "435px",
            height: "435px",
            dataSource: data, 
            dataValue: "count",
            dataLabel: "Badge_SubDomain",
            labelsPosition: "bestFit" });
});
</script>
</body>
</html>

来自URL的JSON数据是: -

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

我需要从URL读取数据而不是直接给出值。 &#39;伯爵&#39;在JSON数据中是字符串格式,它需要更改浮动格式以绘制pi图表。我怎样才能改变它的代码?

1 个答案:

答案 0 :(得分:-1)

$.get("url", function(data, status){ 
  

var data = [           {&#34; Domain&#34;:&#34; Artificial Intelligence&#34;,&#34; Count&#34;:&#34; 46&#34;},           {&#34; Domain&#34;:&#34; Data Architecture&#34;,&#34; Count&#34;:&#34; 21&#34;},           {&#34; Domain&#34;:&#34; Data Science&#34;,&#34; Count&#34;:&#34; 50&#34;},           ]

finalArray = data.map(data => parseFloat(data.Count));

}); 结果= [46,21,50]