/// <summary> /// Add Title visual to DrawingArea /// </summary> /// <param name="chart">Chart</param> /// <param name="title">Title to add</param> /// <param name="panel">Panel where title to be added</param> /// <param name="width">Available width for title</param> /// <param name="height">Available height for title</param> private void AddTitle(Chart chart, Title title, Panel panel, Double width, Double height) { Double tempFontSize = title.InternalFontSize; title.Chart = chart; RECREATE_TITLE: title.CreateVisualObject(new ElementData() { Element = title }); Size size = Graphics.CalculateVisualSize(title.Visual); if (title.InternalVerticalAlignment == VerticalAlignment.Top || title.InternalVerticalAlignment == VerticalAlignment.Bottom || (title.InternalVerticalAlignment == VerticalAlignment.Center && title.InternalHorizontalAlignment == HorizontalAlignment.Center)) { if (size.Width > width && (chart.ActualWidth - width) < width) { if (title.InternalFontSize == 1) goto OUT; title.IsNotificationEnable = false; title.InternalFontSize -= 1; title.IsNotificationEnable = true; goto RECREATE_TITLE; } } else { if (size.Height >= height || title.Height >= height) { if (title.InternalFontSize == 1) goto OUT; title.IsNotificationEnable = false; title.InternalFontSize -= 1; title.IsNotificationEnable = true; goto RECREATE_TITLE; } } OUT: title.IsNotificationEnable = false; title.InternalFontSize = tempFontSize; title.IsNotificationEnable = true; // Add title Visual as children of panel panel.Children.Add(title.Visual); }
/// <summary> /// Returns label for DataPoint /// </summary> /// <param name="dataPoint"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="isPositive"></param> /// <param name="markerLeft"></param> /// <param name="markerTop"></param> /// <param name="labelCanvas"></param> /// <param name="IsSetPosition">Whether to set the position while creating it (in this function itself) </param> /// <returns>New position of the label</returns> private static Point CreateLabel4LineDataPoint(DataPoint dataPoint, Double width, Double height, Boolean isPositive, Double markerLeft, Double markerTop, ref Canvas labelCanvas, Boolean IsSetPosition) { Point retVal = new Point(); if (dataPoint.LabelVisual != null) { Panel parent = dataPoint.LabelVisual.Parent as Panel; if (parent != null) parent.Children.Remove(dataPoint.LabelVisual); } Chart chart = dataPoint.Chart as Chart; if (dataPoint.Faces == null || Double.IsNaN(dataPoint.InternalYValue)) return retVal; if ((Boolean)dataPoint.LabelEnabled && !String.IsNullOrEmpty(dataPoint.LabelText)) { LabelStyles autoLabelStyle = (LabelStyles)dataPoint.LabelStyle; Title tb = new Title() { Text = dataPoint.TextParser(dataPoint.LabelText), InternalFontFamily = dataPoint.LabelFontFamily, InternalFontSize = dataPoint.LabelFontSize.Value, InternalFontWeight = (FontWeight)dataPoint.LabelFontWeight, InternalFontStyle = (FontStyle)dataPoint.LabelFontStyle, InternalBackground = dataPoint.LabelBackground, InternalFontColor = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, autoLabelStyle), Padding = new Thickness(0.1, 0.1, 0.1, 0.1), Tag = new ElementData() { Element = dataPoint } }; tb.CreateVisualObject(new ElementData() { Element = dataPoint }); Double labelLeft = 0; Double labelTop = 0; Double gap = 6; if (Double.IsNaN(dataPoint.LabelAngle) || dataPoint.LabelAngle == 0) { SetLabelPosition4LineDataPoint(dataPoint, width, height, isPositive, markerLeft, markerTop, ref labelLeft, ref labelTop, gap, new Size(tb.TextBlockDesiredSize.Width, tb.TextBlockDesiredSize.Height)); retVal.X = labelLeft; retVal.Y = labelTop; if (IsSetPosition) { tb.Visual.SetValue(Canvas.LeftProperty, labelLeft); tb.Visual.SetValue(Canvas.TopProperty, labelTop); } Double depth3D = chart.ChartArea.PLANK_DEPTH / chart.PlotDetails.Layer3DCount * (chart.View3D ? 1 : 0); if (!dataPoint.IsLabelStyleSet && !dataPoint.Parent.IsLabelStyleSet) { if (isPositive) { if (labelTop < -depth3D) autoLabelStyle = LabelStyles.Inside; } else { if (labelTop + tb.TextBlockDesiredSize.Height > chart.PlotArea.BorderElement.Height - depth3D + chart.ChartArea.PLANK_THICKNESS) autoLabelStyle = LabelStyles.Inside; } } if (autoLabelStyle != dataPoint.LabelStyle) { SetLabelPosition4LineDataPoint(dataPoint, width, height, isPositive, markerLeft, markerTop, ref labelLeft, ref labelTop, gap, new Size(tb.TextBlockDesiredSize.Width, tb.TextBlockDesiredSize.Height)); retVal.X = labelLeft; retVal.Y = labelTop; if (IsSetPosition) { tb.Visual.SetValue(Canvas.LeftProperty, labelLeft); tb.Visual.SetValue(Canvas.TopProperty, labelTop); } } } else { if (isPositive) { Point centerOfRotation = new Point(markerLeft, markerTop - tb.TextBlockDesiredSize.Height / 2); Double radius = dataPoint.Marker.MarkerSize.Height / 2 * dataPoint.Marker.ScaleFactor; Double angle = 0; Double angleInRadian = 0; if (autoLabelStyle == LabelStyles.OutSide) { if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width; angle = (angleInRadian - Math.PI) * (180 / Math.PI); ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } else { centerOfRotation = new Point(markerLeft, markerTop + dataPoint.Marker.MarkerSize.Height / 2); if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = 180 + dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width + 3; angle = (angleInRadian - Math.PI) * (180 / Math.PI); ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { //radius += 3; angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } } else { Point centerOfRotation = new Point(); Double radius = dataPoint.Marker.MarkerSize.Height / 2 * dataPoint.Marker.ScaleFactor; Double angle = 0; Double angleInRadian = 0; if (autoLabelStyle == LabelStyles.OutSide) { centerOfRotation = new Point(markerLeft, markerTop + dataPoint.Marker.MarkerSize.Height / 2); if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = 180 + dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width; angle = (angleInRadian - Math.PI) * (180 / Math.PI); ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } else { centerOfRotation = new Point(markerLeft, markerTop - dataPoint.Marker.MarkerSize.Height / 2); if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width + 3; angle = (angleInRadian - Math.PI) * (180 / Math.PI); ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { //radius += 3; angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; ColumnChart.SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } } } if (autoLabelStyle != dataPoint.LabelStyle) { tb.TextElement.Foreground = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, (dataPoint.InternalYValue <= 0 ? LabelStyles.OutSide : autoLabelStyle)); } dataPoint.LabelVisual = tb.Visual; dataPoint.LabelVisual.Width = tb.TextBlockDesiredSize.Width; dataPoint.LabelVisual.Height = tb.TextBlockDesiredSize.Height; labelCanvas.Children.Add(tb.Visual); } return retVal; }
/// <summary> /// Create visual object of the Legend /// </summary> internal void CreateVisualObject() { if (!(Boolean)Enabled) { Visual = null; return; } ElementData tag = new ElementData { Element = this, VisualElementName = "Legend" }; Visual = new Border() { Tag = tag }; Grid innerGrid = new Grid() { Tag = tag }; (Visual as Border).Child = innerGrid; LegendContainer = new StackPanel() { Tag = tag }; if (!String.IsNullOrEmpty(Title)) { Title legendTitle = new Title(); ApplyFontProperty(legendTitle); if (TitleBackground != null) legendTitle.InternalBackground = TitleBackground; legendTitle.InternalHorizontalAlignment = TitleAlignmentX; legendTitle.InternalVerticalAlignment = VerticalAlignment.Top; legendTitle.TextAlignment = TitleTextAlignment; legendTitle.CreateVisualObject(tag); legendTitle.Measure(new Size(Double.MaxValue, Double.MaxValue)); if (legendTitle.DesiredSize.Width > InternalMaxWidth) legendTitle.Visual.Width = InternalMaxWidth; LegendContainer.Children.Add(legendTitle.Visual); } Grid legendContent = CreateLegendContent(); legendContent.Tag = tag; LegendContainer.Children.Add(legendContent); LegendContainer.VerticalAlignment = VerticalAlignment.Center; LegendContainer.HorizontalAlignment = HorizontalAlignment.Stretch; ApplyVisualProperty(); innerGrid.Children.Add(LegendContainer); Visual.Cursor = this.Cursor; Visual.Measure(new Size(Double.MaxValue, Double.MaxValue)); if (!Double.IsPositiveInfinity(InternalMaxHeight) && InternalMaxHeight < Visual.DesiredSize.Height) Visual.Height = InternalMaxHeight; else Visual.Height = Visual.DesiredSize.Height; if (!Double.IsPositiveInfinity(InternalMaxWidth) && InternalMaxWidth < Visual.DesiredSize.Width + InternalPadding.Left) Visual.Width = InternalMaxWidth; else if (Layout == Layouts.GridLayout) Visual.Width = Visual.DesiredSize.Width + InternalPadding.Left; else Visual.Width = Visual.DesiredSize.Width; PlotArea plotArea = (Chart as Chart).PlotArea; RectangleGeometry rectGeo = new RectangleGeometry(); rectGeo.Rect = new Rect(InternalBorderThickness.Left, InternalBorderThickness.Top, Visual.Width - InternalBorderThickness.Left - InternalBorderThickness.Right, Visual.Height - InternalBorderThickness.Top - InternalBorderThickness.Bottom); rectGeo.RadiusX = CornerRadius.TopLeft; rectGeo.RadiusY = CornerRadius.TopRight; LegendContainer.Clip = rectGeo; ApplyShadow(innerGrid); if (VerticalAlignment == System.Windows.VerticalAlignment.Bottom && (HorizontalAlignment == System.Windows.HorizontalAlignment.Center || HorizontalAlignment == System.Windows.HorizontalAlignment.Left || HorizontalAlignment == System.Windows.HorizontalAlignment.Right || HorizontalAlignment == System.Windows.HorizontalAlignment.Stretch)) Visual.Margin = new Thickness(0, DEFAULT_MARGIN, 0, 0); else if (VerticalAlignment == System.Windows.VerticalAlignment.Top && (HorizontalAlignment == System.Windows.HorizontalAlignment.Center || HorizontalAlignment == System.Windows.HorizontalAlignment.Left || HorizontalAlignment == System.Windows.HorizontalAlignment.Right || HorizontalAlignment == System.Windows.HorizontalAlignment.Stretch)) Visual.Margin = new Thickness(0, 0, 0, DEFAULT_MARGIN); else if (HorizontalAlignment == System.Windows.HorizontalAlignment.Left && (VerticalAlignment == System.Windows.VerticalAlignment.Center || VerticalAlignment == System.Windows.VerticalAlignment.Stretch)) Visual.Margin = new Thickness(0, 0, DEFAULT_MARGIN, 0); else if (HorizontalAlignment == System.Windows.HorizontalAlignment.Right && (VerticalAlignment == System.Windows.VerticalAlignment.Center || VerticalAlignment == System.Windows.VerticalAlignment.Stretch)) Visual.Margin = new Thickness(DEFAULT_MARGIN, 0, 0, 0); }
/// <summary> /// Create labels for DataPoint /// </summary> /// <param name="dataPoint">DataPoint</param> /// <returns>Border</returns> private static Border CreateLabelForDataPoint(DataPoint dataPoint, Boolean isStreamLine, Int32 sliceIndex) { Title title = new Title() { IsNotificationEnable = false, Chart = dataPoint.Chart, Text = dataPoint.TextParser(dataPoint.LabelText), InternalFontSize = (Double)dataPoint.LabelFontSize, InternalFontColor = (isStreamLine && sliceIndex == 0) ? Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, null, LabelStyles.OutSide) : Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, (LabelStyles)dataPoint.LabelStyle), InternalFontFamily = dataPoint.LabelFontFamily, InternalFontStyle = (FontStyle)dataPoint.LabelFontStyle, InternalFontWeight = (FontWeight)dataPoint.LabelFontWeight, InternalBackground = dataPoint.LabelBackground }; // If its a StreamLine funnel then default size of the Title should be a bit bigger if (isStreamLine && sliceIndex == 0 && dataPoint.GetValue(DataPoint.LabelFontSizeProperty) == null && dataPoint.Parent.GetValue(DataPoint.LabelFontSizeProperty) == null) { title.InternalFontSize = 11.5; } title.CreateVisualObject(new ElementData() { Element = dataPoint }); if (!(Boolean)dataPoint.LabelEnabled) title.Visual.Visibility = Visibility.Collapsed; return title.Visual; }
/// <summary> /// Returns label for DataPoint /// </summary> /// <param name="chart"></param> /// <param name="barParams"></param> /// <param name="dataPoint"></param> /// <param name="canvasLeft"></param> /// <param name="canvasTop"></param> /// <param name="canvasRight"></param> /// <returns></returns> private static void CreateLabel(Chart chart, Size barVisualSize, Boolean isPositive, Boolean isTopOfStack, DataPoint dataPoint, Double canvasLeft, Double canvasTop, Double canvasRight, Canvas labelCanvas) { if (dataPoint.Faces == null) return; LabelStyles autoLabelStyle = (LabelStyles)dataPoint.LabelStyle; if (isPositive || dataPoint.YValue == 0) isPositive = true; // Calculate proper position for Canvas top canvasTop -= 7; Double angle = 0; if ((Boolean)dataPoint.LabelEnabled && !String.IsNullOrEmpty(dataPoint.LabelText)) { Title tb = new Title() { Text = dataPoint.TextParser(dataPoint.LabelText), InternalFontFamily = dataPoint.LabelFontFamily, InternalFontSize = dataPoint.LabelFontSize.Value, InternalFontWeight = (FontWeight)dataPoint.LabelFontWeight, InternalFontStyle = (FontStyle)dataPoint.LabelFontStyle, InternalBackground = dataPoint.LabelBackground, InternalFontColor = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, autoLabelStyle), Tag = new ElementData() { Element = dataPoint } }; tb.CreateVisualObject(new ElementData() { Element = dataPoint }); Double labelTop = 0; Double labelLeft = 0; Double outsideGap = (chart.View3D ? 5 : 3); Double insideGap = (chart.View3D ? 4 : 3); if (Double.IsNaN(dataPoint.LabelAngle) || dataPoint.LabelAngle == 0) { Boolean isVertical = false; if (!dataPoint.IsLabelStyleSet && !dataPoint.Parent.IsLabelStyleSet && !isTopOfStack && dataPoint.Parent.RenderAs != RenderAs.Bar) { autoLabelStyle = LabelStyles.Inside; } CalculateAutoPlacement(chart.View3D, dataPoint, barVisualSize, isPositive, autoLabelStyle, ref labelLeft, ref labelTop, ref angle, canvasLeft, canvasTop, canvasRight, isVertical, insideGap, outsideGap, tb); tb.Visual.SetValue(Canvas.LeftProperty, labelLeft); tb.Visual.SetValue(Canvas.TopProperty, labelTop); tb.Visual.RenderTransformOrigin = new Point(0, 0.5); tb.Visual.RenderTransform = new RotateTransform() { CenterX = 0, CenterY = 0, Angle = angle }; if (!dataPoint.IsLabelStyleSet && !dataPoint.Parent.IsLabelStyleSet) { if (isPositive) { if (labelLeft + tb.TextBlockDesiredSize.Width > chart.PlotArea.BorderElement.Width) autoLabelStyle = LabelStyles.Inside; } else { if (labelLeft < 0) autoLabelStyle = LabelStyles.Inside; } } if (autoLabelStyle != dataPoint.LabelStyle) { CalculateAutoPlacement(chart.View3D, dataPoint, barVisualSize, isPositive, autoLabelStyle, ref labelLeft, ref labelTop, ref angle, canvasLeft, canvasTop, canvasRight, isVertical, insideGap, outsideGap, tb); tb.Visual.SetValue(Canvas.LeftProperty, labelLeft); tb.Visual.SetValue(Canvas.TopProperty, labelTop); } if (chart.SmartLabelEnabled) { if (dataPoint.Parent.RenderAs == RenderAs.StackedBar || dataPoint.Parent.RenderAs == RenderAs.StackedBar100) { if (!isVertical) { if (autoLabelStyle == LabelStyles.Inside) { if (barVisualSize.Width < tb.Visual.Width) return; } } } } } else { if (isPositive) { Point centerOfRotation = new Point(canvasRight + (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale), canvasTop + barVisualSize.Height / 2 + 6); Double radius = 0; angle = 0; Double angleInRadian = 0; if (autoLabelStyle == LabelStyles.OutSide) { if (dataPoint.LabelAngle <= 90 && dataPoint.LabelAngle >= -90) { angle = dataPoint.LabelAngle; radius += 4; angleInRadian = (Math.PI / 180) * angle; SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } else { centerOfRotation = new Point(canvasRight - (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale), canvasTop + barVisualSize.Height / 2 + 6); if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width + 3; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width + 4; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } } else { Point centerOfRotation = new Point(canvasLeft - (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale), canvasTop + barVisualSize.Height / 2 + 6); Double radius = 0; angle = 0; Double angleInRadian = 0; if (autoLabelStyle == LabelStyles.OutSide) { if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width + 3; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width + 3; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } else { centerOfRotation = new Point(canvasLeft + (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale), canvasTop + barVisualSize.Height / 2 + 6); if (dataPoint.LabelAngle <= 90 && dataPoint.LabelAngle >= -90) { angle = dataPoint.LabelAngle; radius += 3; angleInRadian = (Math.PI / 180) * angle; SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } } } if (autoLabelStyle != dataPoint.LabelStyle) { tb.TextElement.Foreground = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, (dataPoint.YValue == 0 ? LabelStyles.OutSide : autoLabelStyle)); } dataPoint.LabelVisual = tb.Visual; labelCanvas.Children.Add(tb.Visual); } }
/// <summary> /// Place label for DataPoint /// </summary> /// <param name="visual">Visual</param> /// <param name="labelCanvas">Canvas for label</param> /// <param name="dataPoint">DataPoint</param> internal static void CreateAndPositionLabel(Canvas labelCanvas, DataPoint dataPoint) { if (dataPoint.LabelVisual != null) { Panel parent = dataPoint.LabelVisual.Parent as Panel; if(parent != null) parent.Children.Remove(dataPoint.LabelVisual); } if ((Boolean)dataPoint.LabelEnabled && !String.IsNullOrEmpty(dataPoint.LabelText)) { Canvas dataPointVisual = dataPoint.Faces.Visual as Canvas; Title tb = new Title() { Text = dataPoint.TextParser(dataPoint.LabelText), FontFamily = dataPoint.LabelFontFamily, FontSize = dataPoint.LabelFontSize.Value, FontWeight = (FontWeight)dataPoint.LabelFontWeight, FontStyle = (FontStyle)dataPoint.LabelFontStyle, Background = dataPoint.LabelBackground, FontColor = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, LabelStyles.OutSide) }; tb.CreateVisualObject(new ElementData() { Element = dataPoint }); tb.Visual.Height = tb.Height; tb.Visual.Width = tb.Width; dataPoint.LabelVisual = tb.Visual; // Double labelTop = (Double)dataPointVisual.GetValue(Canvas.TopProperty) - tb.Height; // Double labelLeft = (Double)dataPointVisual.GetValue(Canvas.LeftProperty) + (dataPointVisual.Width - tb.Width) / 2; // if (labelTop < 0) labelTop = (Double)dataPointVisual.GetValue(Canvas.TopProperty); // if (labelLeft < 0) labelLeft = 1; // if (labelLeft + tb.ActualWidth > labelCanvas.Width) // labelLeft = labelCanvas.Width - tb.ActualWidth - 2; // tb.Visual.SetValue(Canvas.LeftProperty, labelLeft); // tb.Visual.SetValue(Canvas.TopProperty, labelTop); SetLabelPosition(dataPoint, labelCanvas.Width, labelCanvas.Height); labelCanvas.Children.Add(tb.Visual); } }
/// <summary> /// Returns label for DataPoint /// </summary> /// <param name="chart"></param> /// <param name="columnParams"></param> /// <param name="dataPoint"></param> /// <param name="canvasLeft"></param> /// <param name="canvasTop"></param> /// <returns></returns> private static void CreateLabel(Chart chart, Size columnVisualSize, Boolean isPositive, Boolean isTopOfStack, DataPoint dataPoint, Double canvasLeft, Double canvasTop, ref Canvas labelCanvas) { if (dataPoint.Faces == null) return; if ((Boolean)dataPoint.LabelEnabled && !String.IsNullOrEmpty(dataPoint.LabelText)) { LabelStyles autoLabelStyle = (LabelStyles)dataPoint.LabelStyle; if (isPositive || dataPoint.YValue == 0) isPositive = true; // Calculate proper position for Canvas top if (isPositive) canvasTop -= 6; else canvasTop -= 8; Double angle = 0; Title tb = new Title() { Text = dataPoint.TextParser(dataPoint.LabelText), InternalFontFamily = dataPoint.LabelFontFamily, InternalFontSize = dataPoint.LabelFontSize.Value, InternalFontWeight = (FontWeight)dataPoint.LabelFontWeight, InternalFontStyle = (FontStyle)dataPoint.LabelFontStyle, InternalBackground = dataPoint.LabelBackground, InternalFontColor = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, autoLabelStyle), Padding = new Thickness(0.1, 0.1, 0.1, 0.1), Tag = new ElementData() { Element = dataPoint } }; tb.CreateVisualObject(new ElementData() { Element = dataPoint }); Double labelLeft = 0; Double labelTop = 0; Double outsideGap = 4; Double insideGap = 6; if (Double.IsNaN(dataPoint.LabelAngle) || dataPoint.LabelAngle == 0) { Boolean isVertical = false; if (columnVisualSize.Width < tb.TextBlockDesiredSize.Width) { tb.Visual.RenderTransformOrigin = new Point(0, 0.5); tb.Visual.RenderTransform = new RotateTransform() { CenterX = 0, CenterY = 0, Angle = -90 }; isVertical = true; } else isVertical = false; if (!dataPoint.IsLabelStyleSet && !dataPoint.Parent.IsLabelStyleSet && !isTopOfStack && dataPoint.Parent.RenderAs != RenderAs.Column) { autoLabelStyle = LabelStyles.Inside; } if (dataPoint.Parent.RenderAs == RenderAs.StackedColumn100 && chart.View3D && isTopOfStack) { if (!dataPoint.IsLabelStyleSet && !dataPoint.Parent.IsLabelStyleSet && !isVertical && tb.TextBlockDesiredSize.Height >= columnVisualSize.Height) autoLabelStyle = LabelStyles.OutSide; } CalculateAutoPlacement(chart.View3D, dataPoint, columnVisualSize, isPositive, autoLabelStyle, ref labelLeft, ref labelTop, ref angle, canvasLeft, canvasTop, isVertical, insideGap, outsideGap, tb, isTopOfStack); tb.Visual.SetValue(Canvas.LeftProperty, labelLeft); tb.Visual.SetValue(Canvas.TopProperty, labelTop); tb.Visual.RenderTransformOrigin = new Point(0, 0.5); tb.Visual.RenderTransform = new RotateTransform() { CenterX = 0, CenterY = 0, Angle = angle }; Double depth3D = chart.ChartArea.PLANK_DEPTH / chart.PlotDetails.Layer3DCount * (chart.View3D ? 1 : 0); if (!dataPoint.IsLabelStyleSet && !dataPoint.Parent.IsLabelStyleSet) { if (isPositive) { if (isVertical) { if (labelTop + outsideGap - tb.TextBlockDesiredSize.Width < -depth3D) autoLabelStyle = LabelStyles.Inside; } else { if (labelTop < -depth3D) autoLabelStyle = LabelStyles.Inside; } } else { if (isVertical) { if (labelTop + outsideGap + 2 > chart.PlotArea.BorderElement.Height - depth3D + chart.ChartArea.PLANK_THICKNESS) autoLabelStyle = LabelStyles.Inside; } else { if (labelTop + tb.TextBlockDesiredSize.Height > chart.PlotArea.BorderElement.Height - depth3D + chart.ChartArea.PLANK_THICKNESS) autoLabelStyle = LabelStyles.Inside; } } } if (autoLabelStyle != dataPoint.LabelStyle) { CalculateAutoPlacement(chart.View3D, dataPoint, columnVisualSize, isPositive, autoLabelStyle, ref labelLeft, ref labelTop, ref angle, canvasLeft, canvasTop, isVertical, insideGap, outsideGap, tb, isTopOfStack); tb.Visual.SetValue(Canvas.LeftProperty, labelLeft); tb.Visual.SetValue(Canvas.TopProperty, labelTop); tb.Visual.RenderTransformOrigin = new Point(0, 0.5); tb.Visual.RenderTransform = new RotateTransform() { CenterX = 0, CenterY = 0, Angle = angle }; } if (chart.SmartLabelEnabled) { if (dataPoint.Parent.RenderAs == RenderAs.StackedColumn || dataPoint.Parent.RenderAs == RenderAs.StackedColumn100) { if (isVertical) { if (autoLabelStyle == LabelStyles.Inside) { if (columnVisualSize.Height < tb.Visual.Width) return; } } } } } else { if (isPositive) { Point centerOfRotation = new Point(canvasLeft + columnVisualSize.Width / 2, canvasTop - (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale)); Double radius = 0; angle = 0; Double angleInRadian = 0; if (autoLabelStyle == LabelStyles.OutSide) { if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } else { centerOfRotation = new Point(canvasLeft + columnVisualSize.Width / 2, canvasTop + (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale)); radius = 4; if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = 180 + dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width + 5; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { radius += 5; angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } } else { Point centerOfRotation = new Point(); Double radius = 0; angle = 0; Double angleInRadian = 0; if (autoLabelStyle == LabelStyles.OutSide) { centerOfRotation = new Point(canvasLeft + columnVisualSize.Width / 2, canvasTop + (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale) + 10); radius = 4; if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { angle = 180 + dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } else { centerOfRotation = new Point(canvasLeft + columnVisualSize.Width / 2, canvasTop - (((Double)dataPoint.MarkerSize / 2) * (Double)dataPoint.MarkerScale)); if (dataPoint.LabelAngle > 0 && dataPoint.LabelAngle <= 90) { angle = dataPoint.LabelAngle - 180; angleInRadian = (Math.PI / 180) * angle; radius += tb.TextBlockDesiredSize.Width; angle = (angleInRadian - Math.PI) * (180 / Math.PI); SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } else if (dataPoint.LabelAngle >= -90 && dataPoint.LabelAngle < 0) { //radius += 3; angle = dataPoint.LabelAngle; angleInRadian = (Math.PI / 180) * angle; SetRotation(radius, angle, angleInRadian, centerOfRotation, labelLeft, labelTop, tb); } } } } if (autoLabelStyle != dataPoint.LabelStyle) { tb.TextElement.Foreground = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, (dataPoint.YValue == 0 ? LabelStyles.OutSide : autoLabelStyle)); } dataPoint.LabelVisual = tb.Visual; labelCanvas.Children.Add(tb.Visual); } }
/// <summary> /// Create visual object of the Legend /// </summary> internal void CreateVisualObject() { if (!(Boolean)Enabled) { Visual = null; return; } ElementData tag = new ElementData { Element = this, VisualElementName = "Legend" }; Visual = new Border() { Tag = tag }; Grid innerGrid = new Grid() { Tag = tag }; (Visual as Border).Child = innerGrid; LegendContainer = new StackPanel() { Tag = tag }; if (!String.IsNullOrEmpty(Title)) { Title legendTitle = new Title(); ApplyFontProperty(legendTitle); if (TitleBackground != null) legendTitle.InternalBackground = TitleBackground; legendTitle.InternalHorizontalAlignment = TitleAlignmentX; legendTitle.InternalVerticalAlignment = VerticalAlignment.Top; legendTitle.TextAlignment = TitleTextAlignment; legendTitle.CreateVisualObject(tag); legendTitle.Measure(new Size(Double.MaxValue, Double.MaxValue)); if (legendTitle.DesiredSize.Width > InternalMaxWidth) legendTitle.Visual.Width = InternalMaxWidth; LegendContainer.Children.Add(legendTitle.Visual); } Grid legendContent = CreateLegendContent(); legendContent.Tag = tag; LegendContainer.Children.Add(legendContent); LegendContainer.VerticalAlignment = VerticalAlignment.Center; LegendContainer.HorizontalAlignment = HorizontalAlignment.Center; ApplyVisualProperty(); innerGrid.Children.Add(LegendContainer); Visual.Cursor = this.Cursor; Visual.Measure(new Size(Double.MaxValue, Double.MaxValue)); if (!Double.IsPositiveInfinity(InternalMaxHeight) && InternalMaxHeight < Visual.DesiredSize.Height) Visual.Height = InternalMaxHeight; else Visual.Height = Visual.DesiredSize.Height; if (!Double.IsPositiveInfinity(InternalMaxWidth) && InternalMaxWidth < Visual.DesiredSize.Width + InternalPadding.Left) Visual.Width = InternalMaxWidth; else Visual.Width = Visual.DesiredSize.Width + InternalPadding.Left; }
/// <summary> /// Create labels for DataPoint /// </summary> /// <param name="dataPoint">DataPoint</param> /// <returns>Border</returns> private static Border CreateLabelForDataPoint(DataPoint dataPoint, Int32 sliceIndex) { Title title = new Title() { IsNotificationEnable = false, Chart = dataPoint.Chart, Text = dataPoint.TextParser(dataPoint.LabelText), InternalFontSize = (Double)dataPoint.LabelFontSize, InternalFontColor = Chart.CalculateDataPointLabelFontColor(dataPoint.Chart as Chart, dataPoint, dataPoint.LabelFontColor, (LabelStyles)dataPoint.LabelStyle), InternalFontFamily = dataPoint.LabelFontFamily, InternalFontStyle = (FontStyle)dataPoint.LabelFontStyle, InternalFontWeight = (FontWeight)dataPoint.LabelFontWeight, InternalBackground = dataPoint.LabelBackground }; title.CreateVisualObject(new ElementData() { Element = dataPoint }); if (!(Boolean)dataPoint.LabelEnabled) title.Visual.Visibility = Visibility.Collapsed; return title.Visual; }