/// <summary> /// 子类重写此函数时,必须设置Y轴宽度 /// </summary> /// <param name="axisCanvas">画布</param> /// <param name="seriesCollection">Series集合</param> /// <returns>Label的Y列表</returns> protected override List <double> PrimitiveDrawY(Canvas axisCanvas, ChartCollection <ISeries> seriesCollection) { this._labelTextSize = this.MeasureLabelTextSize(); axisCanvas.Width = base.CalculateAxisSize(this._labelTextSize.Width); this._axisData = this.CreateAxisData(seriesCollection); return(this.DrawY(axisCanvas)); }
private double CalculateYLabelStep(DateTimeAxisData axisData) { double labelStepMilliseconds = this._labelStep != null ? this._labelStep.Value.TotalMilliseconds : double.NaN; if (double.IsNaN(labelStepMilliseconds)) { int labelCount = (int)(this._axisCanvas.Height / ChartConstant.DEFAULT_STEP_SIZE); if (this._axisCanvas.Height % ChartConstant.DEFAULT_STEP_SIZE > ChartConstant.ZERO_D) { labelCount += 1; } if (labelCount == 0) { labelStepMilliseconds = axisData.Area.TotalMilliseconds; } else { TimeSpan intervalTimeLength = TimeSpan.FromMilliseconds(axisData.Area.TotalMilliseconds / labelCount);//一个刻度内时长 labelStepMilliseconds = this.AdjustIntervalTimeLength(intervalTimeLength); } } return(labelStepMilliseconds); }
private double CalculateXLabelStep(DateTimeAxisData axisData) { double labelStepMilliseconds = this._labelStep != null ? this._labelStep.Value.TotalMilliseconds : double.NaN; if (double.IsNaN(labelStepMilliseconds)) { const double INTERVAL = 20d; double labelTextSpace = INTERVAL + this._labelTextSize.Width * 1.5d; int count = (int)(this._axisCanvas.Width / labelTextSpace); if (count == 0) { labelStepMilliseconds = axisData.Area.TotalMilliseconds; } else { TimeSpan intervalTimeLength = TimeSpan.FromMilliseconds(axisData.Area.TotalMilliseconds / count);//一个刻度内时长 labelStepMilliseconds = this.AdjustIntervalTimeLength(intervalTimeLength); } } return(labelStepMilliseconds); }
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); }
/// <summary> /// 绘制X轴 /// </summary> /// <param name="axisCanvas">画布</param> /// <param name="seriesCollection">Series集合</param> /// <returns>Label的X列表</returns> protected override List <double> PrimitiveDrawX(Canvas axisCanvas, ChartCollection <ISeries> seriesCollection) { this._axisData = this.CreateAxisData(seriesCollection); return(this.DrawX(axisCanvas)); }
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); }