昨天看到一个帖子,很感兴趣今天就动手改改实现啦,分享一下,大致效果如下:
上图说明:
1.曲线是动态的,截图原因无法体现,请谅解 ;
2.鼠标移上曲线会有更详细信息小的悬浮框,截图并未体现;
3. 曲线的数据采用随机数模拟的,后面代码红色部分会有解释,实际项目可以动态绑定数据;
4. 主要用到名字为 highcharts.js插件,感兴趣的朋友可以网上查一下可以下载;
新建一个HTML页面,代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <TITLE>实时更新数据的jQuery图表插件DEMO演示</TITLE> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="highcharts.js"></script> <script type="text/javascript"> $(function () { $(document).ready(function() { Highcharts.setOptions({ global: { useUTC: false } }); var chart; $('#container').highcharts({ chart: { type: 'spline', animation: Highcharts.svg, // don't animate in old IE marginRight: 10, events: { load: function() { // set up the updating of the chart each second var series = this.series[0]; setInterval(function() { var x = (new Date()).getTime(), // current time y = Math.random(); series.addPoint([x, y], true, true); }, 1000); } } }, title: { text: 'CPU动态走势图--1秒' }, xAxis: { type: 'datetime', tickPixelInterval: 100 }, yAxis: { title: { text: 'CPU动态走势图--1秒' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ Highcharts.numberFormat(this.y, 2); } }, legend: { enabled: false }, exporting: { enabled: false }, series: [{ name: 'Random data', data: (function() { // generate an array of random data var data = [], time = (new Date()).getTime(), i; for (i = -19; i <= 0; i++) { data.push({ x: time + i * 1000, y: Math.random() }); } return data; })() }] }); }); }); </script> </HEAD> <BODY> <div id="container" style="width:700px;height:400px;margin:0 auto;"></div> <div style="text-align:center;clear:both;"> </div> </BODY> </HTML>
具体用法可以参考一个写得很详细的博客,地址:点击打开链接