多语言展示
当前在线:759今日阅读:60今日分享:30

ASP.NET Chart如何生成蜡烛图

K线图(Candlestick Charts)又称蜡烛图、日本线、阴阳线、棒线、红黑线等,常用说法是“K线”。它是以每个分析周期的开盘价、最高价、最低价和收盘价绘制而成。后K线图因其细腻独到的标画方式而被引入到股市及期货市场。股市及期货市场中的K线图的画法包含四个数据,即开盘价、最高价、最低价、收盘价,所有的k线都是围绕这四个数据展开,反映大势的状况和价格信息。如果把每日的K线图放在一张纸上,就能得到日K线图,同样也可画出周K线图、月K线图。
工具/原料

vs

方法/步骤
1

打g工vs开发工具,新建一个web项目,然后再新建一个文件。

2

然后打开工具栏,拖动Chart。

3

绘制如下图所示的页面。详细代码如下:<%@ Register Assembly='TeeChart' Namespace='Steema.TeeChart.Web' TagPrefix='tchart' %>                           

   
       

            行情报价

       
           
               
                       
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                    
  •                                            
  •                
           
           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
       
   
   

4

设置控控件属性。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

5

增加后台相关代码

6

导入命名空间

7

增加控件处理方法

8

后台详细代码如下:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Web.UI.DataVisualization.Charting;using CMS.BLL;namespace CMS.Web.admin{    public partial class ProductK : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!this.IsPostBack)            {                PageRefresh('XAUUSD');            }        }        #region        protected void btnAU_Click(object sender, EventArgs e)        {            PageRefresh('XAUUSD');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnAG_Click(object sender, EventArgs e)        {            PageRefresh('XAGUSD');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnPt_Click(object sender, EventArgs e)        {            PageRefresh('XPTUSD');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnPd_Click(object sender, EventArgs e)        {            PageRefresh('XPDUSD');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnCopper_Click(object sender, EventArgs e)        {            PageRefresh('Copper');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnUk_Click(object sender, EventArgs e)        {            PageRefresh('UKOil');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnEu_Click(object sender, EventArgs e)        {            PageRefresh('EURUSD');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnGb_Click(object sender, EventArgs e)        {            PageRefresh('GBPUSD');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnEur_Click(object sender, EventArgs e)        {            PageRefresh('EURGBP');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnUs_Click(object sender, EventArgs e)        {            PageRefresh('USDJPY');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        protected void btnUsd_Click(object sender, EventArgs e)        {            PageRefresh('USDCHF');            ClientScript.RegisterStartupScript(GetType(), '', '');         }        #endregion              private void PageRefresh(string type)        {            this.cPu.ViewStateContent = SerializationContents.All;            this.cPu.EnableViewState = false;            Series series = cPu.Series[0];                         this.FillStockData(series, type);            if (type == 'EURGBP' ||type == 'EURUSD' || type == 'USDCHF' || type == 'EURUSD')            {                this.cPu.ChartAreas[0].AxisY.LabelStyle.Format = 'F4';            }            else if (type == 'GBPUSD')            {                this.cPu.ChartAreas[0].AxisY.LabelStyle.Format = 'F2';            }            else if (type == 'USDJPY')            {                this.cPu.ChartAreas[0].AxisY.LabelStyle.Format = 'F3';            }            else            {                this.cPu.ChartAreas[0].AxisY.LabelStyle.Format = 'F';            }            this.cPu.ChartAreas[0].AxisX.LabelStyle.Format = 'MM-dd';            this.cPu.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = false;            this.cPu.ChartAreas[0].AxisX.IsMarksNextToAxis = false;            this.cPu.ChartAreas[0].AxisY.LabelStyle.IsEndLabelVisible = false;            series.BorderWidth =1;            series.ShadowOffset = 1;         //  series.BackSecondaryColor = Color.Red;            series.Color = Color.Green;             this.cPu.ChartAreas[0].AxisX.Interval = 3;            cPu.ChartAreas[0].AxisX.LineWidth =1;         }        ///

        ///         ///         ///         ///         ///         /// 开始日期        /// 类别        private void FillStockData(Series series, string type)        {            DataTable dt = new ProductPrice().GetProductTypeList(type);            SProduct.TradeClient trade = new SProduct.TradeClient();            char splitStr = '\t';            string[] price = null;            string day = string.Empty;                        int i = 0;            DateTime date = DateTime.Now;            for (int n = dt.Rows.Count - 1; n >= 0; n--)            {                day = dt.Rows[n]['weektime'].ToString().Substring(4, 4);               day= day.Insert(2, '-');                series.Points.AddXY(day, Convert.ToDouble(dt.Rows[n]['highprice']));                series.Points[i].YValues[1] = Convert.ToDouble(dt.Rows[n]['lowprice']);                series.Points[i].YValues[2] = Convert.ToDouble(dt.Rows[n]['openprice']);                series.Points[i].YValues[3] = Convert.ToDouble(dt.Rows[n]['closeprice']);                i = i + 1;            }            try            {                price = trade.GetLastPillarData('D1', type).Split(splitStr);                // series.Points[i].XValue = cPu.Series[0].Points[i - 1].XValue + 1;                // 周期 \t 行情编码 \t 时间(yyyyMMddHHmm) \t开盘价 \t最高价 \t最低价\t 收盘价\t成交量                day = price[2].Substring(4, 4);                day = day.Insert(2, '-');                series.Points.AddXY(day, Convert.ToDouble(price[4]));//dt.Rows[n]['highprice']));                series.Points[i].YValues[1] = Convert.ToDouble(price[5]);// dt.Rows[n]['lowprice']);                series.Points[i].YValues[2] = Convert.ToDouble(price[3]);//dt.Rows[n]['openprice']);                series.Points[i].YValues[3] = Convert.ToDouble(price[6]);//dt.Rows[n]['closeprice']);            }            catch              {            }                   }    }}

9

运行程序

推荐信息