protected unsafe override bool TextLayout(textpara_t* text, byte** fontpath) { TextBlock label = new TextBlock(); label.Inlines.Add(text->Text); label.FontFamily = new FontFamily(text->FontName); label.FontSize = text->fontsize; switch ((char)text->just) { case 'l': label.TextAlignment = TextAlignment.Left; break; case 'n': label.TextAlignment = TextAlignment.Center; break; case 'r': label.TextAlignment = TextAlignment.Right; break; } label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); Size desiredSize = label.DesiredSize; text->width = desiredSize.Width; text->height = desiredSize.Height; text->yoffset_centerline = 3; return true; }
public PostInfoDisplay(double textRightEdge, double viewRightEdge, Geometry newTextGeometry, string body) { if (brush == null) { brush = new SolidColorBrush(Color.FromArgb(0x20, 0x48, 0x3d, 0x8b)); brush.Freeze(); Brush penBrush = new SolidColorBrush(Colors.DarkSlateBlue); penBrush.Freeze(); solidPen = new Pen(penBrush, 0.5); solidPen.Freeze(); dashPen = new Pen(penBrush, 0.5); dashPen.DashStyle = DashStyles.Dash; dashPen.Freeze(); } this.textGeometry = newTextGeometry; TextBlock tb = new TextBlock(); tb.Text = "Blog Entry: " + body; const int MarginWidth = 8; this.postGrid = new Grid(); this.postGrid.RowDefinitions.Add(new RowDefinition()); this.postGrid.RowDefinitions.Add(new RowDefinition()); ColumnDefinition cEdge = new ColumnDefinition(); cEdge.Width = new GridLength(MarginWidth); ColumnDefinition cEdge2 = new ColumnDefinition(); cEdge2.Width = new GridLength(MarginWidth); this.postGrid.ColumnDefinitions.Add(cEdge); this.postGrid.ColumnDefinitions.Add(new ColumnDefinition()); this.postGrid.ColumnDefinitions.Add(cEdge2); System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle(); rect.RadiusX = 6; rect.RadiusY = 3; rect.Fill = brush; rect.Stroke = Brushes.DarkSlateBlue; Size inf = new Size(double.PositiveInfinity, double.PositiveInfinity); tb.Measure(inf); this.postGrid.Width = tb.DesiredSize.Width + 2 * MarginWidth; Grid.SetColumn(rect, 0); Grid.SetRow(rect, 0); Grid.SetRowSpan(rect, 1); Grid.SetColumnSpan(rect, 3); Grid.SetRow(tb, 0); Grid.SetColumn(tb, 1); this.postGrid.Children.Add(rect); this.postGrid.Children.Add(tb); Canvas.SetLeft(this.postGrid, Math.Max(viewRightEdge - this.postGrid.Width - 20.0, textRightEdge + 20.0)); Canvas.SetTop(this.postGrid, textGeometry.GetRenderBounds(solidPen).Top); this.Children.Add(this.postGrid); }
/// <summary> /// Sets the tooltip to the TextBlock only if the entire text does not /// fit in the available space (i.e. the TextBlock shows Ellipis at the end) /// </summary> /// <param name="textBlock">TextBlock object</param> private static void SetTooltip(TextBlock textBlock) { if (textBlock == null) return; textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); var width = textBlock.DesiredSize.Width; ToolTipService.SetToolTip(textBlock, textBlock.ActualWidth < width ? textBlock.Text : null); }
/// <summary> /// Assigns the ToolTip for the given TextBlock based on whether the text is trimmed /// </summary> private static void ComputeAutoTooltip(TextBlock textBlock) { textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); var width = textBlock.DesiredSize.Width; if (textBlock.ActualWidth < width) { ToolTipService.SetToolTip(textBlock, textBlock.Text); } else { ToolTipService.SetToolTip(textBlock, null); } }
private static void ComputeAutoToolTip(TextBlock textBlock) { // It is necessary to call Measure so that the DesiredSize gets updated. textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); var desiredWidth = textBlock.DesiredSize.Width; if (textBlock.ActualWidth < desiredWidth) { ToolTipService.SetToolTip(textBlock, textBlock.Text); } else { ToolTipService.SetToolTip(textBlock, null); } }
public static Size MeasureString(string s, int offset) { if (string.IsNullOrEmpty(s)) { return new Size(0, 0); } var textBlock = new TextBlock() { Text = s, FontSize = 16 }; textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); return new Size(textBlock.DesiredSize.Width + offset, textBlock.DesiredSize.Height); }
public static double CalculateLeftOffset(this ICanvasRange range) { double offset = 0; for (double dy = range.Ymin; dy < range.Ymax; dy += range.YTick) { TextBlock tb = new TextBlock { Text = dy.ToString(), TextAlignment = TextAlignment.Right }; tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); if (offset < tb.DesiredSize.Width) offset = tb.DesiredSize.Width; } return offset + 5; }
public static BitmapSource StringToBitmapSource(this string str, int fontSize, System.Windows.Media.Color foreground, System.Windows.Media.Color background) { TextBlock tbX = new TextBlock(); tbX.FontFamily = new System.Windows.Media.FontFamily("Consolas"); tbX.Foreground = new System.Windows.Media.SolidColorBrush(foreground); tbX.Background = new System.Windows.Media.SolidColorBrush(background); tbX.TextAlignment = TextAlignment.Center; tbX.FontSize = fontSize; tbX.FontStretch = FontStretches.Normal; tbX.FontWeight = FontWeights.Medium; tbX.Text = str; var size = tbX.MeasureString(); tbX.Width = size.Width; tbX.Height = size.Height; tbX.Measure(new Size(size.Width, size.Height)); tbX.Arrange(new Rect(new Size(size.Width, size.Height))); return tbX.ToBitmapSource(); }
private static void ComputeAutoTooltip(TextBlock textBlock) { textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); double width = textBlock.DesiredSize.Width; if (textBlock.ActualWidth < width) { var toolBlock = new TextBlock() { Text = textBlock.Text, FontSize = GetAutoTooltipFontSize(textBlock) }; ToolTipService.SetToolTip(textBlock, toolBlock); } else { ToolTipService.SetToolTip(textBlock, null); } }
private Size InternalGetSize(char c, double fontSize, bool bold, bool italic) { var @event = new AutoResetEvent(false); var size = new Size(); ((Action)(() => { var textBlock = new TextBlock { Text = Convert.ToString(c), FontSize = fontSize, FontFamily = _fontFamily, FontStyle = italic ? FontStyles.Italic : FontStyles.Normal, FontWeight = bold ? FontWeights.Bold : FontWeights.Normal }; textBlock.Measure(new Size(1024.0, 1024.0)); size = new Size(textBlock.ActualWidth, textBlock.ActualHeight); @event.Set(); })).OnUIThread(); @event.WaitOne(); return size; }
public static void GenerateXLabels(this ICanvasRange range, double leftOffset) { for (double dx = range.Xmin; dx <= range.Xmax; dx += range.XTick) { Point pt = range.NormalizePoint(new Point(dx, range.Ymin)); Line tick = new Line { Stroke = Brushes.Black, X1 = pt.X, Y1 = pt.Y, X2 = pt.X, Y2 = pt.Y - 5 }; range.ChartCanvas.Children.Add(tick); TextBlock tb = new TextBlock { Text = dx.ToString() }; tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); range.TextCanvas.Children.Add(tb); Canvas.SetLeft(tb, leftOffset + pt.X - tb.DesiredSize.Width / 2); Canvas.SetTop(tb, pt.Y + 2 + tb.DesiredSize.Height / 2); } }
private string GetAdaptedText(string text, string filler, double height, double width) { bool isAnyChanged = false; var textBlock = new System.Windows.Controls.TextBlock { Text = text, TextWrapping = TxtContent.TextWrapping, FontSize = FontSize, FontFamily = FontFamily, FontStretch = FontStretch, FontStyle = FontStyle, FontWeight = FontWeight, }; textBlock.Measure(new Size(width, Double.PositiveInfinity)); textBlock.Arrange(new Rect(textBlock.DesiredSize)); while (text.Length > 1 && (textBlock.ActualHeight > height || textBlock.ActualWidth > width)) { isAnyChanged = true; text = text.Remove(text.Length - 1); textBlock = new System.Windows.Controls.TextBlock { Text = text + filler, TextWrapping = TextWrapping, FontSize = FontSize, FontFamily = FontFamily, FontStretch = FontStretch, FontStyle = FontStyle, FontWeight = FontWeight, }; textBlock.Measure(new Size(width, Double.PositiveInfinity)); textBlock.Arrange(new Rect(textBlock.DesiredSize)); } return(isAnyChanged ? (text + filler) : text); }
/// <summary> /// Aprēķina vajadzību pēc uzpeldošā padoma un piešķir to teksta elementam <param name="textBlock"/>. /// </summary> private static void ComputeTrimAndShowToolTip(TextBlock textBlock) { if (textBlock.Text == string.Empty) { textBlock.ToolTip=null; return; } // Neesošs teksts mēdz izmērities lielāks, nekā aizpildīts, tāpēc šeit novērš tukšos taisnstūrus. textBlock.Measure(new Size(textBlock.TextWrapping == TextWrapping.Wrap ? textBlock.ActualWidth:Double.PositiveInfinity, Double.PositiveInfinity)); double measuredDimension, actualDimension; if (textBlock.TextWrapping == TextWrapping.Wrap) { measuredDimension=textBlock.DesiredSize.Height; actualDimension=textBlock.ActualHeight; } else { measuredDimension=textBlock.DesiredSize.Width; actualDimension=textBlock.ActualWidth; } if (actualDimension < measuredDimension) { // Garāku tekstu rāda ilgāk, lai paspēj izlasīt. ToolTipService.SetShowDuration(textBlock, Math.Min(textBlock.Text.Length*200, 30000)); textBlock.ToolTip=new ToolTip { Content=textBlock.Text, Style=GetTrimAndShowToolTip(textBlock) }; } else { ToolTipService.SetShowDuration(textBlock, 5000); textBlock.ToolTip=null; } }
private void PrintFixedDocument() { PrintDialog dialog; if (LoadPrintDialog(out dialog)) { var document = new FixedDocument(); var page = new FixedPage(); var t = new TextBlock { FontFamily = font, FontSize = kFontSize, Text = Data.ToString() }; page.Children.Add(t); t.Measure(new Size(document.DocumentPaginator.PageSize.Width, document.DocumentPaginator.PageSize.Width)); page.Height = t.DesiredSize.Height; page.Width = t.DesiredSize.Width; var p = new PageContent { Child = page }; document.Pages.Add(p); Print(document, dialog); } }
private void cmdPrint_Click(object sender, RoutedEventArgs e) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { // Create the text. Run run = new Run("This is a test of the printing functionality in the Windows Presentation Foundation."); // Wrap it in a TextBlock. TextBlock visual = new TextBlock(run); visual.Margin = new Thickness(15); // Allow wrapping to fit the page width. visual.TextWrapping = TextWrapping.Wrap; // Scale the TextBlock in both dimensions. double zoom; if (Double.TryParse(txtScale.Text, out zoom)) { visual.LayoutTransform = new ScaleTransform(zoom / 100, zoom / 100); // Get the size of the page. Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight); // Trigger the sizing of the element. visual.Measure(pageSize); visual.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height)); // Print the element. printDialog.PrintVisual(visual, "A Scaled Drawing"); } else { MessageBox.Show("Invalid scale value."); } } }
void SetUpTextBoxForApproxNodeBoundaries() { textBoxForApproxNodeBoundaries = new TextBlock { Text = "Fox jumping over River", FontFamily = new FontFamily(Label.DefaultFontName), FontSize = Label.DefaultFontSize, }; textBoxForApproxNodeBoundaries.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); textBoxForApproxNodeBoundaries.Width = textBoxForApproxNodeBoundaries.DesiredSize.Width; textBoxForApproxNodeBoundaries.Height = textBoxForApproxNodeBoundaries.DesiredSize.Height; }
/// <summary> /// Measures the text. /// </summary> /// <param name="text">The text.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <returns>The text size.</returns> public OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (string.IsNullOrEmpty(text)) { return OxySize.Empty; } var tb = new TextBlock { Text = text }; if (fontFamily != null) { tb.FontFamily = new FontFamily(fontFamily); } if (fontSize > 0) { tb.FontSize = fontSize; } tb.FontWeight = GetFontWeight(fontWeight); tb.Measure(new Size(1000, 1000)); return new OxySize(tb.ActualWidth, tb.ActualHeight); }
/// <summary> /// Draws the text. /// </summary> /// <param name="p">The position.</param> /// <param name="text">The text.</param> /// <param name="fill">The fill color.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <param name="rotate">The rotation angle.</param> /// <param name="halign">The horizontal alignment.</param> /// <param name="valign">The vertical alignment.</param> /// <param name="maxSize">The maximum size of the text.</param> public void DrawText( ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, OxyPlot.HorizontalAlignment halign, OxyPlot.VerticalAlignment valign, OxySize? maxSize) { var tb = new TextBlock { Text = text, Foreground = new SolidColorBrush(fill.ToColor()) }; // tb.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Animated); if (fontFamily != null) { tb.FontFamily = new FontFamily(fontFamily); } if (fontSize > 0) { tb.FontSize = fontSize; } tb.FontWeight = GetFontWeight(fontWeight); tb.Measure(new Size(1000, 1000)); var size = new Size(tb.ActualWidth, tb.ActualHeight); if (maxSize != null) { if (size.Width > maxSize.Value.Width) { size.Width = maxSize.Value.Width; } if (size.Height > maxSize.Value.Height) { size.Height = maxSize.Value.Height; } tb.Clip = new RectangleGeometry { Rect = new Rect(0, 0, size.Width, size.Height) }; } double dx = 0; if (halign == OxyPlot.HorizontalAlignment.Center) { dx = -size.Width / 2; } if (halign == OxyPlot.HorizontalAlignment.Right) { dx = -size.Width; } double dy = 0; if (valign == OxyPlot.VerticalAlignment.Middle) { dy = -size.Height / 2; } if (valign == OxyPlot.VerticalAlignment.Bottom) { dy = -size.Height; } var transform = new TransformGroup(); transform.Children.Add(new TranslateTransform { X = (int)dx, Y = (int)dy }); if (!rotate.Equals(0)) { transform.Children.Add(new RotateTransform { Angle = rotate }); } transform.Children.Add(new TranslateTransform { X = (int)p.X, Y = (int)p.Y }); tb.RenderTransform = transform; this.ApplyTooltip(tb); if (this.clip) { // add a clipping container that is not rotated var c = new Canvas(); c.Children.Add(tb); this.Add(c); } else { this.Add(tb); } }
private void UpdateYPlots() { var diviser = 1d; var step = double.MaxValue; var nextstep = double.MaxValue; var minstep = 50; while (nextstep >= minstep) { step = nextstep; diviser *= 2; nextstep = CharInnertHeight / diviser; } for (var y = CharInnertHeight - step; y >= -6; y -= step) { var line = new Line() { Stroke = Brushes.Black, X1 = _marginLeft - 5, X2 = _marginLeft, Y1 = y + _marginTop, Y2 = y + _marginTop }; _Canvas.Children.Add(line); _temporaryElements.Add(line); var dottedLine = new Line() { Stroke = Brushes.DarkGray, StrokeDashArray = { 1, 2 }, X1 = _marginLeft, X2 = ChartInnertWidth + _marginLeft, Y1 = y + _marginTop, Y2 = y + _marginTop }; _Canvas.Children.Add(dottedLine); _temporaryElements.Add(dottedLine); var speedToPrint = FormatSpeed((CharInnertHeight - y) * _maxBps / CharInnertHeight, true); var text = new TextBlock() { Text = speedToPrint, FontSize = 9, FontWeight = FontWeights.Bold }; _Canvas.Children.Add(text); text.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); var textHeight = text.DesiredSize.Height; var textWidth = text.DesiredSize.Width; text.Margin = new Thickness(_marginLeft - textWidth - 10, y + _marginTop - textHeight / 2 - 1, 0, 0); _temporaryElements.Add(text); } }
private void UpdateTimePlots() { var offset = GetOverflowOffset(); var pixelsPerSecond = ChartInnertWidth * 1000d / _chartLength; var overflowOffsetSeconds = offset / pixelsPerSecond; var stepSeconds = 1; var step = 0d; var minstep = 70; // Find the step in seconds corresponding to the minimal desired minstep pixel space while (step < minstep) { step = pixelsPerSecond * stepSeconds; stepSeconds += 1; } // loop step by step from 0 to chartlength for (var timePos = 0; timePos <= (_chartLength / 1000) + overflowOffsetSeconds; timePos += stepSeconds) { var x = (timePos * pixelsPerSecond) + _marginLeft - offset; // Add the plot line as a temporary element var line = new Line() { Stroke = Brushes.Black, X1 = x, X2 = x, Y1 = CharInnertHeight + _marginTop, Y2 = CharInnertHeight + 4 + _marginTop }; _Canvas.Children.Add(line); _temporaryElements.Add(line); // If we are in the chart inner visible space, draw a vertical dotted line // and add it as a temporary element if (x > _marginLeft) { var dottedLine = new Line() { Stroke = Brushes.LightGray, StrokeDashArray = { 1, 1 }, X1 = x, X2 = x, Y1 = ChartInnerBottomY, Y2 = _marginTop }; _Canvas.Children.Add(dottedLine); _temporaryElements.Add(dottedLine); } var timeToPrint = GraphStartTime.AddSeconds(timePos).ToString("HH:mm:ss"); // draw the time text // and add it as a temporary element var text = new TextBlock() { Text = timeToPrint, FontSize = 10 }; var isFirst = timePos == 0 && offset == 0; if (isFirst) text.FontWeight = FontWeights.Bold; _Canvas.Children.Add(text); _temporaryElements.Add(text); // center the text beside the plot text.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); var textWidth = text.DesiredSize.Width; text.Margin = new Thickness(x - (textWidth / 2), isFirst ? CharInnertHeight + _marginTop + 4 : CharInnertHeight + _marginTop + 6, 0, 0); } }
/// <summary> /// Draws the chart without data, just the frame /// </summary> public void ApplyStyle() { double tolerance = 0.001d; Point pt; Line gridLine, tick; double dx, dy; TextBlock tb = new TextBlock(); Size size = new Size(); Canvas.SetLeft(this.chartCanvas, leftOffset); Canvas.SetBottom(this.chartCanvas, bottomOffset); this.chartCanvas.Width = Math.Abs(this.textCanvas.Width - leftOffset - rightOffset); this.chartCanvas.Height = Math.Abs(this.textCanvas.Height - bottomOffset); Rectangle chartRect = new Rectangle(); chartRect.Stroke = Brushes.Black; chartRect.Fill = Brushes.White; chartRect.Width = this.chartCanvas.Width; chartRect.Height = this.chartCanvas.Height; this.chartCanvas.Children.Add(chartRect); // Create vertical gridlines: if (this.drawXGrid) { for (dx = this.xmin + this.xtickInitDelta; dx + tolerance < this.xmax; dx += this.xtickDelta) { gridLine = ChartsHelper.CreateLine(this.lineColor, this.gridLineThickness, this.gridLinePattern); pt = this.NormalizePoint(new Point(dx, this.ymin)); gridLine.X1 = pt.X; gridLine.Y1 = pt.Y; pt = this.NormalizePoint(new Point(dx, this.ymax)); gridLine.X2 = pt.X; gridLine.Y2 = pt.Y; this.chartCanvas.Children.Add(gridLine); } } // Create horizontal gridlines: if (this.drawYGrid) { for (dy = this.ymin + this.ytickInitDelta; dy + tolerance < this.ymax; dy += this.ytickDelta) { gridLine = ChartsHelper.CreateLine(this.lineColor, this.gridLineThickness, this.gridLinePattern); pt = this.NormalizePoint(new Point(this.xmin, dy)); gridLine.X1 = pt.X; gridLine.Y1 = pt.Y; pt = this.NormalizePoint(new Point(this.xmax, dy)); gridLine.X2 = pt.X; gridLine.Y2 = pt.Y; this.chartCanvas.Children.Add(gridLine); } } // Create x-axis tick marks if (this.drawXTicks) { for (dx = this.xmin + this.xtickInitDelta; dx + tolerance < this.xmax; dx += this.xtickDelta) { pt = NormalizePoint(new Point(dx, this.ymin)); tick = ChartsHelper.CreateLine(this.lineColor, this.tickLineThickness, LinePatternEnum.Solid); tick.X1 = pt.X; tick.Y1 = pt.Y; tick.X2 = pt.X; tick.Y2 = pt.Y - this.tickSize; this.chartCanvas.Children.Add(tick); tb = new TextBlock(); tb.Text = Math.Round(dx, 3).ToString(); tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); size = tb.DesiredSize; this.textCanvas.Children.Add(tb); Canvas.SetLeft(tb, this.leftOffset + pt.X - size.Width / 2.0d); Canvas.SetTop(tb, pt.Y + size.Height / 2.0d); } } // Create y-axis tick marks if (this.drawYTicks) { for (dy = this.ymin + this.ytickInitDelta; dy + tolerance < this.ymax; dy += this.ytickDelta) { pt = NormalizePoint(new Point(this.xmin, dy)); tick = ChartsHelper.CreateLine(this.lineColor, this.tickLineThickness, LinePatternEnum.Solid); tick.X1 = pt.X; tick.Y1 = pt.Y; tick.X2 = pt.X + this.tickSize; tick.Y2 = pt.Y; this.chartCanvas.Children.Add(tick); tb = new TextBlock(); tb.Text = Math.Round(dy, 3).ToString(); tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); size = tb.DesiredSize; this.textCanvas.Children.Add(tb); Canvas.SetRight(tb, this.chartCanvas.Width + 10.0d); Canvas.SetTop(tb, pt.Y - size.Height / 2.0d); } } }
/// <summary> /// Measures the child elements of a <see cref="T:System.Windows.Controls.Canvas"/> in anticipation of arranging them during the /// <see cref="M:System.Windows.Controls.Canvas.ArrangeOverride(System.Windows.Size)"/> /// pass. /// </summary> /// <param name="constraint"> /// An upper limit <see cref="T:System.Windows.Size"/> that should not be exceeded. /// </param> /// <returns> /// A <see cref="T:System.Windows.Size"/> that represents the size that is required to arrange child content. /// </returns> protected override Size MeasureOverride(Size constraint) { var size = base.MeasureOverride(constraint); var maxWidth = this.GetTickLabels().Max( c => { var tb = new TextBlock(new Run(c)); tb.Measure(constraint); return tb.DesiredSize.Width; }); size.Width = maxWidth + this.BarWidth + this.TickLength + this.Padding.Left + this.Padding.Right + this.TextMargin; return size; }
public void DrawLegend(Canvas canvas) { TextBlock tb = new TextBlock(); if (this.lines.Count < 1) return; double legendWidth = 0.0d; Size size = new Size(0.0d, 0.0d); foreach (LineData l in this.lines) { tb = new TextBlock(); tb.Text = l.Name; tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); size = tb.DesiredSize; if (legendWidth < size.Width) legendWidth = size.Width; } legendWidth += 20.0d; canvas.Width = legendWidth + 5.0d; double legendHeight = 25.0d * this.lines.Count; double sx = 5.0d; double sy = 0.0d; double textHeight = size.Height; double lineLength = 34.0d; Rectangle legendRect = new Rectangle(); legendRect.Stroke = Brushes.Black; legendRect.Fill = Brushes.White; legendRect.Width = legendWidth + 18.0d; legendRect.Height = legendHeight; canvas.Children.Add(legendRect); Rectangle rect; int n = 1; foreach (LineData l in this.lines) { double xText = 2.0d * sx + lineLength; double yText = n * sy + (2.0d * n - 1.0d) * textHeight / 2.0d; rect = new Rectangle(); rect.Stroke = Brushes.Black; rect.StrokeThickness = this.strokeThickness; rect.Fill = l.Color; rect.Width = 10.0d; rect.Height = 10.0d; Canvas.SetLeft(rect, sx + lineLength / 2.0d - 15.0d); Canvas.SetTop(rect, yText - 2.0d); canvas.Children.Add(rect); tb = new TextBlock(); tb.Text = l.Name; canvas.Children.Add(tb); Canvas.SetTop(tb, yText - size.Height / 2.0d + 3.0d); Canvas.SetLeft(tb, xText - 20.0d); n++; } canvas.Width = legendRect.Width; canvas.Height = legendRect.Height; }
/// <summary> /// Updates the visuals. /// </summary> protected override void AddVisuals() { if (this.Categories == null || this.Categories.Count == 0 || this.ColorScheme == null) { return; } base.AddVisuals(); for (int i = 0; i < this.Categories.Count; i++) { var text = this.Categories[i]; var tb = new TextBlock(new Run(text)) { Foreground = this.Foreground }; tb.Measure(new Size(this.ActualWidth, this.ActualHeight)); double y = this.ColorArea.Top + (((double)i / this.Categories.Count) * this.ColorArea.Height); double y1 = this.ColorArea.Top + (((i + 0.5) / this.Categories.Count) * this.ColorArea.Height); double y2 = this.ColorArea.Top + (((i + 1.0) / this.Categories.Count) * this.ColorArea.Height); Point p0, p1, p2, p3, p4; switch (this.Position) { case ColorAxisPosition.Right: p0 = new Point(this.ColorArea.Right, y); p1 = new Point(this.ColorArea.Left - this.TickLength, y); p2 = new Point( this.ColorArea.Left - this.TickLength - this.TextMargin - tb.DesiredSize.Width, y1 - (tb.DesiredSize.Height / 2)); p3 = new Point(this.ColorArea.Right, y2); p4 = new Point(this.ColorArea.Left - this.TickLength, y2); break; default: p0 = new Point(this.ColorArea.Left, y); p1 = new Point(this.ColorArea.Right + this.TickLength, y); p2 = new Point( this.ColorArea.Right + this.TickLength + this.TextMargin, y1 - (tb.DesiredSize.Height / 2)); p3 = new Point(this.ColorArea.Left, y2); p4 = new Point(this.ColorArea.Right + this.TickLength, y2); break; } var l = new System.Windows.Shapes.Line { X1 = p0.X, X2 = p1.X, Y1 = p0.Y, Y2 = p1.Y, Stroke = this.Foreground, StrokeThickness = 1, SnapsToDevicePixels = true }; this.Canvas.Children.Add(l); if (i == this.Categories.Count - 1) { var l2 = new System.Windows.Shapes.Line { X1 = p3.X, X2 = p4.X, Y1 = p3.Y, Y2 = p4.Y, Stroke = this.BorderBrush, StrokeThickness = 1, SnapsToDevicePixels = true }; this.Canvas.Children.Add(l2); } Canvas.SetLeft(tb, p2.X); Canvas.SetTop(tb, p2.Y); this.Canvas.Children.Add(tb); } }
public override void Plot(bool animate = true) { var pChart = Chart as PieChart; if (pChart == null) return; if (pChart.PieTotalSum <= 0) return; var rotated = 0d; Chart.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); var minDimension = Chart.DesiredSize.Width < Chart.DesiredSize.Height ? Chart.DesiredSize.Width : Chart.DesiredSize.Height; minDimension -= pChart.DrawPadding; minDimension = minDimension < pChart.DrawPadding ? pChart.DrawPadding : minDimension; var sliceId = 0; var isFist = true; foreach (var point in Values.Points) { var participation = point.Y / pChart.PieTotalSum; if (isFist) { rotated = participation * -.5; isFist = false; } var slice = new PieSlice { CentreX = 0, CentreY = 0, RotationAngle = 360 * rotated, WedgeAngle = 360 * participation, Radius = minDimension / 2, InnerRadius = pChart.InnerRadius, Fill = new SolidColorBrush { Color = Colors != null && Colors.Length > sliceId ? Colors[sliceId] : GetColorByIndex(sliceId), Opacity = 1 }, Stroke = Chart.Background, StrokeThickness = pChart.SlicePadding }; var wa = new DoubleAnimation { From = 0, To = slice.WedgeAngle, Duration = TimeSpan.FromMilliseconds(300) }; var ra = new DoubleAnimation { From = 0, To = slice.RotationAngle, Duration = TimeSpan.FromMilliseconds(300) }; Canvas.SetTop(slice, Chart.ActualHeight / 2); Canvas.SetLeft(slice, Chart.ActualWidth / 2); Chart.Canvas.Children.Add(slice); Shapes.Add(slice); var valueBlock = new TextBlock { Text = Chart.AxisX.LabelFormatter == null ? point.Y.ToString(CultureInfo.InvariantCulture) : Chart.AxisX.LabelFormatter(point.Y), FontFamily = Chart.AxisX.FontFamily, FontSize = Chart.AxisX.FontSize, FontStretch = Chart.AxisX.FontStretch, FontStyle = Chart.AxisX.FontStyle, FontWeight = Chart.AxisX.FontWeight, Foreground = Brushes.White }; var hypo = ((minDimension / 2) + (pChart.InnerRadius > 10 ? pChart.InnerRadius : 10)) / 2; var gamma = participation * 360 / 2 + rotated * 360; var cp = new Point(hypo * Math.Sin(gamma * (Math.PI / 180)), hypo * Math.Cos(gamma * (Math.PI / 180))); valueBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); Canvas.SetTop(valueBlock, Chart.ActualHeight / 2 - cp.Y - valueBlock.DesiredSize.Height * .5); Canvas.SetLeft(valueBlock, cp.X + Chart.ActualWidth / 2 - valueBlock.DesiredSize.Width * .5); Panel.SetZIndex(valueBlock, int.MaxValue); //because math is kind of complex to detetrmine if label fits inside the slide, by now we //will just add it if participation > 5% ToDo: the math! if (participation > .05 && Chart.AxisX.PrintLabels) { Chart.Canvas.Children.Add(valueBlock); Chart.Shapes.Add(valueBlock); } if (!Chart.DisableAnimation) { if (animate) { slice.BeginAnimation(PieSlice.WedgeAngleProperty, wa); slice.BeginAnimation(PieSlice.RotationAngleProperty, ra); } } if (Chart.Hoverable) { slice.MouseEnter += Chart.DataMouseEnter; slice.MouseLeave += Chart.DataMouseLeave; Chart.HoverableShapes.Add(new HoverableShape { Series = this, Shape = slice, Target = slice, Value = new Point(0, point.Y), Label = Labels != null && Labels.Count > point.X ? Labels[(int) point.X] : "" }); } sliceId++; rotated += participation; } }
/// <summary> /// Resizes a column to fit its visible contents. /// </summary> /// <param name="col">Index of the column to resize.</param> /// <param name="extra">Extra width to add to the column in pixels.</param> /// <param name="allCells">Whether to measure all cells or only the visible ones.</param> /// <returns>The new column width in pixels.</returns> public double AutoSizeColumn(int col, double extra, bool allCells) { double width = -1.0; if (allCells) { ViewRange = new CellRange(0, 0, Rows.Count - 1, Columns.Count - 1); if (Grid.AllowMerging == AllowMerging.None) { Column column = Grid.Columns[col]; int len = 0; int row = -1; TextBlock textBlock = new TextBlock(); Grid.Cells.Children.Add(textBlock); for (int i = 0; i < Rows.Count; i++) { if (Rows[i].ActualHeight > 0) { string dataFormatted = Rows[i].GetDataFormatted(column); if (dataFormatted.Length + 25 >= len) { len = Math.Max(len, dataFormatted.Length); textBlock.Text = dataFormatted; textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); if (textBlock.DesiredSize.Width > width) { width = textBlock.DesiredSize.Width; row = i; } } } } Grid.Cells.Children.Remove(textBlock); if (row > -1) { Size size = MeasureCell(row, col, double.PositiveInfinity, Grid.Rows[row].ActualHeight); width = size.Width + extra; } } else { for (int i = 0; i < Rows.Count; i++) { double actualHeight = Rows[i].ActualHeight; if (actualHeight > 0) { CellRange mergedRange = Grid.GetMergedRange(this, new CellRange(i, col)); if (mergedRange.RowSpan > 1) { actualHeight = 0; for (int j = mergedRange.Row; j <= mergedRange.Row2; j++) { actualHeight = actualHeight + Rows[j].ActualHeight; } } Size size = MeasureCell(i, col, double.PositiveInfinity, actualHeight); width = Math.Max(width, size.Width/mergedRange.ColumnSpan + extra); } } } UpdateViewRange(); } else { foreach (CellRange rng in _cells.Keys) { if (rng.Column == col) { FrameworkElement cell = _cells[rng]; if (cell != null) { Size desiredSize = GetDesiredSize(cell, new Size(double.PositiveInfinity, cell.MaxHeight)); width = Math.Max(width, desiredSize.Width/rng.ColumnSpan + extra); } } } } return Math.Ceiling(width); }
private TextBlock CreateTextLabel(IsolineTextLabel textLabel) { var transform = Plotter2D.Viewport.Transform; Point screenPos = textLabel.Position.DataToScreen(transform); double angle = textLabel.Rotation; Debug.WriteLine(angle); if (angle < 0) angle += 360; if (135 < angle && angle < 225) angle -= 180; TextBlock res = new TextBlock { Text = textLabel.Text, RenderTransform = new RotateTransform(angle), Tag = textLabel, RenderTransformOrigin = new Point(0.5, 0.5) }; res.Measure(SizeHelper.CreateInfiniteSize()); Size textSize = res.DesiredSize; Point position = new Point(screenPos.X - textSize.Width / 2, screenPos.Y - textSize.Height / 2); Canvas.SetLeft(res, position.X); Canvas.SetTop(res, position.Y); return res; }
private static Animator<string> MakeTextAnimator(Ani<Brush> foreground = null, Ani<double> fontSize = null, Ani<FontWeight> fontWeight = null) { var t = new TextBlock(); return (value, visible, topLeft) => Tuple.Create( new Animation { new TextDesc( value.Combine(visible, (v1, v2) => v2 ? (v1 ?? "") : ""), topLeft, new Point(0,0), fontSize: fontSize, fontWeight: fontWeight, foreground: foreground) }, value.Select(e => { t.Text = e; t.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); return t.DesiredSize; })); }
//------------------------------------------------------------------- // // Protected Methods // //------------------------------------------------------------------- #region Protected Methods /// <summary> /// Content measurement. /// </summary> /// <param name="constraint">Constraint size.</param> /// <returns>Computed desired size.</returns> protected sealed override Size MeasureOverride(Size constraint) { TextBlock.Measure(constraint); return(TextBlock.DesiredSize); }
public void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalAlignment halign, VerticalTextAlign valign) { var tb = new TextBlock { Text = text, Foreground = new SolidColorBrush(fill.ToColor()) }; if (fontFamily != null) tb.FontFamily = new FontFamily(fontFamily); if (fontSize > 0) tb.FontSize = fontSize; if (fontWeight > 0) tb.FontWeight = FontWeight.FromOpenTypeWeight((int)fontWeight); tb.Measure(new Size(1000, 1000)); double dx = 0; if (halign == HorizontalAlignment.Center) dx = -tb.DesiredSize.Width / 2; if (halign == HorizontalAlignment.Right) dx = -tb.DesiredSize.Width; double dy = 0; if (valign == VerticalTextAlign.Middle) dy = -tb.DesiredSize.Height / 2; if (valign == VerticalTextAlign.Bottom) dy = -tb.DesiredSize.Height; var transform = new TransformGroup(); transform.Children.Add(new TranslateTransform(dx, dy)); if (rotate != 0) transform.Children.Add(new RotateTransform(rotate)); transform.Children.Add(new TranslateTransform(p.X, p.Y)); tb.RenderTransform = transform; tb.SetValue(RenderOptions.ClearTypeHintProperty, ClearTypeHint.Enabled); Add(tb); }
public OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (String.IsNullOrEmpty(text)) return OxySize.Empty; var tb = new TextBlock { Text = text }; if (fontFamily != null) tb.FontFamily = new FontFamily(fontFamily); if (fontSize > 0) tb.FontSize = fontSize; if (fontWeight > 0) tb.FontWeight = FontWeight.FromOpenTypeWeight((int)fontWeight); tb.Measure(new Size(1000, 1000)); return new OxySize(tb.DesiredSize.Width, tb.DesiredSize.Height); }
static TextBlock CreateTextBlock(Label drawingLabel) { var textBlock = new TextBlock { Tag = drawingLabel, Text = drawingLabel.Text, FontFamily = new FontFamily(drawingLabel.FontName), FontSize = drawingLabel.FontSize, Foreground = Common.BrushFromMsaglColor(drawingLabel.FontColor) }; textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); textBlock.Width = textBlock.DesiredSize.Width; textBlock.Height = textBlock.DesiredSize.Height; textBlock.Background = Brushes.Transparent; textBlock.IsHitTestVisible = true; return textBlock; }