Highcharts简介
最近要做一个油田油压或温度数据的监控软件,数据会秒级写到数据库中,界面上需要动态展示数据跟随时间变化。
在网上找了很多js插件,希望能够即时高效的展示数据,最终确定了使用Highcharts插件。
Highcharts插件可以免费使用,而且可以高效的展示数据,下面先上一张效果图:
数据通过ajax每秒向数据库请求,图形跟随时间的变化向左移动。使用方法如下:
1.前台界面
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Highcharts Example</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript"> $(function () { $(document).ready(function () { Highcharts.setOptions({ global: { useUTC: false } }); var chart; chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'spline', animation: Highcharts.svg, // don't animate in old IE marginRight: 10, backgroundColor: { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [ [0, 'rgb(96, 96, 96)'], [1, 'rgb(16, 16, 16)'] ] }, borderWidth: 0, borderRadius: 15, plotBackgroundColor: null, plotShadow: false, plotBorderWidth: 0, events: { load: function () { // set up the updating of the chart each second var series = this.series[0]; var loaddata = function () { var x = (new Date()).getTime(); // current time var y = 0; $.ajax({ async: false, type: "POST", dataType: "json", contentType: "application/json;charset=utf-8", url: "DataHandler.ashx", //读取数据 success: function (result) { y = parseInt(result.amount); } }); series.addPoint([x, y], true, true); $.ajax({ type: "POST", dataType: "json", url: "AddHandler.ashx", //模拟向数据库插入数据 success: function (msg) { } }); }; setInterval(loaddata, 5000);//每5s执行一次 } } }, title: { text: '油压数据实时监控', style: { color: '#FFFF00', fontSize: '16px' } }, xAxis: { title: { text: '检测时间', style: { color: '#FFFF00' } }, type: 'datetime', tickPixelInterval: 150, labels: { style: { color: '#FFE4B5' } }, gridLineWidth: 1 }, yAxis: { title: { text: '油压(pa)', style: { color: '#FFFF00' } }, plotLines: [{ value: 0, width: 1, color: '#808080' }], labels: { style: { color: '#FFE4B5' } } }, tooltip: { formatter: function () { return '<b>油压:' + Highcharts.numberFormat(this.y, 2) + '</b><br/>' + '时间:' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) }, crosshairs: true }, plotOptions: { spline: { marker: { radius: 4, lineColor: '#FFE4B5', lineWidth: 1 } } }, legend: { enabled: true }, exporting: { enabled: true }, series: [{ name: '油压变化图', 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 * 2000, y: Math.random() * 100 }); } return data; })() }] }); }); }); </script> </head> <body> <script src="highcharts.src.js" type="text/javascript"></script> <script src="exporting.js" type="text/javascript"></script> <div id="container" style="min-width: 310px; height:550px; margin: 0 auto"> </div> </body> </html>
2.Aajx读取数据程序:
public class DataHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string test = SQLHelper.ExecuteScalar("SELECT TOP(1) amount FROM RealOil ORDER BY recordtime DESC").ToString(); //Random ran=new Random(0); //test=ran.Next(15,100).ToString(); string jsonString = "{\"time\":1,\"amount\":\"" + test + "\"}"; context.Response.Write(jsonString); } public bool IsReusable { get { return false; } } }