public void Start(IndicatorInfo info) { if (info.VisibleBarCount < 1) { return; } float high, low, close, open; Graphics gdi = info.GDI; Color fg = info.Style.Foreground; string format = "f" + info.ChartView.Digits; using (Brush brushfgFont = new SolidBrush(fg)) using (Brush brushInvfgFont = new SolidBrush(Color.FromArgb(255, 255 - fg.R, 255 - fg.G, 255 - fg.B))) using (Pen penGrid = new Pen(info.Style.Grid)) using (Pen penBarup = new Pen(info.Style.Barup)) using (Brush brushBarup = new SolidBrush(info.Style.Bullcandle)) using (Pen penBardown = new Pen(info.Style.Bardown)) using (Pen penLine = new Pen(info.Style.Linegraph)) using (Brush brushBardown = new SolidBrush(info.Style.Bearcandle)) { int dx = info.RightMargin - info.OffsetShift - 1; close = info.GetYPos(info.Bars[0].Close); gdi.DrawLine(penGrid, new PointF(info.LeftMargin + 1, close), new PointF(info.RightMargin + 1, close)); float height = info.StrSize.Height; gdi.FillRectangle(brushfgFont, (float)(info.RightMargin + 2), close - height / 2, (float)info.ChartWidth - info.RightMargin - 3, height); gdi.DrawString(info.Bars[0].Close.ToString(format), info.ChartFont, brushInvfgFont, info.RightMargin + 6, close - height / 2 - 0.5f); for (int i = info.ScrollBarCount; i < info.VisibleBarCount; i++) { dx -= info.BarWidth + info.BarPadding; high = info.GetYPos(info.Bars[i].High); low = info.GetYPos(info.Bars[i].Low); close = info.GetYPos(info.Bars[i].Close); open = info.GetYPos(info.Bars[i].Open); switch (info.Properties.Chartsticks) { case Chartsticks.Candlesticks: if (info.Bars[i].Close > info.Bars[i].Open) { gdi.DrawLine(penBarup, new PointF(dx - info.BarCenter, high), new PointF(dx - info.BarCenter, low)); gdi.FillRectangle(brushBarup, (float)(dx - info.BarWidth), close, (float)info.BarWidth, open - close); gdi.DrawRectangle(penBarup, (float)(dx - info.BarWidth), close, (float)info.BarWidth, open - close); } else if (info.Bars[i].Close < info.Bars[i].Open) { gdi.DrawLine(penBardown, new PointF(dx - info.BarCenter, high), new PointF(dx - info.BarCenter, low)); gdi.FillRectangle(brushBardown, (float)(dx - info.BarWidth), open, (float)info.BarWidth, close - open); gdi.DrawRectangle(penBardown, (float)(dx - info.BarWidth), open, (float)info.BarWidth, close - open); } else { gdi.DrawLine(penLine, new PointF(dx - info.BarCenter, high), new PointF(dx - info.BarCenter, low)); gdi.DrawLine(penLine, new PointF(dx - info.BarWidth, open), new PointF(dx, open)); gdi.DrawRectangle(penLine, (float)(dx - info.BarWidth), close, (float)info.BarWidth, open - close); } break; case Chartsticks.LineChart: break; } } } }
protected override void OnPaint(PaintEventArgs e) { ChartProperties properties = this.ChartView.Properties; ChartStyle style = this.ChartView.Style; Graphics g = e.Graphics; g.Clear(style.Background); //SizeF strSize = g.MeasureString((1m).ToString("f" + ChartView.Digits), ChartFont); SizeF strSize = new SizeF(this.ChartView.SymbolWidth, 14); Rectangle borderRect = new Rectangle( BorderLeft, BorderTop, this.Width - BorderLeft - (12 + (int)strSize.Width) - 1, this.Height - BorderTop - BorderBottom - 1); //画图表边框 using (Pen penFg = new Pen(style.Foreground)) g.DrawRectangle(penFg, borderRect); int offsetShift = 0; if (properties.ChartShift) { offsetShift = ChartView.ChartShift; } const float figureWidth = 9.1f; //画平移图表图标 float x = borderRect.Right - figureWidth - 1 - offsetShift; float y = borderRect.Top + 1; using (Brush brush = new SolidBrush(Color.FromArgb(255, 127, 137, 149))) { g.FillPolygon(brush, new PointF[] { new PointF(x, y), new PointF(x + figureWidth, y), new PointF(x + 4.5f, y + 5.0f) }); } List <Bar> bars = this.ChartView.Bars; int barPadding = BarPaddings[this.ChartView.Zoom - 1]; int barWidth = BarWidths[this.ChartView.Zoom - 1]; int nVisibleWidth = (borderRect.Width - offsetShift - barWidth - barPadding); //视图可见柱数 int nVisibleBarsCounter = nVisibleWidth / (barWidth + barPadding); if (nVisibleBarsCounter < 1) { return; } int nScrollBarCount = this.ChartView.ScrollBarCount; //计算最高最低 decimal highest = decimal.MinValue, lowest = decimal.MaxValue; int nVisibleBarCount = nVisibleBarsCounter + nScrollBarCount; if (nVisibleBarCount > bars.Count) { nVisibleBarCount = bars.Count; } if (nScrollBarCount >= nVisibleBarCount) { return; } if (nVisibleBarCount == 0) { return; } for (int i = nScrollBarCount; i < nVisibleBarCount; i++) { if (bars[i].High > highest) { highest = bars[i].High; } if (bars[i].Low < lowest) { lowest = bars[i].Low; } } const int tickPadding = 32, TickTop = 14, tickBottom = 4; decimal Point = (decimal)Math.Pow(10, -this.ChartView.Digits); //扩展最高最低空间 int remainder = Remainders[this.ChartView.Digits]; int Power = (int)Math.Pow(10, this.ChartView.Digits); decimal dtmp = highest * Power; decimal mMax = (dtmp + remainder - dtmp % remainder) * Point; dtmp = lowest * Power; decimal mMin = (dtmp - dtmp % remainder) * Point; //decimal pMax = decimal.Round(highest + Point * 99, 3); //decimal pMin = decimal.Round(lowest - Point * 99, 3); float centerH = strSize.Height / 2.0f; int tickCount = (borderRect.Height - TickTop - tickBottom) / tickPadding; if (tickCount == 0) { return; } decimal perTick = decimal.Round((mMax - mMin) / (decimal)tickCount, 4); if (perTick == 0m) { perTick = 0.0001m; } int tickHeight = borderRect.Bottom - tickBottom; info = new IndicatorInfo(this.ChartView, mMin, perTick) { Properties = properties, Style = style, BarCenter = BarCenters[ChartView.Zoom - 1], BarPadding = barPadding, VisibleBarCount = nVisibleBarCount, VisibleBarsCounter = nVisibleBarsCounter, BarWidth = barWidth, GDI = g, OffsetShift = offsetShift, LeftMargin = BorderLeft, RightMargin = borderRect.Right, ChartWidth = this.Width, ScrollBarCount = nScrollBarCount, TickHeight = tickHeight, TickPadding = tickPadding, ChartFont = ChartFont, StrSize = strSize }; //画刻度尺 string format = "f" + this.ChartView.Digits; using (Pen penFg = new Pen(style.Foreground)) { using (Brush brFg = new SolidBrush(style.Foreground)) { int yTick; decimal mTick; for (int nTick = 0; nTick <= tickCount; nTick++) { yTick = tickHeight - nTick * tickPadding; mTick = (nTick * perTick + mMin); g.DrawLine(penFg, borderRect.Right, yTick, borderRect.Right + 5, yTick); g.DrawString(mTick.ToString(format), ChartFont, brFg, borderRect.Right + 6, yTick - centerH); } } } indicator.Start(info); if (this.dropChart == null) { this.dropChart = new DropChart(this); this.MouseMove += this.dropChart.OnMouseMove; this.MouseDown += this.dropChart.OnMouseDown; this.MouseUp += this.dropChart.OnMouseUp; } }