java代碼實現highchart與資料庫資料結合完整案例分析(一)---餅狀圖

來源:互聯網
上載者:User

標籤:nts   chart   long   charts   head   enabled   cursor   cti   border   

作者原創:轉載請註明出處

       在做項目的過程中,經常會用到統計資料,同時會用到highchart或echart進行資料展示,highchart是外國開發的資料統計圖外掛程式,

echart是我們國家開發的資料統計外掛程式,我比較喜歡highchart的統計外掛程式,在這裡展示的也是highchart外掛程式的應用。

       應用highchart外掛程式並不難,找到官方文檔,copy代碼,就能把表徵圖呈現出來,難的是如何將本機資料庫中的資料與其結合。因此,

在這裡主要分析將資料庫資料和外掛程式結合的過程,我用的是java代碼實現的。下面是實現的比例圖:

 

先展示js代碼

<script type="text/javascript">  $(function () {      var livePercent=document.getElementById("livePercent").value;      var vodPercent=$("#vodPercent").val();      var playCount=$("#playCount").val();     // alert(typeof());     // alert("vodPercent:"+vodPercent+"######livePercent="+livePercent);      $(‘#pieChart_res‘).highcharts({            chart: {                plotBackgroundColor: null,                plotBorderWidth: null,                plotShadow: false            },            title: {                text: ‘播放統計比例圖‘            },            credits: {                text: ‘‘,                href: ‘‘            },            tooltip: {                headerFormat: ‘{series.name}<br>‘,                pointFormat: ‘{point.name}: <b>{point.percentage:.1f}%</b>‘            },            plotOptions: {                pie: {                    allowPointSelect: true,                    cursor: ‘pointer‘,                    dataLabels: {                        enabled: true,                        format:‘{point.description}‘,                        distance:-140                    },                    showInLegend:true                }            },            series: [{                                type: ‘pie‘,                name: ‘該資源訪問量佔比‘,                data: [                        [‘直播訪問量佔比‘,parseFloat(livePercent)],                       {                            name:‘點播訪問量佔比‘,                            y:parseFloat(vodPercent),                             sliced: true,                             selected: true,                             description:‘總播放數‘+playCount                       }                ]            }]        });    });  </script> 

上面的代碼和highchart中API的代碼基本一致,主要的思想是:需要什麼資料,提供什麼資料。

所以我在後台中已經將資料塞到jsp頁面中,並在js中解析出來,將資料提供給統計圖。

######特別要注意的是,從jsp擷取到的屬性值(int或long,或double,float),當我用js變數var來接收的時候,它統統變成了

string類型的變數,所以在供給資料的時候,一定要解析為js中的數字類型格式,此處用到的方法為:parseLong(),或parseFloa();

這裡調了很長時間,由於資料類型的不匹配,統計圖在調試的過程中,老是不顯示。要長記性。。。。。

此處為將後台擷取到的資料放在頁面中的代碼展示(在頁面中均是隱藏顯示):

<input type="hidden" name="liveTotalCount" id="livePercent" value="${liveCountPercent }">                       <input type="hidden" name="vodTotalCount" id="vodPercent" value="${vodCountPercent }">

之所以將其放入頁面中,是為了方便接收資料。
最關鍵的地方來了,java代碼擷取統計圖所需資料:

//從資料庫查詢資料UserPlaySummaryStatistics playStatistics=userPlayService.getUserPlayStatistics(playSummaryStatistics);                String liveTotalCount= playStatistics.getLiveTotalCount();        String vodTotalCount= playStatistics.getVodTotalCount();        //將擷取到的資料類型轉化為long類型        long liveCount=percentString(playStatistics.getLiveTotalCount());//A站總次數        long vodCount=percentString(playStatistics.getVodTotalCount());//B站總次數                //計算百分比,百分比為double類型,,,,,        double livePercent = (double)liveCount/ (liveCount+vodCount);        DecimalFormat format = new DecimalFormat("0.00%");        String liveCountPercent = format.format(livePercent);        System.out.println("liveCountPercent"+liveCountPercent);                double vodPercent = (double)vodCount/ (liveCount+vodCount);        DecimalFormat format1 = new DecimalFormat("0.00%");        String vodCountPercent = format1.format(vodPercent);        System.out.println("vodCountPercent"+vodCountPercent); request.setAttribute("playCount", playCount);        request.setAttribute("liveCountPercent", liveCountPercent);        request.setAttribute("vodCountPercent", vodCountPercent);        

主要的代碼部分就是以上,這樣就可以輕而易舉實現一個統計圖,而且載入的資料比較快。

也有用ajax實現資料請求的,過程和邏輯也比較複雜,後面我也會將我用ajax實現統計圖的代碼案例分享出來,已做筆記。

 

java代碼實現highchart與資料庫資料結合完整案例分析(一)---餅狀圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.