private double CalSegDurationSecond(XAxisPlotElementInfo axisX, Graphics graphics, double durationTimeMillisecond) { string labelText = this.GetLabelText(DateTime.Now, double.NaN); SizeF labelTextSize = graphics.MeasureString(labelText, axisX.Font); float labelTextMinWidth = labelTextSize.Width * 2; int maxLabelCount = (int)(axisX.Area.Width / labelTextMinWidth); double separatorSecond; double segDurationMillisecond = durationTimeMillisecond / maxLabelCount; if (segDurationMillisecond < 500) { separatorSecond = 0.5d; } else if (segDurationMillisecond < 1000) { separatorSecond = 1d; } else { separatorSecond = (int)Math.Ceiling(segDurationMillisecond / 1000); } return(separatorSecond); }
private void DrawTimeAxis(Graphics graphics, WavePlotPara plotPara) { XAxisPlotElementInfo axisX = this._xAxis; if (axisX == null) { return; } //时间图背景 RectangleF rectangleTimeArea = axisX.Area; graphics.FillRectangle(axisX.BackgroudColor, rectangleTimeArea); if (plotPara == null) { return; } //边框 //graphics.DrawLine(this._borderPen, rectangleTimeArea.X, rectangleTimeArea.Y, rectangleTimeArea.X, rectangleTimeArea.Y + rectangleTimeArea.Height); //float borderX = rectangleTimeArea.X + rectangleTimeArea.Width - this._borderPen.Width; //graphics.DrawLine(this._borderPen, borderX, rectangleTimeArea.Y, borderX, rectangleTimeArea.Y + rectangleTimeArea.Height); //graphics.DrawLine(this._borderPen, rectangleTimeArea.X, rectangleTimeArea.Y, rectangleTimeArea.X + rectangleTimeArea.Width, rectangleTimeArea.Y); //绘制起始时间-毫秒 double beginTimeMillisecond = plotPara.SBTOMillisecond; //绘制结束时间-毫秒 double endTimeMillisecond = plotPara.GetSETOMillisecond(); //绘制总时长 double durationTimeMillisecond = endTimeMillisecond - beginTimeMillisecond; if (durationTimeMillisecond <= 0d) { return; } //绘制时间间隔 double separatorSecond = this.CalSegDurationSecond(axisX, graphics, durationTimeMillisecond); //绘制起始时间-秒 double beginTimeSecond = beginTimeMillisecond / 1000; //绘制时间-秒 double timeSecond = PlotConstant.ZEROR_D; if (beginTimeMillisecond > PlotConstant.ZEROR_D) { int mult = (int)(beginTimeSecond / separatorSecond); double mod = beginTimeSecond % separatorSecond; if (mod - separatorSecond < PlotConstant.ZEROR_D) { mult = mult + 1; } timeSecond = separatorSecond * mult; } //绘制时间 DateTime time = plotPara.BaseTime.AddSeconds(timeSecond); DateTime endTime = plotPara.BaseTime.AddMilliseconds(endTimeMillisecond); float labelHeight = rectangleTimeArea.Height / 3; float y1 = rectangleTimeArea.Y + rectangleTimeArea.Height; float y2 = y1 - labelHeight; float y3 = y1 - labelHeight / 2; float labelX, labelSmallX; double separatorSecondHalf = separatorSecond / 2; float secondLength = (float)(axisX.Area.Width / (durationTimeMillisecond / 1000)); string labelText; SizeF labelTextSize; float labelTextX, lastLabelTextRightX = 0f; float labelTextY = rectangleTimeArea.Y + labelHeight / 2; AxisLabelLocation labelTextLocation = AxisLabelLocation.First; var contentWidth = axisX.Area.Width; Pen pen = axisX.Pen; while (true) { //刻度-x labelX = (float)(secondLength * (timeSecond - beginTimeSecond)); labelSmallX = (float)(secondLength * (timeSecond - beginTimeSecond - separatorSecondHalf)); //绘制刻度文本 labelText = this.GetLabelText(time, separatorSecond); labelTextSize = graphics.MeasureString(labelText, axisX.Font); labelTextX = labelX - labelTextSize.Width / 2; switch (labelTextLocation) { case AxisLabelLocation.First: if (labelTextX < PlotConstant.ZEROR_D && beginTimeMillisecond <= PlotConstant.ZEROR_D) { labelTextX = (float)PlotConstant.ZEROR_D; } labelTextLocation = AxisLabelLocation.Middle; break; case AxisLabelLocation.Middle: if (labelTextX + labelTextSize.Width - contentWidth > PlotConstant.ZEROR_D) { labelTextX = contentWidth - labelTextSize.Width; } break; case AxisLabelLocation.Last: if (labelTextX + labelTextSize.Width - contentWidth > PlotConstant.ZEROR_D) { labelTextX = contentWidth - labelTextSize.Width; if (labelTextX - lastLabelTextRightX < PlotConstant.ZEROR_D) { if (labelSmallX - contentWidth < PlotConstant.ZEROR_D) { graphics.DrawLine(pen, labelSmallX, y1, labelSmallX, y3); } return; } } break; default: throw new NotImplementedException(); } graphics.DrawString(labelText, axisX.Font, axisX.ForeColor, labelTextX, labelTextY); lastLabelTextRightX = labelTextX + labelTextSize.Width; //绘制刻度 if (labelSmallX > PlotConstant.ZEROR_D) { graphics.DrawLine(pen, labelSmallX, y1, labelSmallX, y3); } if (labelX - contentWidth < PlotConstant.ZEROR_D) { graphics.DrawLine(pen, labelX, y1, labelX, y2); } if (labelTextLocation == AxisLabelLocation.Last) { break; } timeSecond += separatorSecond; time = plotPara.BaseTime.AddSeconds(timeSecond); if (time >= endTime) { time = endTime; labelTextLocation = AxisLabelLocation.Last; } } }