问题描述
最近看了highchart后想着自己编一个试试,结果遇到额不少问题,求大神指点下。现在第一个数据能传进去,数据是从后台数据库读,谢谢了。<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="ts.aspx.cs"Inherits="动态图最终版.ts"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>成功了</title><scripttype="text/javascript"src="js/jquery.js"></script><scripttype="text/javascript"src="js/highcharts.js"></script><scripttype="text/javascript">$(function(){$(document).ready(function(){Highcharts.setOptions({global:{useUTC:false}});varjsonData=document.getElementById("Hidden1").value;varchart;$('#container').highcharts({chart:{type:'spline',animation:Highcharts.svg,//don'tanimateinoldIEmarginRight:100,events:{load:function(){//setuptheupdatingofthecharteachsecondvarseries=this.series[0];setInterval(function(){varx=(newDate()).getTime(),//currenttime//y=Math.random();y=jsonData;//alert(jsonData);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:'Randomdata',data:(function(){//generateanarrayofrandomdatavardata=[],time=(newDate()).getTime(),i;for(i=-19;i<=0;i++){data.push({x:time+i*1000,y:Math.random()});}returndata;})()}]});});});//functiongetForm()//{//$.ajax({//type:"Post",////后台获取数据的函数,注意当对该页面重命名时,////必须手动更改该选项//url:"ts.aspx.cs/getData",//contentType:"application/json;charset=utf-8",//dataType:"json",//////成功获取数据//success:function(data){////data=jQuery.parseJSON(data.d);//JSON再次转换为Table形式;////varseries=chart.series[0];////series.addPoint([data[0]["x"],data[0]["y"]],false,true);////chart.redraw();//},//////显示错误//error:function(err){//alert(err+"调用后台程序出现错误,请尝试刷新!");//}//});//}//$(document).ready(function(){////每隔3秒自动调用方法,实现图表的实时更新//window.setInterval(getForm,2000);//});//});</script></head><body><formid="form1"runat="server"><divid="container"style="min-width:400px;height:400px;margin:0auto;"><inputid="Hidden1"runat="server"type="hidden"value=""/></div></form></body></html>这是CS的usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingSystem.Data.SqlClient;namespace动态图最终版{publicpartialclassts:System.Web.UI.Page{protectedstringjsonstr="";protectedvoidPage_Load(objectsender,EventArgse){Stringjsonstr=getData();Hidden1.Value=jsonstr;}publicstringgetData(){//stringsqlConnStr="DataSource=R410-05\HEPANBO;InitialCatalog=Test;IntegratedSecurity=True";stringsqlConnStr="DataSource=(local);InitialCatalog=aimosh;IntegratedSecurity=True";SqlConnectionsqlConn=newSqlConnection(sqlConnStr);sqlConn.Open();SqlCommandcom=newSqlCommand("selecttop1TMPTime,TMPPricefromTest",sqlConn);SqlDataAdapterda=newSqlDataAdapter(com);DataSetdtPrice=newDataSet();da.Fill(dtPrice);//DataTabledtPrice=SQLDBHelper.ExecuteTable("selecttop20TMPCode,TMPTime,TMPPricefromTabMerPricewith(nolock)orderbyTMPTimedesc");//stringjson="{"rows":[";stringjson="";for(inti=(dtPrice.Tables[0].Rows.Count-1);i>=0;i--){//json+="{"time1":""+(Convert.ToDateTime(dtPrice.Tables[0].Rows[i]["TMPTime"]).AddHours(-8)-newDateTime(1970,1,1)).TotalMilliseconds+"","price":""+(Convert.ToDecimal(dtPrice.Tables[0].Rows[i]["TMPPrice"])/200)+""},";//json+="{""+(Convert.ToDecimal(dtPrice.Tables[0].Rows[i]["TMPPrice"]))+""},";json+=(Convert.ToDecimal(dtPrice.Tables[0].Rows[i]["TMPPrice"]));}//json=json.TrimEnd(',');//json+="]}";jsonstr=json;//Response.Write(@"<scriptlanguage=JavaScript>{window.alert('数据库扣脸上了!');}</script>");returnjsonstr;}}}