HighCharts使用

這個是不少天以前作的了,當時上級要求作一個報表,內部用的小工具。其中要畫折線圖,柱狀圖這些。用了一下JFreeChart,圖是作出來了,可是這個效果很很差。直接生成了一張圖片展現出來,沒法進行交互。百度一下,看到了highcharts,你們評論也不錯,生成的圖表很酷。前端

highcharts和easyui很像,都是經過json來傳遞數據,咱們要作的,就是在後臺封裝前端所須要的數據和格式。ajax

這裏說的是Highcharts中的HighStock這個工具,由於作時間圖的時候,數據很是多,HighCharts是沒法實現那麼多的,可是HighStock沒問題。json

先上前端的代碼:後端

function querygraph(){
    $(function () { 
        var chart;
        var ground=$('#cc').combobox('getValue'); 
        var begin_time=$('#begin_time').datetimebox('getValue');
        var end_time=$('#end_time').datetimebox('getValue');
        $(function(){
            $.ajax({
                type:'POST',
                dataType:'JSON',
                url:'TimeGraph',
                data:"ground="+ground+"&begin_time="+begin_time+"&end_time="+end_time,
                beforeSend: ajaxLoading(),
                    success:function(result){
                        ajaxLoadEnd();
                        $('#container').highcharts('StockChart',{  
                        credits:{
                            enabled:false
                        },
                        rangeSelector : {
                                selected : 1
                            },
                            
                        title: { text: result.text, x: -20 }, 
                        
                        subtitle: {    text:result.subtitle, x: -20},
                        
                         plotOptions: { 
                             series:{
                                 turboThreshold:0
                             }
                         },
                         xAxis: {  
                                tickPixelInterval: 200,//x軸上的間隔  
                            //  title :{  
                            //      text:"title"  
                            //  },  
                                type: 'datetime', //定義x軸上日期的顯示格式  
                                labels: {  
                                formatter: function() {  
                                    return  Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.value); 
                                },  
                                align: 'center'  
                            }  
                            }, 
                        yAxis: { 
                            title: { text: '泊位數量' },
                            //min: 0, 
                            minorGridLineWidth: 0,
                            gridLineWidth: 0,
                            alternateGridColor: null,
                             plotBands: [{ // Light air 
                                 from: result.begin, to: result.total, 
                                 color: 'rgba(255, 0, 0, 0.7)',
                                 label: { 
                                     text: '總泊位數:'+result.total, 
                                     style: { color: '#606060' 
                                         } 
                             } }]
                             },
                        series : [{ 
                            name : '空餘數量', 
                            data : result.series,
                            tooltip: { valueDecimals: 0 }
                        }]
                        
                    }); 
            }
            }
            );
        });
    });
    }

主要用的js代碼如上,其中首部聲明的變量是爲了從easyui獲取字段數據的。api

而後在ajax中請求url,獲取json數據。工具

比較麻煩的是後端數據拼裝了。ui

    public JSONObject getData(String begin_time,String end_time,String ground_code){
        logger.info("ground:"+ground_code+"==");
        logger.info("begin:"+begin_time);
        logger.info("end:"+end_time);
        JSONObject result=new JSONObject();
        String ground_name=reportService.getGroundnameByid(Long.parseLong(ground_code));
        logger.info("groundname:"+ground_name);
        List<Report> list=reportService.getReport(Long.parseLong(ground_code), Timestamp.valueOf(begin_time), Timestamp.valueOf(end_time));
        //數據1,空閒數量
        JSONArray jsonArray2=new JSONArray();
        //數據2,所有
        long total=reportService.getTotalBerth(Long.parseLong(ground_code));
        for (int i = 0; i < list.size(); i++) {
            Report report=list.get(i);
            Date date=report.getRecord_time();
            long time=date.getTime();
            JSONArray jsonArray=new JSONArray();
            jsonArray.add(time);
            jsonArray.add(report.getAvailable_berth_number());
            jsonArray2.add(jsonArray);
        }
        result.put("text", ground_name+" 泊位統計圖");
        result.put("subtitle", begin_time+" 到 "+end_time+"之間的統計量");
        result.put("begin", total-0.5);
        result.put("total", total);
        result.put("series",jsonArray2);
        logger.info("result:"+result.toString());
        return result;
    }
    

其中,主要的數據是jsonArray2這個,這個類型是[[時間,數據],[時間,數據],...] 。時間是utc時間,若是先後臺用的時間不一致的話,其中有一個不是utc時間,那麼會形成時差八小時的問題,這個要統一。要麼是前端禁用utc時間,要麼後臺時間也轉成utc時間。this

效果圖以下:url

除了這個工具,highcharts默認的那些都很好用,api裏面說明也不少。spa

也作了一個柱狀圖,數據拼裝代碼以下:

    public JSONObject getData(String begin_time,String end_time,String ground){
        JSONObject result=new JSONObject();
        Timestamp begin=Timestamp.valueOf(begin_time);
        Timestamp end=Timestamp.valueOf(end_time);
        String ground_name=groundFlowService.getGroundNameById(Long.parseLong(ground));
        List<FlowDaily> list=groundFlowService.getGroundflow(Long.parseLong(ground), begin, end);
        JSONObject categories_json=new JSONObject();
        JSONObject series_json_in=new JSONObject();
        JSONObject series_json_out=new JSONObject();
        JSONArray series_jsonarray=new JSONArray();
        String[] cata=new String[list.size()];
        long[] data_int=new long[list.size()];
        long[] data_out=new long[list.size()];
        //駛入量
        series_json_in.put("name", "駛入量");
        series_json_out.put("name", "駛出量");
        for (int i = 0; i < data_int.length; i++) {
            FlowDaily flowDaily=list.get(i);
            data_int[i]=flowDaily.getIncount();
            data_out[i]=flowDaily.getOutcount();
            cata[i]=flowDaily.getStat_date().toString().substring(0, 10);
        }
        series_json_in.put("data", data_int);
        series_json_out.put("data", data_out);
        categories_json.put("categories", cata);
        series_jsonarray.add(series_json_in);
        series_jsonarray.add(series_json_out);
        result.put("text", ground_name+" 柱狀統計圖");
        result.put("subtitle", begin_time+" 到 "+end_time+"之間的統計量");
        result.put("categories", categories_json);
        result.put("series", series_jsonarray);
        /*拼接的結果格式
         * categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas'] 
         *     series: [{ name: 'John', data: [5, 3, 4, 7, 2] }, 
                    { name: 'Jane', data: [2, -2, -3, 2, 1] }, 
                    { name: 'Joe', data: [3, 4, 4, -2, 5] }] */
        logger.info(result);
        return result;
    }
相關文章
相關標籤/搜索