highcharts 柱状图 or折线图 or 饼图 使用

dodo.highcharts.js

(function(){
    var defaults={id:"container",type:"spline",data:[],date:[],totalcount:30,verticalAlign:"bottom"};
    var dodo={
            //柱状图 or折线图 or 饼图
            highcharts:function(options){
                var jg=10;
                defaults = $.extend(defaults, options); 
                if(defaults.totalcount <= 30 && defaults.totalcount>7){ //一个月
                    jg=7; //7天一显示
                }else if(defaults.totalcount <= 120 && defaults.totalcount>30){
                    jg=30; //30天一显示
                }else if(defaults.totalcount <= 7 && defaults.totalcount>0){
                    jg=1;  //一天一显示
                }else if(defaults.totalcount <= 365 && defaults.totalcount>120){
                    jg=90;  //一个季度一显示
                }else if(defaults.totalcount > 365){
                    jg=120;
                }
                //y 轴 显示的个数
               /* var tickPositionsdata=[];
                if(totalcount>8){
                        tickPositionsdata.push(0); 
                     tickPositionsdata.push(parseInt(parseInt(totalcount)/7));
                     tickPositionsdata.push(parseInt(parseInt(totalcount)/7)*2);
                     tickPositionsdata.push(parseInt(parseInt(totalcount)/7)*3);
                     tickPositionsdata.push(parseInt(parseInt(totalcount)/7)*4);
                     tickPositionsdata.push(parseInt(parseInt(totalcount)/7)*5);
                     tickPositionsdata.push(parseInt(parseInt(totalcount)/7)*6);
                     tickPositionsdata.push(totalcount-1);
                }*/
                
                var chart = new Highcharts.Chart({  
                    chart: { 
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false,
                        renderTo: defaults.id, 
                        type: defaults.type,
                        ignoreHiddenSeries:true
                    },  
                    title: { 
                        enabled: true, 
                        text: "" 
                    }, 
                    exporting: { enabled: false },
                    credits: { enabled: false },
                    subtitle: {  
                        text: ''  
                    },  
                    xAxis: {
                        categories:defaults.date,
                        type: "datetime",
                        maxPadding : 0,
                        minPadding : 0,
                        /*tickPositions:tickPositionsdata,*/
                        tickInterval :jg,
                        tickPixelInterval : 10,
                        tickWidth:1,//刻度的宽度
                        lineColor : '#990000',//自定义刻度颜色
                        lineWidth :1,//自定义x轴宽度
                        gridLineWidth :0
                    },
                    
                    yAxis: {
                        endOnTick: false,
                        gridLineWidth: 1,
                        gridLineDashStyle: "dash",
                        gridLineColor: "#c0c0c0",
                        lineWidth:1,
                        title: { text: null },
                        labels: {
                            formatter: function() {
                                return Highcharts.numberFormat(this.value, 2);
                            }
                        }
                    },  
                    tooltip : {
                        useHTML : true,
                        crosshairs: true,
                        shared: true
                    },
                    plotOptions: {
                        spline: {
                            marker: {
                                radius: 0, //是否描点
                                states: { 
                                    hover: {
                                        symbol: 'circle', 
                                        radius: 5,
                                        fillColor: 'red', 
                                        lineWidth: 1
                                    } 
                                },
                                marker: {
                                    enabled: false
                                }
                            }
                        },
                        pie: {
                            allowPointSelect: true,
                            cursor: 'pointer',
                            dataLabels: {
                                enabled: false
                            },
                            showInLegend: true
                        }
                    },
                    legend: {
                         enabled: true,    //是否显示线的介绍,默认为true
                        align: 'center' ,
                        verticalAlign: defaults.verticalAlign 
                    },
                    series:defaults.data
                }); 
            }
            
    }
    window.dodo = dodo;
}());
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, 
        "d+": this.getDate(),
        "h+": this.getHours(), 
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
 

 

jsp 页面调用:

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>highcharts</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hightchartsLesson</title>
<script type="text/javascript" src="/static/asset/jquery-2.2.4.min.js"></script>
<script src="/static/asset/highcharts.js"></script>
<script src="/static/asset/aresoft.highcharts.js"></script>

