通過php動態傳數據到highcharts


1:在平時工作中,在對數據進行展示的時候,是直接通過后台提供的接口來獲取json串,用來展示。今天別人問怎么在本地演示一下請求的動態數據。

2:在本地搭建環境,我用的WampServer,下載地址:http://221.181.72.84/softdl.360tpcdn.com/WampServer/WampServer_2.5.exe,瀏覽器打開localhost,文件存放在wamp/www目錄下

3:php代碼,沒有寫與數據庫實時請求的過程。

1 <?php
2     $b = array(
3             array('name'=>'北京', 'y'=>20.2),
4             array('name'=>'上海', 'y'=>9.6),
5             array('name'=>'武漢', 'y'=>16.6),
6     );
7     $data = json_encode($b);
8     echo($data);
9 ?>

4:html文件

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3     <head>
 4         <meta charset="utf-8">  <meta name="viewport" content="width=device-width, initial-scale=1">
 5         <meta http-equiv="x-ua-compatible" content="ie=edge">
 6         <link rel="icon" href="https://static.jianshukeji.com/hcode/images/favicon.ico">
 7         <style>
 8         </style>
 9         <script src="https://img.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>
10         <script src="https://img.hcharts.cn/highcharts/highcharts.js"></script>
11         <script src="https://img.hcharts.cn/highcharts/modules/exporting.js"></script>
12         <script src="https://img.hcharts.cn/highcharts-plugins/highcharts-zh_CN.js"></script>
13     </head>
14     <body>
15         <div id="container" style="min-width:400px;height:400px"></div>
16         <script>
17             $(function () {
18                 $.getJSON('http://localhost/index-1.php', function (csv) {
19                     console.log(csv)
20                     $('#container').highcharts({
21                         chart: {
22                             plotBackgroundColor: null,
23                             plotBorderWidth: null,
24                             plotShadow: false
25                         },
26                         title: {
27                             text: ''
28                         },
29                         tooltip: {
30                             pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
31                         },
32                         plotOptions: {
33                             pie: {
34                                 allowPointSelect: true,
35                                 cursor: 'pointer',
36                                 dataLabels: {
37                                     enabled: true,
38                                     color: '#000000',
39                                     connectorColor: '#000000',
40                                     formatter: function() {
41                                         return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
42                                     }
43                                 }
44                             }
45                         },
46                         series: [{
47                             type: 'line',
48                             name: '',
49                             data: csv,
50                         }]
51                     });
52                 });
53         });
54         </script>
55     </body>
56 </html>

5:在這里,引入js文件,當需要對一個相同的json串展示為不同的圖形時,修改series里的type屬性,同時修改highcharts里的數據列參數plotOptions,就可以展示不同的圖形了,highcharts可顯示圖形類型。

php返回數據格式:[{"name":"\u5317\u4eac","y":20.2},{"name":"\u4e0a\u6d77","y":9.6},{"name":"\u6b66\u6c49","y":16.6}],當需要對請求的數據進行處理時,比如只需要其中一部分的數據時,可以在通過get請求數據時,對傳過來的數組進行處理:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM