jQuery有很多好用的插件,最近由于项目需要学习使用了Highcharts、flexigrid、jsTree三个插件: (一) Highcharts是一个制作图表的纯Javascript类库,支持大部分的图表类型:直线图,曲线图、区域图、区域曲线图、柱状图、饼装图、散布图;使用需要导入jquery.js,highcharts.js以及exporting.js,可以通过网站http://www./下载。 首先是 Highcharts插件
Highcharts包括配置选项 chart:{ renderTo//放置图表的容器 defaultSeriesType//图表类型line, spline, area, areaspline, column, bar, pie , scatter inverted: true//左右显示,默认上下正向 } tooltip:{ formatter:当鼠标悬置数据点时的提示 } xAxis:{ categories:值的列表eg.[1,2,3] } yAxis:{ title:纵柱标题 labels:纵柱标尺 } title:{ text:图标标题名 style:标题样式 } subtitle:{ } legend:{}图例位置样式 series接受的数据格式为json Array:[{},{},{}]具体对应yAxis 不再啰嗦直接上例子:
$(document).ready(
function() { chart = new Highcharts.Chart( { chart : { renderTo : 'container', margin : [ 80, 200, 60, 100 ], zoomType : 'xy' }, title : { text : '合同 ${contractName} 数据分析', style : { margin : '10px 0 0 0' // center it } }, subtitle : { text : ' style : { margin : '10px 0 0 0' // center it } }, xAxis : [ { categories : [ } ], yAxis : [ // Secondary yAxis { title : { text : '点击数', margin : 70, style : { color : '#4572A7' } }, labels : { formatter : function() { return this.value + ' 次'; }, style : { color : '#4572A7' } } }, { // Tertiary yAxis
title : { text : '曝光数', margin : 80, style : { color : '#AA4643' } }, labels : { formatter : function() { return this.value + ' 次'; }, style : { color : '#AA4643' } }, opposite : true, offset : 100 }, { // Primary yAxis labels : { formatter : function() { return this.value + '%'; }, style : { color : '#89A54E' } }, title : { text : '点击率', style : { color : '#89A54E' }, margin : 50 }, opposite : true }],
tooltip : { formatter : function() { var unit = { '点击数' : '次', '曝光数' : '次', '点击率' : '%' }[this.series.name]; return '' + this.x + ': ' + this.y + ' ' + unit;
} }, legend : { layout : 'vertical', style : { left : '30px', bottom : 'auto', right : 'auto', top : '10px' }, backgroundColor : '#FFFFFF' }, series : [ { name : '点击数', color : '#4572A7', type : 'line', data : [ },
{ name : '曝光数', type : 'line', color : '#AA4643', yAxis : 1, data : [ },
{ name : '点击率', color : '#89A54E', type : 'line', yAxis : 2, data : [ } ] }); }); Html中只需要适当位置加上
即可。如果要显示网格式的风格再js中再加入下面代码:Highcharts.theme = {
colors: ['#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'], chart: { backgroundColor: { linearGradient: [0, 0, 500, 500], stops: [ [0, 'rgb(255, 255, 255)'], [1, 'rgb(240, 240, 255)'] ] } , borderWidth: 2, plotBackgroundColor: 'rgba(255, 255, 255, .9)', plotShadow: true, plotBorderWidth: 1 }, title: { style: { color: '#000', font: 'bold 16px "Trebuchet MS", Verdana, sans-serif' } }, subtitle: { style: { color: '#666666', font: 'bold 12px "Trebuchet MS", Verdana, sans-serif' } }, xAxis: { gridLineWidth: 1, lineColor: '#000', tickColor: '#000', labels: { style: { color: '#000', font: '11px Trebuchet MS, Verdana, sans-serif' } }, title: { style: { color: '#333', fontWeight: 'bold', fontSize: '12px', fontFamily: 'Trebuchet MS, Verdana, sans-serif' }
} }, yAxis: { minorTickInterval: 'auto', lineColor: '#000', lineWidth: 1, tickWidth: 1, tickColor: '#000', labels: { style: { color: '#000', font: '11px Trebuchet MS, Verdana, sans-serif' } }, title: { style: { color: '#333', fontWeight: 'bold', fontSize: '12px', fontFamily: 'Trebuchet MS, Verdana, sans-serif' } } }, legend: { itemStyle: { font: '9pt Trebuchet MS, Verdana, sans-serif', color: 'black' },
itemHoverStyle: { color: '#039' }, itemHiddenStyle: { color: 'gray' } }, labels: { style: { color: '#99b' } } }; var highchartsOptions = Highcharts.setOptions(Highcharts.theme);
你也可以自己按要求定义显示风格。
另外,要从服务器获取数据动态生成运行曲线,像心电图那样可以
配置chart:{
events:{ load:requestData } } , series: [{ name: '服务器数据', data: [] }] 方法: chart.series[0].addPoint([x,y], true, shift);
(二)jQuery插件flexigrid是像ext的DataGrid一样的活动表格插件,使用它首先要引入query.js,还要引入flexigrid.js和flexigrid.css
看看插件中部分代码以及说明
了解了每个属性的含义后就可以根据自己的需要来配置了,看我的实例:
$("#flex1").flexigrid
( { url: './contract_getAllJsonData.action?startDate= dataType: 'json', method: 'POST', colModel : [ {display: '广告位', name : 'advName', width : 300, sortable : true, align: 'center'}, {display: '开始投放时间', name : 'start', width : 180, sortable : true, align: 'left'}, {display: '结束投放时间', name : 'end', width : 180, sortable : true, align: 'left'}, {display: '点击数', name : 'clicks', width : 80, sortable : true, align: 'left'}, {display: '曝光数', name : 'impressions', width : 80, sortable : true, align: 'left'}, {display: '分析', name : 'impressions', width : 80, sortable : true, align: 'left'} ], sortname: "advName", sortorder: "asc", usepager: true, useRp: true, title: '统计期间该合同广告位投放', showTableToggleBtn: true, width: 1000, height: 200 } ); 后台java代码(struts2),由于框架不同代码有可能不同,我这里一次查出了所有需要数据,没有考虑分页,需要分页的话,请将page 等信息传入后来查询。
/**
* 用于结合flexigrid实现可选表格 * @return */ public String getAllJsonData() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); ContractReportService service = (ContractReportService) SpringHelper .getBean(TMG_REPORTS_CONTRACT); Map map = new HashMap(); map.put("contractId", contractId); map.put("startdate", startDate); map.put("enddate", endDate); list = service.getAdvListbyContract(map); try { List mapList = new ArrayList(); for(int i = 0; i < list.size(); i++) { Map cellMap = new HashMap(); ContractReport cr = (ContractReport)list.get(i); cellMap.put("cell", new Object[] {cr.getAdvName(), formatter.format(cr.getStart()), formatter.format(cr.getEnd()), cr.getClicks(), cr.getImpressions()," ![]() mapList.add(cellMap); } Map pageInfo = new HashMap(); pageInfo.put("rows", mapList); JSONObject object = new JSONObject(); object.accumulateAll(pageInfo); Struts2Utils.getResponse().setContentType("text/x-json");
Struts2Utils.getResponse().setCharacterEncoding("UTF-8"); Struts2Utils.getResponse().setHeader("Cache-Control", "no-cache"); Struts2Utils.getResponse().getWriter().write(object.toString());
Struts2Utils.getResponse().getWriter().flush(); Struts2Utils.getResponse().getWriter().close(); } catch (IOException e) {
e.printStackTrace(); } return null; } 最后上图(用highcharts和flexigrid实现):
![]() 由于时间仓促,仅仅写了些皮毛,大家参考使用,详细使用方法还请参考官网。 |
|