echart和php實現動態擷取資料

來源:互聯網
上載者:User
本文主要和大家分享echart和php動態擷取資料的實現方法,要是以下代碼執行不起來,請參考注釋部分,希望能協助到大家。

html部分的代碼

<html>  <head>      <title>bingtu.html</title>      <meta charset="utf-8" />      <script src="https://cdn.bootcss.com/echarts/4.0.2/echarts-en.js"></script>      <script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>  </head>  </head>  <body>      <p id="main" style="border:1px solid red;height:400px;"></p>  </body></html>

js部分的代碼

<script>   var chart = echarts.init(document.getElementById('main'));   chart.setOption({     title:{       text : '非同步載入',     },     tooltip: {},     legend:{       data:['銷量'],     },     xAxis:{       data:[],     },     yAxis:{},     series:[{       name:'銷量',       type:'bar',       data:[]     }]   });   var categories = [] , data = [];    /*注意一下 {:url()} 這一塊填寫你要請求的地址  你可以是http://.....com  也可以是你項目中某塊檔案 Index/index*/   $.post("{:url('ticket/getSellRecord',['type'=>'week'])}").done(function (info) {    // 填入資料    /*特別注意這裡 如果info是json對象就不用JSON.parse了 如果是json字串就要轉成對象*/    console.log(info);     info = JSON.parse(info);    for(var i = 0 ; i < info.length ; i++)    {      categories.push(info[i]['seller_id']);      data.push(info[i]['amount']);    }    chart.setOption({        xAxis: {            data: categories        },        series: [{            // 根據名字對應到相應的系列            name: '銷量',            data: data        }]    });});</script>

php部分代碼

<?php    /*你可以設個定值去測試 , 動態就是你從資料庫去擷取  這部分主要的是格式*/    /*返回的資料格式為json 在js中列印一下傳過去的資料是json字串還是json對象 如果是json字串一定要轉成json對象  可用JSON.parse */    $data = [["seller_id"=>2,"amount"=>"2000"],["seller_id"=>8,"amount"=>"501"]];    return json_encode($data);?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.