owchart证券图形控件学习:如何使用画线交易?
源码:
namespace WinFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Chart chart = new Chart(); //创建K线 chart.Dock = DockStyle.Fill; //设置填充 Controls.Add(chart); //添加控件 //设置显示左轴的宽度 chart.LeftYScaleWidth = 100; //设置K线的间隔 chart.XScalePixel = 20; //添加第一个层 ChartDiv div1 = chart.AddChartDiv(70); //开盘价=0,最高价=1,最低价=2,收盘价=3 int openField = 0; int highField = 1; int lowField = 2; int closeField = 3; //添加K线 CandleShape candleShape = chart.AddCandle("K线", openField, highField, lowField, closeField, div1); //添加第二个层 ChartDiv div2 = chart.AddChartDiv(30); //声明成交量字段 int volField = 4; //添加K线 BarShape barShape = chart.AddBar("成交量", volField, div2); div1.XScale.Visible = false; //获取数据源 CTableEx dataSource = chart.DataSource; candleShape.StyleField = CTableEx.AutoField; candleShape.ColorField = CTableEx.AutoField; dataSource.AddColumn(candleShape.StyleField); dataSource.AddColumn(candleShape.ColorField); //读取文件,并填充数据 String filePath = Application.StartupPath + "\\SH600000.txt"; String content = File.ReadAllText(filePath, Encoding.Default); String[] strs = content.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; //获取起始索引 int startIndex = dataSource.RowsCount; for (int i = 2; i < strs.Length - 1; i++) { String str = strs[i]; String[] subStrs = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); double date = (Convert.ToDateTime(subStrs[0]) - new DateTime(1970, 1, 1)).TotalSeconds; double open = Convert.ToDouble(subStrs[1]); double high = Convert.ToDouble(subStrs[2]); double low = Convert.ToDouble(subStrs[3]); double close = Convert.ToDouble(subStrs[4]); double volume = Convert.ToDouble(subStrs[5]); double amount = Convert.ToDouble(subStrs[6]); dataSource.Set(date, openField, open); dataSource.Set(date, highField, high); dataSource.Set(date, lowField, low); dataSource.Set(date, closeField, close); dataSource.Set(date, volField, volume); } //刷新图像 chart.RefreshGraph(); List<TradeLine> tradeLines = new List<TradeLine>(); //添加多头画线 TradeLine tradeLine = new TradeLine(); tradeLine.Bs = "多头"; Point mp = new Point(100, 100); AddPlot(chart, tradeLine, System.Drawing.Color.FromArgb(200, 200, 200), System.Drawing.Color.White, mp, 1, null, div1); tradeLines.Add(tradeLine); //添加多头画线 TradeLine tradeLine2 = new TradeLine(); tradeLine2.Bs = "空头"; Point mp2 = new Point(100, 200); AddPlot(chart, tradeLine2, System.Drawing.Color.FromArgb(200, 200, 200), System.Drawing.Color.White, mp2, 1, null, div1); tradeLines.Add(tradeLine2); //监控价格 Random rd = new Random(); double latest = dataSource.Get2(dataSource.RowsCount - 1, closeField) + (double)(rd.Next(100) - 100) / 100; for (int i = 0; i < tradeLines.Count; i++) { TradeLine trendLine = tradeLines[i]; if (trendLine.Bs == "多头") { if (latest <= trendLine.Value) { trendLine.Bs += "已成交"; } } else if (trendLine.Bs == "空头") { if (latest >= trendLine.Value) { trendLine.Bs += "已成交"; } } } } /// <summary> /// 追加画线 /// </summary> /// <param name="chart">K线</param> /// <param name="plot">画线</param> /// <param name="lineColor">颜色</param> /// <param name="type">画线工具类型</param> /// <param name="mp">初始坐标</param> /// <param name="lineWidth">宽度</param> /// <param name="dashPattern">线的类型</param> /// <param name="divID">图层ID</param> /// <returns></returns> public static void AddPlot(Chart chart, PlotBase plot, Color lineColor, Color selectedColor, Point mp, int lineWidth, float[] dashPattern, ChartDiv chartDiv) { CTableEx dataSource = chart.DataSource; if (dataSource.RowsCount >= 2) { double value = chart.GetValueByPoint(chartDiv, mp, AttachYScale.Left); if (plot != null) { plot.Chart = chart; plot.ChartDiv = chartDiv; plot.LineColor = lineColor; plot.SelectedColor = selectedColor; plot.Selected = true; plot.LineWidth = lineWidth; plot.Style = dashPattern; plot.ZOrder = LbCommon.SerialNumber; bool flag = plot.InitPlot(mp); if (flag) { chartDiv.PlotList.Add(plot); chart.SelectedPlot = plot; plot.StartMove(); } } } } } /// <summary> /// 画线交易线 /// </summary> public class TradeLine : PlotHorizontal { private String bs = "多头"; /// <summary> /// 获取或设置多空方向 /// </summary> public String Bs { get { return bs; } set { bs = value; } } /// <summary> /// 文字的字体 /// </summary> private Font wordFont = LbCommon.GetDefaultFont(); public Font WordFont { get { return wordFont; } set { wordFont = value; } } /// <summary> /// 获取或设置值 /// </summary> public double Value { get { return PointList[0].Value; } set { PlotMark mark = new PlotMark(PointList[0].Index, PointList[0].Key, value); PointList[0] = mark; } } /// <summary> /// 画线 /// </summary> public override void DrawPlot(Graphics g, List<PlotBase.PlotMark> pList, Color curColor) { if (pList.Count == 0) { return; } ChartDiv div = ChartDiv; float y1 = Chart.GetY(div, pList[0].Value, AttachYScale.Left) - div.DisplayRectangle.Y - div.TitleHeight; Brush pBrush = new SolidBrush(curColor); Pen pPen = new Pen(pBrush); pPen.Width = LineWidth; if (Style != null) { pPen.DashPattern = Style; } g.DrawLine(pPen, 0, y1, Chart.GetWorkSpaceX(), y1); String str = bs + " " + LbCommon.GetValueByDigit(pList[0].Value, 2, true); SizeF sizeF = g.MeasureString(str, wordFont); g.DrawString(str, wordFont, pBrush, new PointF((float)Chart.GetWorkSpaceX() - sizeF.Width, y1 - sizeF.Height)); pPen.Dispose(); pBrush.Dispose(); } } }
界面: