private void SetTooltip() { var size = UITextHelper.MeasureTextSize(this._textBlock); if (size.Width > this.ActualWidth) { this._textBlock.ToolTip = this._textBlock.Text; } else { this._textBlock.ToolTip = null; } }
/// <summary> /// 当文本长度超出TextBox范围时,设置ToolTip /// </summary> /// <param name="textBox">TextBox</param> /// <param name="tooltip">tooltip</param> /// <param name="pre">误差值</param> public static void SetTextBoxToolTip(TextBox textBox, string tooltip, double pre = 5d) { if (double.IsInfinity(textBox.MaxWidth)) { throw new ArgumentException("TextBox未设置MaxWidth值"); } double width = UITextHelper.MeasureTextSize(textBox).Width; if (textBox.MaxWidth - width < pre) { textBox.ToolTip = tooltip; } else { textBox.ToolTip = null; } }
/// <summary> /// 测量标签文本大小 /// </summary> /// <param name="axis">坐标轴</param> /// <param name="labelText">标签文本</param> /// <returns>标签文本大小</returns> public static Size MeasureLabelTextSize(AxisAbs axis, string labelText) { if (_measureTextLabel == null) { _measureTextLabel = new TextBlock(); } TextBlock measureTextLabel = _measureTextLabel; measureTextLabel.Text = labelText; if (axis.LabelStyle == null) { measureTextLabel.Style = ChartStyleHelper.GetAxisLabelStyle(axis.DockOrientation); } else { measureTextLabel.Style = axis.LabelStyle; } var size = UITextHelper.MeasureTextSize(measureTextLabel); measureTextLabel.Style = null; return(size); }
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> 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> 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); }