private List <double> DrawYAxisBottomToTop(Canvas axisCanvas, NumberAxisData axisData) { List <double> yList = new List <double>(); double axisHeight = axisCanvas.Height; double labelStep = this.CalculateLabelStep(axisData.Area, axisHeight); double labelStepSize = ChartHelper.CalculateLabelStepSize(axisData.Area, axisHeight, labelStep); double labelTextLineInterval = base.GetAxisYLabelTextLineInterval(); double bottom = ChartConstant.ZERO_D, bottom2; double value = axisData.MinValue; double y = axisHeight; TextBlock label; Size labelSize; double labelWidth = ChartConstant.ZERO_D; AxisLabelLocation labelTextLocation = AxisLabelLocation.First; bool addLabelControl; double lastLabelY = axisHeight; while (true) { label = ChartHelper.CreateLabelControl(this, this.CreateAxisText(value)); labelSize = UITextHelper.MeasureTextSize(label); if (labelSize.Width - labelWidth > base._PRE) { labelWidth = labelSize.Width; } if (axisHeight > labelSize.Height) { addLabelControl = false; switch (labelTextLocation) { case AxisLabelLocation.First: axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetBottom(label, bottom); lastLabelY = axisHeight - labelSize.Height; labelTextLocation = AxisLabelLocation.Middle; break; case AxisLabelLocation.Middle: bottom2 = bottom - labelSize.Height / 2; if (bottom2 > ChartConstant.ZERO_D) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetBottom(label, bottom2); lastLabelY = bottom2 - labelSize.Height; } break; case AxisLabelLocation.Last: if (lastLabelY - labelSize.Height > ChartConstant.ZERO_D) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetTop(label, ChartConstant.ZERO_D); } break; default: throw new NotImplementedException(); } if (addLabelControl) { if (base.IsAxisYLeft()) { Canvas.SetRight(label, labelTextLineInterval); } else { Canvas.SetLeft(label, labelTextLineInterval); } } } if (labelTextLocation == AxisLabelLocation.Last) { if (this._showLastLabel) { yList.Add(y); } break; } yList.Add(y); value += labelStep; if (value >= axisData.MaxValue) { labelStepSize = (labelStep - (value - axisData.MaxValue)) * labelStepSize / labelStep; double labelHeight = ChartHelper.MeasureLabelTextSize(this, axisData.MaxValue.ToString()).Height + 10d; if (labelStepSize < labelHeight) { break; } value = axisData.MaxValue; y = ChartConstant.ZERO_D; labelTextLocation = AxisLabelLocation.Last; } else { y -= labelStepSize; } bottom += labelStepSize; } axisCanvas.Width = base.CalculateAxisSize(labelWidth); return(yList); }
private List <double> DrawXAxisLeftToRight(Canvas axisCanvas, NumberAxisData axisData) { List <double> xList = new List <double>(); double axisWidth = axisCanvas.Width; double labelStep = this.CalculateLabelStep(axisData.Area, axisWidth); double labelStepSize = ChartHelper.CalculateLabelStepSize(axisData.Area, axisWidth, labelStep); double left = ChartConstant.ZERO_D; double lastLabelX = ChartConstant.ZERO_D; double x = ChartConstant.ZERO_D; double value = axisData.MinValue; TextBlock label; Size labelSize; AxisLabelLocation labelTextLocation = AxisLabelLocation.First; bool addLabelControl; double offset; while (true) { label = ChartHelper.CreateLabelControl(this, this.CreateAxisText(value)); labelSize = UITextHelper.MeasureTextSize(label); if (axisWidth - labelSize.Width > ChartConstant.LABEL_TEXT_INTERVAL) { addLabelControl = false; switch (labelTextLocation) { case AxisLabelLocation.First: axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetLeft(label, left); lastLabelX = left + labelSize.Width; labelTextLocation = AxisLabelLocation.Middle; break; case AxisLabelLocation.Middle: left += labelStepSize; offset = left - labelSize.Width / 2 - lastLabelX; if (offset >= ChartConstant.LABEL_TEXT_INTERVAL) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetLeft(label, left - labelSize.Width / 2); lastLabelX = left + labelSize.Width; } else if (offset > 0) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetLeft(label, left - labelSize.Width / 2 + offset); lastLabelX = left + labelSize.Width + offset; } break; case AxisLabelLocation.Last: if (lastLabelX + labelSize.Width + ChartConstant.LABEL_TEXT_INTERVAL <= axisWidth) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetRight(label, ChartConstant.ZERO_D); lastLabelX = axisWidth; } break; default: throw new NotImplementedException(); } if (addLabelControl) { if (base.IsAxisXBottom()) { Canvas.SetBottom(label, ChartConstant.LABEL_TEXT_INTERVAL); } else { Canvas.SetTop(label, ChartConstant.LABEL_TEXT_INTERVAL); } } } if (labelTextLocation == AxisLabelLocation.Last) { if (this._showLastLabel) { xList.Add(x); } break; } xList.Add(x); value += labelStep; if (value >= axisData.MaxValue) //if (tmp - axisData.MaxValue > _PRE) { value = axisData.MaxValue; x = axisWidth; labelTextLocation = AxisLabelLocation.Last; } else { x += labelStepSize; } } return(xList); }
private List <double> DrawY(Canvas axisCanvas) { double labelStepSize = this.CalculateLabelStepSize(this._axisData.Count, axisCanvas.Height); double columnSeriesOffset = this.CalculateSeriesSize(this._seriesSizeDic, labelStepSize); TextBlock label; Size labelTextSize; double top = 0d, labelTextOffset; AxisLabelLocation labelLocation = AxisLabelLocation.First; object labelObj; LabelSeriesItem labelItem; double y; List <double> yList = new List <double>(); double labelStepSizeHalf = labelStepSize / 2; for (int i = 0; i < this._axisData.Count; i++) { labelObj = this._axisData.ElementAt(i).Key; labelItem = this._axisData.ElementAt(i).Value; label = ChartHelper.CreateLabelControl(this, this.CreateAxisText(labelObj)); labelTextSize = UITextHelper.MeasureTextSize(label); axisCanvas.Children.Add(label); labelTextOffset = (labelStepSize - labelTextSize.Height) / 2; Canvas.SetTop(label, top + labelTextOffset); if (this.IsAxisYLeft()) { Canvas.SetRight(label, ChartConstant.LABEL_TEXT_INTERVAL); } else { Canvas.SetLeft(label, ChartConstant.LABEL_TEXT_INTERVAL); } switch (labelLocation) { case AxisLabelLocation.First: y = columnSeriesOffset; labelLocation = AxisLabelLocation.Middle; break; case AxisLabelLocation.Middle: case AxisLabelLocation.Last: y = top + columnSeriesOffset; break; default: throw new NotImplementedException(labelLocation.ToString()); } foreach (var key in labelItem.Keys.ToArray()) { labelItem[key] = y; y += labelItem.Series.Size; } yList.Add(top + labelStepSizeHalf); top += labelStepSize; } ChartHelper.DrawYAxisLabelLine(this, axisCanvas, ChartConstant.ZERO_D, axisCanvas.Height); return(yList); }
private List <double> DrawX(Canvas axisCanvas) { double labelStepSize = this.CalculateLabelStepSize(this._axisData.Count, axisCanvas.Width); double columnSeriesOffset = this.CalculateSeriesSize(this._seriesSizeDic, labelStepSize); TextBlock label; var angleQuadrantInfo = new AngleQuadrantInfo(this._angle, base._PRE); Size labelTextSize; double left = 0d, labelTextOffset; AxisLabelLocation labelLocation = AxisLabelLocation.First; object labelObj; LabelSeriesItem labelItem; List <double> xList = new List <double>(); double labelStepSizeHalf = labelStepSize / 2; double x; for (int i = 0; i < this._axisData.Count; i++) { labelObj = this._axisData.ElementAt(i).Key; labelItem = this._axisData.ElementAt(i).Value; label = ChartHelper.CreateLabelControl(this, this.CreateAxisText(labelObj)); labelTextSize = UITextHelper.MeasureTextSize(label); axisCanvas.Children.Add(label); labelTextOffset = (labelStepSize - labelTextSize.Width) / 2; Canvas.SetLeft(label, left + labelTextOffset); if (this.IsAxisXBottom()) { Canvas.SetTop(label, ChartConstant.LABEL_TEXT_INTERVAL); this.RotateLabelBottom(label, angleQuadrantInfo, labelTextSize); } else { Canvas.SetBottom(label, ChartConstant.LABEL_TEXT_INTERVAL); this.RotateLabelTop(label, angleQuadrantInfo, labelTextSize); } switch (labelLocation) { case AxisLabelLocation.First: x = columnSeriesOffset; labelLocation = AxisLabelLocation.Middle; break; case AxisLabelLocation.Middle: case AxisLabelLocation.Last: x = left + columnSeriesOffset; break; default: throw new NotImplementedException(labelLocation.ToString()); } foreach (var key in labelItem.Keys.ToArray()) { labelItem[key] = x; x += labelItem.Series.Size; } xList.Add(left + labelStepSizeHalf); left += labelStepSize; } ChartHelper.DrawXAxisLabelLine(this, axisCanvas, ChartConstant.ZERO_D, axisCanvas.Height); return(xList); }
private List <double> DrawXAxisLeftToRight(Canvas axisCanvas, DateTimeAxisData axisData, double labelStepMilliseconds, double labelStepSize) { List <double> xList = new List <double>(); double axisWidth = axisCanvas.Width; double left = ChartConstant.ZERO_D; double lastLabelX = ChartConstant.ZERO_D; DateTime time = axisData.MinValue; AxisLabelLocation labelTextLocation = AxisLabelLocation.First; double labelTextWidth = this._labelTextSize.Width; double labelTextWidthHalf = labelTextWidth / 2; double offset = labelTextWidth / 2; TextBlock label; bool addLabelControl; double x = left; while (true) { label = ChartHelper.CreateLabelControl(this, this.CreateAxisText(time)); if (axisWidth - labelTextWidth > ChartConstant.LABEL_TEXT_INTERVAL) { addLabelControl = false; switch (labelTextLocation) { case AxisLabelLocation.First: this._axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetLeft(label, left); lastLabelX = left + labelTextWidth; labelTextLocation = AxisLabelLocation.Middle; break; case AxisLabelLocation.Middle: left += labelStepSize; offset = left - labelTextWidthHalf - lastLabelX; if (offset >= ChartConstant.LABEL_TEXT_INTERVAL) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetLeft(label, left - labelTextWidthHalf); lastLabelX = left + labelTextWidth; } else if (offset > 0) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetLeft(label, left - labelTextWidthHalf + offset); lastLabelX = left + labelTextWidth + offset; } break; case AxisLabelLocation.Last: if (lastLabelX + labelTextWidth + ChartConstant.LABEL_TEXT_INTERVAL <= axisWidth) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetRight(label, ChartConstant.ZERO_D); lastLabelX = axisWidth; } break; default: throw new NotImplementedException(); } if (addLabelControl) { if (base.IsAxisXBottom()) { Canvas.SetBottom(label, ChartConstant.LABEL_TEXT_INTERVAL); } else { Canvas.SetTop(label, ChartConstant.LABEL_TEXT_INTERVAL); } } } if (labelTextLocation == AxisLabelLocation.Last) { if (this._showLastLabel) { xList.Add(x); } break; } xList.Add(x); time = time.AddMilliseconds(labelStepMilliseconds); if (time >= axisData.MaxValue) { x = this._axisCanvas.Width; time = axisData.MaxValue; labelTextLocation = AxisLabelLocation.Last; } else { x += labelStepSize; } } return(xList); }
private List <double> DrawYAxisBottomToTop(Canvas axisCanvas, DateTimeAxisData axisData, double labelStepMilliseconds, double labelStepSize) { List <double> yList = new List <double>(); double axisHeight = axisCanvas.Height; double bottom = ChartConstant.ZERO_D, bottom2; DateTime time = axisData.MinValue; double y = axisHeight; TextBlock label; Size labelSize = this._labelTextSize; double heightHalf = labelSize.Height / 2; AxisLabelLocation labelLocation = AxisLabelLocation.First; bool addLabelControl; double lastLabelY = axisHeight; while (true) { label = ChartHelper.CreateLabelControl(this, this.CreateAxisText(time)); if (axisHeight > labelSize.Height) { addLabelControl = false; switch (labelLocation) { case AxisLabelLocation.First: axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetBottom(label, bottom); lastLabelY = axisHeight - labelSize.Height; labelLocation = AxisLabelLocation.Middle; break; case AxisLabelLocation.Middle: bottom2 = bottom - heightHalf; if (bottom2 > ChartConstant.ZERO_D) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetBottom(label, bottom2); lastLabelY = bottom2 - labelSize.Height; } break; case AxisLabelLocation.Last: if (lastLabelY - labelSize.Height > ChartConstant.ZERO_D) { axisCanvas.Children.Add(label); addLabelControl = true; Canvas.SetTop(label, ChartConstant.ZERO_D); } break; default: throw new NotImplementedException(labelLocation.ToString()); } if (addLabelControl) { if (base.IsAxisYLeft()) { Canvas.SetLeft(label, ChartConstant.LABEL_TEXT_INTERVAL); } else { Canvas.SetRight(label, ChartConstant.LABEL_TEXT_INTERVAL); } } } if (labelLocation == AxisLabelLocation.Last) { if (this._showLastLabel) { yList.Add(y); } break; } yList.Add(y); time = time.AddMilliseconds(labelStepMilliseconds); if (time >= axisData.MaxValue) { labelStepSize = (labelStepMilliseconds - (time - axisData.MaxValue).TotalMilliseconds) * labelStepSize / labelStepMilliseconds; double labelHeight = ChartHelper.MeasureLabelTextSize(this, axisData.MaxValue.ToString()).Height + 10d; if (labelStepSize < labelHeight) { break; } time = axisData.MaxValue; y = ChartConstant.ZERO_D; labelLocation = AxisLabelLocation.Last; } else { y -= labelStepSize; } bottom += labelStepSize; } return(yList); }
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; } } }