/// <summary> /// 绘制有下划线的数字 /// </summary> /// <param name="paint">绘图对象</param> /// <param name="value">值</param> /// <param name="digit">保留小数位数</param> /// <param name="font">字体</param> /// <param name="fontColor">文字颜色</param> /// <param name="zeroAsEmpty">0是否为空</param> /// <param name="x">横坐标</param> /// <param name="y">纵坐标</param> /// <returns>绘制的横坐标</returns> public static int DrawUnderLineNum(Graphics g, double value, int digit, Font font, Color fontColor, bool zeroAsEmpty, int x, int y) { if (zeroAsEmpty && value == 0) { String text = "-"; Size size = g.MeasureString(text, font).ToSize(); DrawText(g, text, fontColor, font, x, y); return(size.Width); } else { String[] nbs = LbCommon.GetValueByDigit(value, digit, true).Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (nbs.Length == 1) { Size size = g.MeasureString(nbs[0], font).ToSize(); DrawText(g, nbs[0], fontColor, font, x, y); return(size.Width); } else { Size decimalSize = g.MeasureString(nbs[0], font).ToSize(); Size size = g.MeasureString(nbs[1], font).ToSize(); DrawText(g, nbs[0], fontColor, font, x, y); DrawText(g, nbs[1], fontColor, font, x + decimalSize.Width - 4, y); Pen pen = new Pen(fontColor); g.DrawLine(pen, x + decimalSize.Width, y + decimalSize.Height - 3, x + decimalSize.Width + size.Width - 7, y + decimalSize.Height - 3); pen.Dispose(); return(decimalSize.Width + size.Width - 7); } } }
/// <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(); }
/// <summary> /// 初始化控件 /// </summary> public void InitControl() { indicators.Clear(); RemoveAll(); if (minuteMode) { AllowDrag = false; DataSource.SetColsCapacity(20); IsMinute = true; AutoFillXScale = true; ScrollAddSpeed = true; XFieldText = "日期"; CanMoveShape = true; LeftYScaleWidth = 80; RightYScaleWidth = 80; XScalePixel = 21; mainDiv = AddChartDiv(70); mainDiv.ShowVGrid = true; MinuteLineMax = 0; MinuteLineMin = 0; mainDiv.LeftYScale.ScaleType = YScaleType.EqualRatio; mainDiv.RightYScale.ScaleType = YScaleType.EqualRatio; mainDiv.Title = "分时线"; mainDiv.XScale.Visible = false; List <double> scaleSteps = new List <double>(); scaleSteps.Add(LbCommon.GetDateNum(1970, 1, 1, 10, 0, 0, 0)); scaleSteps.Add(LbCommon.GetDateNum(1970, 1, 1, 10, 30, 0, 0)); scaleSteps.Add(LbCommon.GetDateNum(1970, 1, 1, 11, 0, 0, 0)); scaleSteps.Add(LbCommon.GetDateNum(1970, 1, 1, 11, 30, 0, 0)); scaleSteps.Add(LbCommon.GetDateNum(1970, 1, 1, 13, 30, 0, 0)); scaleSteps.Add(LbCommon.GetDateNum(1970, 1, 1, 14, 0, 0, 0)); scaleSteps.Add(LbCommon.GetDateNum(1970, 1, 1, 14, 30, 0, 0)); mainDiv.XScale.ScaleSteps = scaleSteps; mainDiv.PaddingBottom = 10; mainDiv.PaddingTop = 10; LineShape lineShape = AddLine("分时线", COLUMN_CLOSE, mainDiv); lineShape.LineColor = Color.White; //成交量 volumeDiv = AddChartDiv(30); volumeDiv.LeftYScale.Distance = 60; volumeDiv.Title = "成交量"; volumeDiv.RightYScale.Distance = 60; volumeDiv.XScale.ScaleSteps = scaleSteps; BarShape barShape = AddBar("成交量", COLUMN_VOLUME, volumeDiv); volumeDiv.XScale.Format = "hh:mm"; barShape.Title = ""; barShape.BarStyle = BarStyle.Line; SetBar("成交量", System.Drawing.Color.FromArgb(255, 255, 80), System.Drawing.Color.FromArgb(255, 255, 80)); if (!Program.BlackOrWhite) { mainDiv.ForeColor = Color.Black; mainDiv.BackColor = Color.White; mainDiv.LeftYScale.TipForeColor = Color.White; mainDiv.RightYScale.TipForeColor = Color.White; volumeDiv.LeftYScale.TipForeColor = Color.White; volumeDiv.RightYScale.TipForeColor = Color.White; volumeDiv.XScale.TipForeColor = Color.White; volumeDiv.BackColor = Color.White; mainDiv.BorderColor = Color.Black; volumeDiv.BorderColor = Color.Black; macdDiv.BorderColor = Color.Black; mainDiv.LeftYScale.ForeColor = Color.Black; mainDiv.RightYScale.ForeColor = Color.Black; mainDiv.LeftYScale.ScaleColor = Color.Black; mainDiv.RightYScale.ScaleColor = Color.Black; volumeDiv.LeftYScale.ForeColor = Color.Black; volumeDiv.RightYScale.ForeColor = Color.Black; volumeDiv.LeftYScale.ScaleColor = Color.Black; volumeDiv.RightYScale.ScaleColor = Color.Black; mainDiv.GridColor = Color.Black; volumeDiv.GridColor = Color.Black; lineShape.LineColor = Color.Black; barShape.UpColor = Color.Black; barShape.DownColor = Color.Black; volumeDiv.XScale.ForeColor = Color.Black; volumeDiv.XScale.ScaleColor = Color.Black; } } else { AllowDrag = true; DataSource.SetColsCapacity(20); IsMinute = false; AutoFillXScale = false; ScrollAddSpeed = true; XFieldText = "日期"; CanMoveShape = true; LeftYScaleWidth = 80; RightYScaleWidth = 80; XScalePixel = 11; mainDiv = AddChartDiv(60); mainDiv.Title = "日线"; mainDiv.XScale.Visible = false; mainDiv.PaddingBottom = 10; mainDiv.PaddingTop = 10; //mainDiv.LeftYScale.System = VScaleSystem.Logarithmic; mainDiv.RightYScale.ScaleType = YScaleType.Percent; CandleShape candleShape = AddCandle("K线", COLUMN_OPEN, COLUMN_HIGH, COLUMN_LOW, COLUMN_CLOSE, mainDiv); candleShape.UpColor = Color.FromArgb(255, 80, 80); candleShape.DownColor = Color.FromArgb(80, 255, 255); candleShape.CandleStyle = CandleStyle.CloseLine; candleShape.StyleField = CTableEx.AutoField; candleShape.ColorField = CTableEx.AutoField; dataSource.AddColumn(candleShape.StyleField); dataSource.AddColumn(candleShape.ColorField); IndicatorMovingAverage indBoll = (IndicatorMovingAverage)AddIndicator("MA"); indBoll.SetParam(COLUMN_CLOSE, mainDiv); //成交量 volumeDiv = AddChartDiv(15); volumeDiv.XScale.Visible = false; volumeDiv.LeftYScale.Magnitude = 10000; volumeDiv.RightYScale.Magnitude = 10000; volumeDiv.LeftYScale.Digit = 0; volumeDiv.RightYScale.Digit = 0; BarShape barShape = AddBar("成交量", COLUMN_VOLUME, volumeDiv); barShape.Title = "成交量"; barShape.BarStyle = BarStyle.Bar; barShape.Digit = 0; barShape.StyleField = CTableEx.AutoField; barShape.ColorField = CTableEx.AutoField; dataSource.AddColumn(barShape.StyleField); dataSource.AddColumn(barShape.ColorField); SetBar("成交量", System.Drawing.Color.FromArgb(255, 255, 80), System.Drawing.Color.FromArgb(125, 206, 235)); macdDiv = AddChartDiv(25); macdDiv.XScale.Format = "hh:mm"; div2Indicator = ChangeIndicator(indicatorName); indicators.Add(indBoll); indicators.Add(div2Indicator); if (!Program.BlackOrWhite) { mainDiv.LeftYScale.TipForeColor = Color.White; mainDiv.RightYScale.TipForeColor = Color.White; volumeDiv.LeftYScale.TipForeColor = Color.White; volumeDiv.RightYScale.TipForeColor = Color.White; mainDiv.ForeColor = Color.Black; mainDiv.BackColor = Color.White; volumeDiv.BackColor = Color.White; macdDiv.BackColor = Color.White; mainDiv.BorderColor = Color.Black; volumeDiv.BorderColor = Color.Black; macdDiv.BorderColor = Color.Black; mainDiv.LeftYScale.ForeColor = Color.Black; mainDiv.RightYScale.ForeColor = Color.Black; mainDiv.LeftYScale.ScaleColor = Color.Black; mainDiv.RightYScale.ScaleColor = Color.Black; volumeDiv.LeftYScale.ForeColor = Color.Black; volumeDiv.RightYScale.ForeColor = Color.Black; volumeDiv.LeftYScale.ScaleColor = Color.Black; volumeDiv.RightYScale.ScaleColor = Color.Black; macdDiv.LeftYScale.ForeColor = Color.Black; macdDiv.RightYScale.ForeColor = Color.Black; macdDiv.LeftYScale.ScaleColor = Color.Black; macdDiv.RightYScale.ScaleColor = Color.Black; macdDiv.XScale.ForeColor = Color.Black; macdDiv.XScale.ScaleColor = Color.Black; mainDiv.GridColor = Color.Black; volumeDiv.GridColor = Color.Black; macdDiv.GridColor = Color.Black; candleShape.UpColor = Color.Black; candleShape.DownColor = Color.Black; barShape.UpColor = Color.Black; barShape.DownColor = Color.Black; candleShape.HighTitleColor = Color.Black; candleShape.LowTitleColor = Color.Black; candleShape.CloseTitleColor = Color.Black; candleShape.OpenTitleColor = Color.Black; List <BaseShape> shapes = indBoll.GetShapeList(); for (int i = 0; i < shapes.Count; i++) { BaseShape bShape = shapes[i] as BaseShape; if (bShape is BarShape) { (bShape as BarShape).DownColor = Color.Black; (bShape as BarShape).UpColor = Color.Black; } else if (bShape is LineShape) { (bShape as LineShape).LineColor = Color.Black; } } } } }
/// <summary> /// 秒表方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, EventArgs e) { List <String> refreshCodes = new List <string>(); lock (newDataCodes) { if (newDataCodes.Count > 0) { foreach (String key in newDataCodes.Keys) { refreshCodes.Add(key); } newDataCodes.Clear(); } } int refreshCodesSize = refreshCodes.Count; if (refreshCodes.Count > 0) { if (updateCells.Count > 0) { if (updateCells.Count > 10) { for (int i = 0; i < updateCells.Count; i++) { GridCell updateCell = updateCells[i]; updateCell.Style.BackColor = Color.Empty; } } else { for (int i = 0; i < updateCells.Count; i++) { GridCell updateCell = updateCells[i]; updateCell.Style.BackColor = Color.Empty; } } updateCells.Clear(); } for (int i = 0; i < refreshCodesSize; i++) { GridRow row = null; String code = refreshCodes[i]; if (rowsMap.ContainsKey(code)) { row = rowsMap[code]; SecurityLatestData lastestData = new SecurityLatestData(); SecurityService.GetLatestData(code, ref lastestData); double lastClose = lastestData.m_lastClose; double diff = 0, diffRange = 0; diff = lastestData.m_close - lastestData.m_lastClose; if (lastestData.m_lastClose != 0) { diffRange = diff / lastestData.m_lastClose; } GridCell cell3 = row.GetCell("colP3"); if (lastestData.m_close != cell3.GetDouble()) { cell3.SetDouble(lastestData.m_close); cell3.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell3); } GridCell cell4 = row.GetCell("colP4"); if (diff != cell4.GetDouble()) { cell4.SetDouble(Convert.ToDouble(LbCommon.GetValueByDigit(diff, 2, true))); cell4.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell4); } GridCell cell5 = row.GetCell("colP5"); if (diffRange != cell5.GetDouble()) { cell5.SetDouble(diffRange); cell5.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell5); } GridCell cell6 = row.GetCell("colP6"); if (lastestData.m_high != cell6.GetDouble()) { cell6.SetDouble(lastestData.m_high); cell6.Style.TextColor = GetPriceColor(lastestData.m_high, lastClose); updateCells.Add(cell6); } GridCell cell7 = row.GetCell("colP7"); if (lastestData.m_low != cell7.GetDouble()) { cell7.SetDouble(lastestData.m_low); cell7.Style.TextColor = GetPriceColor(lastestData.m_low, lastClose); updateCells.Add(cell7); } GridCell cell8 = row.GetCell("colP8"); if (lastestData.m_open != cell8.GetDouble()) { cell8.SetDouble(lastestData.m_open); cell8.Style.TextColor = GetPriceColor(lastestData.m_open, lastClose); updateCells.Add(cell8); } GridCell cell9 = row.GetCell("colP9"); if (lastestData.m_volume != cell9.GetDouble()) { cell9.SetDouble(lastestData.m_volume); updateCells.Add(cell9); } GridCell cell10 = row.GetCell("colP10"); if (lastestData.m_amount != cell10.GetDouble()) { cell10.SetDouble(lastestData.m_amount); updateCells.Add(cell10); } GridCell cell11 = row.GetCell("colP11"); if (lastestData.m_buyPrice1 != cell11.GetDouble()) { cell11.SetDouble(lastestData.m_buyPrice1); cell11.Style.TextColor = GetPriceColor(lastestData.m_buyPrice1, lastClose); updateCells.Add(cell11); } GridCell cell12 = row.GetCell("colP12"); if (lastestData.m_buyVolume1 != cell12.GetDouble()) { cell12.SetDouble(lastestData.m_buyVolume1); updateCells.Add(cell12); } GridCell cell13 = row.GetCell("colP13"); if (lastestData.m_sellPrice1 != cell13.GetDouble()) { cell13.SetDouble(lastestData.m_sellPrice1); cell13.Style.TextColor = GetPriceColor(lastestData.m_sellPrice1, lastClose); updateCells.Add(cell13); } GridCell cell14 = row.GetCell("colP14"); if (lastestData.m_sellVolume1 != cell14.GetDouble()) { cell14.SetDouble(lastestData.m_sellVolume1); updateCells.Add(cell14); } } } if (updateCells.Count > 0) { for (int i = 0; i < updateCells.Count; i++) { updateCells[i].Style.BackColor = Color.FromArgb(50, 255, 255, 255); } UpdateGrid(); Invalidate(); } } }