<script>
$(document).ready(function(){
    /* var navDate=[], navData=[],totalData=[];
    $.ajax({  
        type: "POST", 
        url: "/hightCharts/demo/json",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{}",             
        success: function (data) {
            for(k=0;k<data.length;k++){
                navDate.push(new Date(data[k].funddate).Format("yyyy-MM-dd"));
                navData.push(parseFloat(data[k].fundvalue));
                totalData.push(parseFloat(data[k].fundtotalvalue));
            }
            var d=[{name: "本产品净值",color: '#ff5723',data:navData},{name: "本产品累计净值",color: '#2894ff',data:totalData}];
            dodo.highcharts({type:"spline",data:d2,date:navDate,totalcount:data.length});
        }  
    }); */
    
    
    var browsers = [];
    $.ajax({  
        type: "POST", 
        url: "/hightCharts/demo/piejson",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{}",             
        success: function (data) {
            for(k=0;k<data.length;k++){
                browsers.push(data[k]);
            }
            var d3=[{name: '游览器访问统计:',  data:browsers}];
            aresoft_highcharts.spline({type:"pie",data:d3});
        }  
    });
    
    
});

</script>

</head>
<body>
    <div id="result"></div>
    <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</body>
</html>

 

action:

@Controller(value = "/hightCharts/demo")    
public class HightChartsController {

    public View json(){
        List<Map<String,String>> list=new ArrayList<Map<String,String>>();
        Map<String,String> map=new HashMap<String,String>();
        map.put("fundcode","000002");
        map.put("fundname","基金二号");
        map.put("funddate","2015-01-01");
        map.put("fundvalue","1.1"); //单位净值
        map.put("fundtotalvalue","2.1"); //累计净值
        list.add(map);
        
        Map<String,String> map2=new HashMap<String,String>();
        map2.put("fundcode","000002");
        map2.put("funddate","2015-01-02");
        map2.put("fundname","基金二号");
        map2.put("fundvalue","2.1"); //单位净值
        map2.put("fundtotalvalue","3.1"); //累计净值
        list.add(map2);
        
        Map<String,String> map3=new HashMap<String,String>();
        map3.put("fundcode","000002");
        map3.put("funddate","2015-01-03");
        map3.put("fundname","基金二号");
        map3.put("fundvalue","2.1"); //单位净值
        map3.put("fundtotalvalue","4.1"); //累计净值
        list.add(map3);
        
        Map<String,String> map4=new HashMap<String,String>();
        map4.put("fundcode","000002");
        map4.put("funddate","2015-01-04");
        map4.put("fundname","基金二号");
        map4.put("fundvalue","2.1"); //单位净值
        map4.put("fundtotalvalue","4.1"); //累计净值
        list.add(map4);
        
        Map<String,String> map5=new HashMap<String,String>();
        map5.put("fundcode","000002");
        map5.put("funddate","2015-01-05");
        map5.put("fundname","基金二号");
        map5.put("fundvalue","2.1"); //单位净值
        map5.put("fundtotalvalue","4.1"); //累计净值
        list.add(map5);
        
        Map<String,String> map6=new HashMap<String,String>();
        map6.put("fundcode","000002");
        map6.put("funddate","2015-01-06");
        map6.put("fundname","基金二号");
        map6.put("fundvalue","2.1"); //单位净值
        map6.put("fundtotalvalue","4.1"); //累计净值
        list.add(map6);
        
        Map<String,String> map7=new HashMap<String,String>();
        map7.put("fundcode","000002");
        map7.put("funddate","2015-01-07");
        map7.put("fundname","基金二号");
        map7.put("fundvalue","2.1"); //单位净值
        map7.put("fundtotalvalue","4.1"); //累计净值
        list.add(map7);
        
        Map<String,String> map8=new HashMap<String,String>();
        map8.put("fundcode","000002");
        map8.put("funddate","2015-01-08");
        map8.put("fundname","基金二号");
        map8.put("fundvalue","2.1"); //单位净值
        map8.put("fundtotalvalue","4.1"); //累计净值
        list.add(map8);
        
        Map<String,String> map9=new HashMap<String,String>();
        map9.put("fundcode","000002");
        map9.put("funddate","2015-01-09");
        map9.put("fundname","基金二号");
        map9.put("fundvalue","2.1"); //单位净值
        map9.put("fundtotalvalue","4.1"); //累计净值
        list.add(map9);
        
        return ViewFactory.json(list);
    }
    
    
    public View piejson(){
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name","Chrome");
        map.put("selected",true);
        map.put("sliced",true);
        map.put("visible",true);
        map.put("y",12.8);
        list.add(map);
        
        Map<String,Object> map2=new HashMap<String,Object>();
        map2.put("name","IE");
        map2.put("visible",true);
        map2.put("y",20.8);
        list.add(map2);
        
        Map<String,Object> map3=new HashMap<String,Object>();
        map3.put("name","OTHERS");
        map3.put("visible",true);
        map3.put("y",30.8);
        list.add(map3);
        
        return ViewFactory.json(list);
    }

    

转载于:https://my.oschina.net/v512345/blog/748235

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值