private void DrawObj(DrawingContext dc, double highest, double lowest, DrawObject obj) { switch (obj.Type) { case DrawObjectType.Line: drawLine(dc, highest, lowest, obj); break; case DrawObjectType.zVLines: DrawZeroVerticalLines(dc, highest, lowest, obj); break; case DrawObjectType.CandleLine: DrawCandleLine(dc, highest, lowest, obj); break; case DrawObjectType.vLines: DrawVLines(dc, highest, lowest, obj); break; } }
//绘制以0为基准的垂线,类似MACD线 private void DrawZeroVerticalLines(DrawingContext dc, double highest, double lowest, DrawObject obj) { if (obj.Type != DrawObjectType.zVLines) { return; } for (var i = 0; i < drawItemCount; ++i) { var itemIndex = drawItemStartIndex + i; var xoffset = (int)(i * (itemWidth + itemSpace)) + 0.5 + ChartStartX; var yoffset = ChartHeight - (int)((obj.Vals[itemIndex] + highest) / (2 * highest) * ChartHeight) + 0.5 + ChartStartY; if (itemIndex > 0) { if (obj.Vals[itemIndex] * obj.Vals[itemIndex - 1] < 0) //正负交汇处画粗线 { if (obj.Vals[itemIndex - 1] < 0) { Pen gpen = getPen(255, 0, 0, 1); //绿色 gpen.Thickness = 2.5; dc.DrawLine(gpen, new Point(xoffset, ChartStartY + ChartHeight / 2 - 4), new Point(xoffset, ChartStartY + ChartHeight / 2 + 4)); } if (obj.Vals[itemIndex - 1] > 0) { Pen rpen = getPen(0, 128, 0, 1); //红色 rpen.Thickness = 2.5; dc.DrawLine(rpen, new Point(xoffset, ChartStartY + ChartHeight / 2 - 4), new Point(xoffset, ChartStartY + ChartHeight / 2 + 4)); } continue; } } if (obj.Vals[itemIndex] > 0) { Pen gpen = getPen(255, 0, 0, 1); //绿色 gpen.Thickness = 1; dc.DrawLine(gpen, new Point(xoffset, ChartStartY + ChartHeight / 2 + 0.5), new Point(xoffset, yoffset)); } else { Pen rpen = getPen(0, 128, 0, 1); //红色 rpen.Thickness = 1; dc.DrawLine(rpen, new Point(xoffset, ChartStartY + ChartHeight / 2 + 0.5), new Point(xoffset, yoffset)); } } }
//绘制Candle线 private void DrawCandleLine(DrawingContext dc, double highest, double lowest, DrawObject obj) { var opens = obj.Vals; var closes = obj.Vals1; var highs = obj.Vals2; var lows = obj.Vals3; if (opens == null || closes == null || highs == null || lows == null || opens.Length == 0 || opens.Length != closes.Length || closes.Length != highs.Length || highs.Length != lows.Length) { return; } //绘制可视区域的K线 for (int i = 0; i < drawItemCount; ++i) { int itemIndex = drawItemStartIndex + i; double xoffset = (int)(i * (itemWidth + itemSpace)) + 0.5 + ChartStartX; double yTop = (int)((highest - highs[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY; double yBottom = (int)((highest - lows[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY; double yOpen = (int)((highest - opens[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY; double yClose = (int)((highest - closes[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY; double bodyBottom = yOpen; double bodyTop = yClose; Color bodyColor = new Color(); bodyColor.R = 255; bodyColor.G = 0; bodyColor.B = 0; if (opens[itemIndex] > closes[itemIndex]) { bodyTop = yOpen; bodyBottom = yClose; bodyColor.R = 0; bodyColor.G = 128; bodyColor.B = 0; } var pen = getPen(0, 0, 0, 1); var brush = getBrush(bodyColor.R, bodyColor.G, bodyColor.B); //draw top vertical line dc.DrawLine(pen, new Point(xoffset + itemWidth / 2, yTop), new Point(xoffset + itemWidth / 2, bodyTop)); //draw kline body double bodyHeight = bodyBottom - bodyTop; dc.DrawRectangle(brush, pen, new Rect(xoffset, bodyTop, itemWidth, bodyHeight)); //draw bottom line. dc.DrawLine(pen, new Point(xoffset + itemWidth / 2, bodyBottom), new Point(xoffset + itemWidth / 2, yBottom)); } }