`
wlhbye
  • 浏览: 26582 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

highcharts 实现曲线报表

阅读更多

由于业务上的需求,网上找了不少例子,最终选择了highcharts 来实现曲线报表。下面我把个人心得与大家分享下:

 

 

如上图所示

X轴为整点时间轴

Y轴为数据坐标轴

该功能主要是检测当天 某时间检测到的数据。

代码如下所示:

 

Highcharts.setOptions({global:{useUTC : false}});
$(function(){  
	//声明报表对象  
	var chart = new Highcharts.Chart({  
		chart: {
		renderTo: 'container', // 报表显示在 页面位置 container 为 div 定义的属性
		defaultSeriesType: 'spline', // 定义报表类型 例如:line ,spline ,colume
		events: {
				load:  getForm  //调用js 方法
			}
		},
		title:{
			text:'实时监测曲线图" //定义曲线报表 名称
		},
		xAxis: {
			type: 'datetime', // 定义时间轴的 类型
			maxPadding : 0.05,
			minPadding : 0.05,
			tickWidth:1,//刻度的宽度
			lineWidth :1,//自定义x轴宽度
			gridLineWidth :1,//默认是0,即在图上没有纵轴间隔线
			lineColor : '#990000' 	
		},
		yAxis: {
				max:12, // 定义Y轴 最大值
				min:0, // 定义最小值
				minPadding: 0.2, 
				maxPadding: 0.2,
				tickInterval:1, // 刻度值
				title: {
					text: 'PH值'
				},
				// plotLines 表示为定义曲线报表中的 (刻度线)或者叫做(定义的区间范围)
// 一下为2条表示线
				plotLines: [{ 
							value: 6,
							color: 'green',
							dashStyle: 'shortdash',
							width: 2,
							label: {
								text: '正常'
							}
						},{
							value: 8,
							color: 'green',
							dashStyle: 'shortdash',
							width: 2,
							label: {
								text: '正常'
							}
						}
				}]
		},
		tooltip: {  // 表示为 鼠标放在报表图中数据点上显示的数据信息
			formatter: function() {
				  return '<b>'+'日期:' +'</b>'
+Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
				   '<b>'+'<%=lbname%>:' +'</b>'+ this.y+'  <%=shll%>';
			}
		},
		series: [{
			name: 'PH',
			data: [] // 此处存放数据值 用,分开 例如:1.8,8.9,null,9.2,   如果在某一点数据为空  
//可以用null 来表示 
			//如果是想动态扶植  这个位置 应该为空 即:data: []
		}]
	});  
	// 与后台进行数据交互
	function getForm(){  
		jQuery.getJSON("test!test.do?id=123456", null, function(data) {   
			//为图表设置值   
			chart.series[0].setData(data);   
		});   	
	}  
	//定时刷新 列表数据
   $(document).ready(function() {  
	   //每隔3秒自动调用方法,实现图表的实时更新  
	   window.setInterval(getForm,50000);   
   });  
});

//定义 曲线报表图 的样式
	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'  // 此处会在Y轴坐标2点之前出现小格  所以就没有使用。
	   },
	   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);

 

 

 

 

 

 针对于此种情况的后台返回的参数如:[new DATE().gettime(),12] --->[时间,数据]

后台传值为:[[new DATE().gettime(),12] ,[new DATE().gettime(),10] ,[new DATE().gettime(),9] ]

 

根据查询返回的list 

        JSONArray array =  new JSONArray();
        array.addAll(list);
        response.getWriter().println(array.toString());

返回 结果集到前台js中去

 

 

上图是实现的 月报表,主要是查询系统当前月每天的平均数的记录

sql:

select round(avg(jcsz), 2) jcsz ,mdate from (

	select to_char(to_date(substr(z_date,1,10),'yyyy-mm-dd'), 'dd') mdate, jcsz  

	from table  where z_date like '%"2011-07-07"%'  and cydd = '123456'
	
) a  group by a.mdate order by mdate

 

如果某天数据不存在 则 需要补空 ‘null’

补空后台代码:

Set<Integer> set = new HashSet<Integer>();
for(int a =1 ; a<31 ; a++){
	set.add(a);
}

for(int k:set){
	boolean flag = false;
	
	for(int i = 0 ; i<list.size() ; i++){
		Test test = list.get(i);
		int day = test.getInt("MDATE");
		if(k==day){
			map.put(k, bean.getStr("JCSZ"));
			flag = true;
		}
	}
	if(flag)
		continue;
	map.put(k, "null");
}

   传map返回页面

request.setAttribute("map", map);

 

前台代码

var chart;
jQuery(document).ready(function() {
	chart = new Highcharts.Chart({
		chart: {
			renderTo: 'container',
			defaultSeriesType: 'spline'
		},
		title: {
			text:  "月监测曲线图",
			x: -20  
		}, 
		xAxis: {
			maxPadding : 0.05,
            minPadding : 0.05,
			tickPixelInterval : 10,
	        tickWidth:5,//刻度的宽度
	        lineColor : '#990000',//自定义刻度颜色
	        lineWidth :1,//自定义x轴宽度
	        gridLineWidth :1,//默认是0,即在图上没有纵轴间隔线
			categories: ['1', '2', '3', '4', '5', '6',  '7', '8', '9', '10', '11', '12', '13', '14', '15',
 '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30','31']		
			},
		yAxis: {
			 max:12, 
			 min:0,
		     minPadding: 0.2,
		     maxPadding: 0.2,
		     tickInterval:1,
			title: {
			text: 'test'
		},
		plotLines: [{
			        	value: 150,
			        	color: 'green',
			        	dashStyle: 'shortdash',
			        	width: 2,
			        	label: {
			        		text: '正常'
			        	}
					},
				   	{
			        	value: 1,
			        	color: 'green',
			        	dashStyle: 'shortdash',
			        	width: 2,
			        	label: {
			        		text: '正常'
			        	}
			        }]
		},
		tooltip: {
			formatter: function() {
	                return '<b>'+ '第' +'</b>'+ this.x +' 天 : '+ this.y;
			}
		},
		series: [ {
			name: 'test',
			data: [ <%
					Map map = (Map)request.getAttribute("map");
						Iterator it = map.entrySet().iterator();
						String valuee =" ";
						while (it.hasNext()) {   
					
							Map.Entry entry = (Map.Entry) it.next();   
					
							Object key = entry.getKey();   
							valuee+= entry.getValue()+",";
						}
						out.println(valuee);
					%>	
				]
		}]
	});
});

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'
	
	   
   },
   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);

 

 

 

  • 大小: 32.4 KB
  • 大小: 44.4 KB
分享到:
评论
12 楼 heppytt 2015-07-29  
11222222
11 楼 qijiacj 2014-12-29  
你好,可以发份完整的源码给我吗,谢谢,149600577@qq.com
10 楼 william_free228 2014-12-29  
您好,可以发一份完整的源码给我么?william_free228@126.com,谢谢!
9 楼 a8999269 2012-07-24  
可以把完整例子发给我一份么?
邮箱:734171637@qq.com
8 楼 308202251 2012-02-18  
请问下LZ。。可以发给我完整的例子吗??1158808160@qq.com
        
7 楼 metal163 2012-02-17  
请问下LZ。。可以发给我完整的例子吗??1315342612@qq.com
6 楼 wl4150 2012-02-07  
楼主 能共享一下么,刚好碰到了类似的需求,试着做了几种,发现性能都不高。
我的邮箱wl4150@163.com,多谢啦~
5 楼 heppytt 2012-01-08  
能不能给个完整的例子282802218@qq.com
4 楼 qingchenyuji 2011-12-22  
能共享一下完整的例子吗?
122351338@qq.com
谢谢
3 楼 haihongxingzi 2011-09-26  
谢谢!已收到!
2 楼 haihongxingzi 2011-08-30  
可以把完整例子发给我一份么?
邮箱:464229847@qq.com
1 楼 zhangpurple 2011-08-15  
完整例子共享一下

相关推荐

    Highcharts AJAX后台JAVA JSON 曲线报表完美可运行例子 不包含官方JS 自己下载

    Highcharts AJAX后台JAVA JSON 曲线报表完美可运行例子 不包含官方JS 自己下载

    使用Highcharts快速开发报表中文WORD版

    Highcharts目前支持折线图,曲线图,块状图,块状-曲线图,柱形图,条形图,饼图和散点图类型。由于Highcharts具有强兼容性、灵活易用性、画面效果好等特点,能被广泛用于报表开发。感兴趣 资源太大,传百度网盘了...

    使用Highcharts快速开发报表

    Highcharts是一个纯JavaScript编写的图表库,它提供了一个将交互式的图表添加到您的网站或Web应用程序的简单方法。 Highcharts目前支持折线图,曲线图,块状图,块状-曲线图,柱形图,条形图,饼图和散点图类型。

    Highcharts

    Highcharts是一个纯JavaScript编写的图表库,它提供了一个将交互式的图表添加到您的网站或Web应用程序的简单方法。 Highcharts目前支持折线图,曲线图,块状图,块状-曲线图,柱形图,条形图,饼图和散点图类型。

    highcharts

    highcharts图片插件,用于创建界面美观、功能强大的图标工具,里面有API demo实例 文档等

    FusionChartsFree、Amcharts、Highcharts用列

    目前HighCharts支持的图表类型有曲线图、区域图、柱状图、饼状图、散状点图和综合图表。HighCharts由于使用JavaScript编写,所以不需要像Flash和Java那样需要插件才可以运行,而且运行速度快。另外HighCharts还有很...

    在.NET MVC 中使用Highcharts+Ajax+Json生成动态曲线图,柱状图,饼图包含网页静态demo

    该项目主要是使用.NET MVC 和Highcharts+Ajax+Json生成动态曲线图,柱状图,饼图,让数据更加的明显美观,可以满足绝多数数据报表的需求!

    HighCharts源码和示例. 2.05Version

    HighCharts源码,jar和示例. 用作报表工具很好, 很好的js报表工具, 曲线图, 柱状图,饼图等. 简单易用. 值得推荐使用.

    HighChart API

    目前HighCharts支持的图表类型有曲线图、区域图、柱状图、饼状图、散状点图和综合图表。  HighCharts界面美观,由于使用JavaScript编写,所以不需要像Flash和Java那样需要插件才可以运行,而且运行速度快。另外...

    highchartsDemo

    纯JS曲线报表highchartsDemo

    制作图表的Javascript类库

    Highcharts是一个制作图表的Javascript类库,可以制作的图表有:直线图,曲线图、区域图、区域曲线图、柱状图、饼装图、散状图等等。

Global site tag (gtag.js) - Google Analytics