asp.net 制作K线图——highstock

highcharts是一款非常不错的开源的js绘图工具,我们所需要做的就是用他能听懂的语言告诉他怎么做,剩下的事情就交给highcharts了。

废话不多说步入正题。

1.从highcharts官网下载Highstock.js

2.在自己的网站引用jquery.min.js和highstock.js(jquery.min.js要放在highstock前面)

3.前台网站代码编写,直接贴代码


    
        股票走势图
    
    
    
    
    
    


    

4.后台数据获取

新建一个axsh文件

代码如下

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

using System.Web.Script.Serialization;
using System.Text;
namespace HighCharts_test.JsonData
{
    /// 
    /// $codebehindclassname$ 的摘要说明
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class StockData : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/javascript";
            string callback = context.Request.QueryString["callback"];

            StringBuilder json = new StringBuilder();
            json.Append(callback);
           
            DataSet ds = GetStockData("600379");
            json.Append(DataSetToJson(ds));
            
            //json = serializer.Serialize(ds);
            context.Response.Write(json);
            
        }
        public string GetUNIX(string dateTimeString)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            DateTime dtNow = DateTime.Parse(dateTimeString);
            TimeSpan toNow = dtNow.Subtract(dtStart);
            string timeStamp = toNow.Ticks.ToString();
            timeStamp = timeStamp.Substring(0, timeStamp.Length - 4);
            return timeStamp;
        }
        /// 
        /// 将DataSet转化成JSON数据
        /// 
        /// 
        /// 
        public StringBuilder DataSetToJson(DataSet ds)
        {
            string json = string.Empty;
            try
            {
                if (ds.Tables.Count == 0)
                    throw new Exception("DataSet中Tables为0");
                json = "([";
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    json += "[";
                    for (int n = 0; n < ds.Tables[0].Columns.Count; n++)
                    {
                        if (n==0)
                        {
                            json +=GetUNIX( ds.Tables[0].Rows[i][n].ToString()) + ",";
                        }
                        else
                        {
                            json += ds.Tables[0].Rows[i][n].ToString() + ",";
                        }
                    }
                    json = json.Substring(0, json.Length - 1);
                    
                    json += "],";
                   
                }
                json = json.Substring(0, json.Length - 1);
                json += "]);";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(json);
            return sb;
        } 
        
        public DataSet GetStockData(string stockId)
        {
            //string sql = "select Cdate,KPJ,ZGJ,ZDJ,SPJ,JYL from CompanyStockDate where StockId=" + stockId + " and Cdate>=DATEADD(YY,-1,GETDATE()) order by Cdate asc";
            string sql = "select Cdate,KPJ,ZGJ,ZDJ,SPJ,JYL from CompanyStockDate where StockId=" + stockId + " and Cdate>=DATEADD(m,-3,GETDATE()) order by Cdate asc";
            DataSet ds = SqlHelper.Query(sql);
            return ds;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
效果如下

asp.net 制作K线图——highstock_第1张图片

你可能感兴趣的:(.net,B/S)