您的位置:首页 > 编程语言 > ASP

Highcharts结合Asp.net实现动态数据股票式图形显示实例 .【转】

2012-06-25 14:51 881 查看
本实例针对的具体应用行情是我需要定时从数据库中读取某商品价格,然后以图形显示在网页上,类似于股票中的交易品种价格走势图.

我使用的是asp.net,结合jQuery中的Highcharts插件实现,该插件可以支持很多类型的图形,但都是静态的,提供的实例都需要改造,有兴趣的朋友可以去Highcharts官网看看(http://www.highcharts.com/),里面提供了很多的Demo,说实在的要实现我的需求,更应该使用Highstock,但是看了Highcharts的dynamic-update实例,也可以实现我的需求界面更加简洁些,所以本文采用的是Highcharts中dynamic-update模型,图形类型是spline最终实现的效果贴图如下(品旗金这个品种的价格是10秒种更新一次的,每10秒对线状图形加一个点):



实现步聚:

用vs新建一web网站,新建asp.net页面DynamicUpdate.aspx,页面代码如下:

[html] view plaincopyprint?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicUpdate.aspx.cs"

Inherits="WebImg.DynamicUpdate" %>

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>动态显示价格图例</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/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',

marginRight: 10,

events: {

load: function () {

// set up the updating of the chart each second

var series = this.series[0];

setInterval(function () {

var randomCode = parseInt(Math.random() * 10000);

var oldrandomCode = parseInt(Math.random() * 10000);

$.ajax({

url: "ajaxhandler/dataupdate.ashx?" + randomCode + "=" + oldrandomCode + "&flag=1",

type: "get",

dataType: "json",

success: function (data, textStatus, XMLHttpRequest) {

var ctime = new Date();

ctime.setTime(parseInt(data.rows[0].times));

var x = ctime.getTime(), // current time

y = data.rows[0].price * 200;

series.addPoint([x, y], true, true);

},

error: function () { }

});

}, 10000);

}

}

},

title: {

text: '品旗撮合交易市场品种行情'

},

xAxis: {

title: {

text: '时间'

},

type: 'datetime',

tickPixelInterval: 150,

labels:{formatter:function(){

var vDate = new Date(this.value);

return vDate.getHours()+":"+vDate.getMinutes()+":"+vDate.getSeconds();

}}

},

yAxis: {

title: {

text: '价格'

},

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

},

credits:{

text:'品旗金属交易市场',

url:'http://www.inv6.com'

},

series: [{

name: '品旗金(GOLD)',

data: (function () {

var data = [],

i;

var jsonData = <%= jsonstr %>;

for (i = -19; i <= 0; i++) {

var ctime = new Date();

ctime.setTime(parseInt(jsonData.rows[i+19].time1));

data.push({

x: ctime.getTime(),

y: jsonData.rows[i + 19].price * 200

});

}

return data;

})()

}]

});

});

});

</script>

</head>

<body>

<script src="http://code.highcharts.com/highcharts.js"></script>

<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto">

</div>

</body>

</html>

[csharp] view plaincopyprint?

public string jsonstr = "";

protected void Page_Load(object sender, EventArgs e)

{

getData();

}

private void getData()

{

DataTable dtPrice = SQLDBHelper.ExecuteTable("select top 20 TMPCode,TMPTime,TMPPrice from TabMerPrice with(nolock) order by TMPTime desc");

string json = "{\"rows\":[";

for (int i = (dtPrice.Rows.Count-1); i >= 0; i--)

{

json += "{\"time1\":\"" + (Convert.ToDateTime(dtPrice.Rows[i]["TMPTime"]).AddHours(-8) - new DateTime(1970, 1, 1)).TotalMilliseconds + "\",\"price\":\"" + (Convert.ToDecimal(dtPrice.Rows[i]["TMPPrice"])/200) + "\"},";

}

json = json.TrimEnd(',');

json += "]}";

jsonstr = json;

}

public string jsonstr = "";
protected void Page_Load(object sender, EventArgs e)
{
getData();
}
private void getData()
{
DataTable dtPrice = SQLDBHelper.ExecuteTable("select top 20 TMPCode,TMPTime,TMPPrice from TabMerPrice with(nolock) order by TMPTime desc");
string json = "{\"rows\":[";
for (int i = (dtPrice.Rows.Count-1); i >= 0; i--)
{
json += "{\"time1\":\"" + (Convert.ToDateTime(dtPrice.Rows[i]["TMPTime"]).AddHours(-8) - new DateTime(1970, 1, 1)).TotalMilliseconds + "\",\"price\":\"" + (Convert.ToDecimal(dtPrice.Rows[i]["TMPPrice"])/200) + "\"},";
}
json = json.TrimEnd(',');
json += "]}";
jsonstr = json;
}


其中TabMerPrice表是品种价格信息表,TMPTime时间,TMPPrice价格,TMPTime的时间间隔是十秒,我用数据库作业生成该表的记录,十秒钟发生一次

AddHours(-8)是为了将时间转换为北京时间,生成的数据格式是json格式

dataupdate.ashx代码如下:

[csharp] view plaincopyprint?

public void ProcessRequest(HttpContext context)

{

string flag = context.Request.QueryString["flag"].ToString();

switch (flag)

{

case "1"://获取品种当前价格

GetMerPrice(context);

break;

}

}

private void GetMerPrice(HttpContext context)

{

context.Response.Clear();

context.Response.Buffer = true;

context.Response.ContentType = "application/json";

context.Response.ContentEncoding.GetBytes("utf-8");

DataTable dtPrice = SQLDBHelper.ExecuteTable("select top 1 TMPCode,TMPTime,TMPPrice from TabMerPrice with(nolock) order by TMPTime desc");

string json = "{\"rows\":[";

json += "{\"times\":\"" + (Convert.ToDateTime(dtPrice.Rows[0]["TMPTime"]).AddHours(-8) - new DateTime(1970, 1, 1)).TotalMilliseconds + "\",\"price\":\"" + (Convert.ToDecimal(dtPrice.Rows[0]["TMPPrice"]) / 200) + "\"}";

json = json.TrimEnd(',');

json += "]}";

context.Response.Write(json);

context.Response.Flush();

context.Response.End();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: