Jquery.foat 生成饼状图,柱状图代码

调用Jquery,flot 直接生成饼状图;

前台代码:

  <div class="row-fluid">
                    <div class="span12">
                        <div class="box">
                            <div class="box-head">
                                <h3>图书相关统计图</h3>
                            </div>
                            <div>
                                
                            </div>
                            <div style="height: 15px; width: 500px;margin-left:500px;">
                                <div style="float:left;">书本种类:</div><div id="navicate" style="height: 15px; width: 40px; background-color: #2872bd;float:left"></div>
                                <div style="float:left;">书本总数:</div><div id="navicate" style="height: 15px; width: 40px; background-color: #666666; float: left; "></div>
                                <div style="float:left;">书本剩余:</div><div id="navicate" style="height: 15px; width: 40px; background-color: #feb900; float: left; "></div>
                            </div>
                            <div class="box-content">
                                <div class="flot flot-bar"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span12">
                        <div class="box">
                            <div class="box-head">
                                <h3>书籍种类统计图</h3>
                            </div>
                            <div class="box-content">
                                <div class="flot flot-pie"></div>
                            </div>
                        </div>
                    </div>
                </div>

js代码:

饼状图:

       //采用回调函数获取数据库的数据转成Json格式传给前台展示
$.get("/Book/getData", function (data, status) {
   var myjson = JSON.parse(data);
   $.plot($(".flot-pie"), myjson, options2);
});


条形图:

var dateX = [];
$.get("/Book/getAllCount", function (data, status) {
   $("#AllCount").val(data);
   var allCount = JSON.parse(data);
   var d1 = [];
   var d2 = [];
   var d3 = [];
   for (var i = 0; i < allCount.length; i += 1) {
       d1.push([i, parseInt(allCount[i].typeAll)]);
       dateX.push([i, allCount[i].type]); 
   }
   for (var i = 0; i < allCount.length; i += 1) {
       d2.push([i, parseInt(allCount[i].all)]);
   }
   for (var i = 0; i < allCount.length; i += 1) {
       d3.push([i, parseInt(allCount[i].remind)]);
   }




   var ds = new Array();


   ds.push({
       data: d1,
       bars: {
           show: true,
           barWidth: 0.2,
           order: 1,
           lineWidth: 2
       }
   });
   ds.push({
       data: d2,
       bars: {
           show: true,
           barWidth: 0.2,
           order: 2
       }
   });
   ds.push({
       data: d3,
       bars: {
           show: true,
           barWidth: 0.2,
           order: 3
           
       }
          });
   
   if ($('.flot-bar').length > 0) {
       $.plot($(".flot-bar"), ds, {
           xaxis: {
               ticks: dateX
           },
           grid: {
               hoverable: true,
               clickable: true
           }, colors: ['#2872bd', '#666666', '#feb900', '#128902', '#c6c12f']
       });
   }
});


读者测试可用代码;

var bookJson = [
{ label: "网络", "data": 10 },
{ label: "技术", "data": 30 },
{ label: "办公", "data": 20 },
];
if($(".flot-pie").length > 0){
   $.plot($(".flot-pie"), bookJson, options2);
}
var d1 = [];
for (var i = 0; i <= 10; i += 1)
d1.push([i, parseInt(Math.random() * 30)]);


var d2 = [];
for (var i = 0; i <= 10; i += 1)
d2.push([i, parseInt(Math.random() * 30)]);


var d3 = [];
for (var i = 0; i <= 10; i += 1)
d3.push([i, parseInt(Math.random() * 30)]);


var ds = new Array();


ds.push({
data:d1,
bars: {
show: true, 
barWidth: 0.2, 
order: 1,
lineWidth : 2
}
});
ds.push({
data:d2,
bars: {
show: true, 
barWidth: 0.2, 
order: 2
}
});
ds.push({
data:d3,
bars: {
show: true, 
barWidth: 0.2, 
order: 3
}
});


if($('.flot-bar').length > 0){
$.plot($(".flot-bar"), ds, {grid: {
hoverable: true,
clickable: true
},colors: [ '#2872bd', '#666666', '#feb900', '#128902', '#c6c12f']});
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值