Declined
Last Updated: 06 Nov 2018 09:47 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 05 Nov 2018 05:57
Category: ChartView
Type: Bug Report
1
FIX. RadChartView - series' labels should be clipped when they don't fit the view area
To reproduce: please refer to the attached sample project  and gif file illustrating the behavior on my end. 

Note: similar to the axis, the series should also have ClipLabels property which will control whether the labels will be clipped or not.

Workaround: use custom renderer:

 Sub New()

        InitializeComponent() 

        AddHandler Me.RadChartView1.CreateRenderer, AddressOf RadChartView1_CreateRenderer

        Dim barSeries As New Telerik.WinControls.UI.BarSeries("Performance", "RepresentativeName")
        barSeries.Name = "Q1"
        barSeries.ShowLabels = True
        barSeries.DataPoints.Add(New CategoricalDataPoint(177, "Harley"))
        barSeries.DataPoints.Add(New CategoricalDataPoint(128, "White"))
        barSeries.DataPoints.Add(New CategoricalDataPoint(143, "Smith"))
        barSeries.DataPoints.Add(New CategoricalDataPoint(111, "Jones"))
        barSeries.DataPoints.Add(New CategoricalDataPoint(118, "Marshall"))
        Me.RadChartView1.Series.Add(barSeries)
        Dim barSeries2 As New Telerik.WinControls.UI.BarSeries("Performance", "RepresentativeName")
        barSeries2.Name = "Q2"
        barSeries2.ShowLabels = True
        barSeries2.DataPoints.Add(New CategoricalDataPoint(153, "Harley"))
        barSeries2.DataPoints.Add(New CategoricalDataPoint(141, "White"))
        barSeries2.DataPoints.Add(New CategoricalDataPoint(130, "Smith"))
        barSeries2.DataPoints.Add(New CategoricalDataPoint(88, "Jones"))
        barSeries2.DataPoints.Add(New CategoricalDataPoint(109, "Marshall"))
        Me.RadChartView1.Series.Add(barSeries2)


        Dim lassoZoomController As New ChartPanZoomController()
        RadChartView1.Controllers.Add(lassoZoomController)

    End Sub
    
    Public Class CustomLabelElementDrawPart
        Inherits BarLabelElementDrawPart
        Public Sub New(owner As ChartSeries, renderer As IChartRenderer)
            MyBase.New(owner, renderer)

        End Sub

        Public Overrides Sub Draw()
            If Not Me.Element.ShowLabels Then
                Return
            End If

            Dim graphics As Graphics = TryCast(Me.Renderer.Surface, Graphics)
            Dim radGraphics As RadGdiGraphics = New RadGdiGraphics(graphics)
            Dim isSmartLabelsEnabled As Boolean = Me.Element.View.ShowSmartLabels
            Dim isLineToLabelEnabled As Boolean = Me.Element.DrawLinesToLabels

            For Each dataPointElement As DataPointElement In Me.Element.Children
                Dim categoricalDataPoint As CategoricalDataPoint = TryCast(dataPointElement.DataPoint, CategoricalDataPoint)


                For i As Integer = 0 To dataPointElement.Children.Count - 1
                    Dim labelElement As LabelElement = TryCast(dataPointElement.Children(i), LabelElement)

                    If labelElement Is Nothing Then
                        Continue For
                    End If

                    labelElement.OnLabelFormatting(New ChartViewLabelFormattingEventArgs(labelElement))

                    If Not labelElement.IsVisible OrElse String.IsNullOrEmpty(labelElement.Text) Then
                        Continue For
                    End If

                    Dim rect As Rectangle
                    Dim slot As RadRect = labelElement.GetLayoutSlot()

                    If isSmartLabelsEnabled AndAlso labelElement.SmartRectangle <> Rectangle.Empty Then
                        rect = labelElement.SmartRectangle
                    Else
                        slot = AdjustLayoutSlot(slot, labelElement.DataPointElement)
                        rect = ChartRenderer.ToRectangle(slot)
                    End If

                    Dim state As Object = radGraphics.SaveState()
                    Dim horizontalTranslate As Integer = rect.X + rect.Width / 2
                    Dim verticalTranslate As Integer = rect.Y + rect.Height / 2
                    Dim clipRect As RectangleF
                    If TypeOf Me.Renderer Is CartesianRenderer Then
                        Dim size As SizeF = graphics.MeasureString("W", Me.Element.Font)
                        Dim plotRect As RadRect = Me.Element.Model.LayoutSlot
                        plotRect.X += Me.ViewportOffsetX
                        plotRect.Y += Me.ViewportOffsetY
                        clipRect = ChartRenderer.ToRectangleF(plotRect)
                        clipRect.Y -= size.Height / 2.0F
                        clipRect.Height += size.Height
                        clipRect.Width += size.Width * 2.0F
                        graphics.SetClip(clipRect)
                    End If

                    Dim angle As Single = CSng(Me.Element.LabelRotationAngle) Mod 360.0F

                    If angle <> 0 Then
                        radGraphics.TranslateTransform(horizontalTranslate, verticalTranslate)
                        radGraphics.RotateTransform(angle)
                        radGraphics.TranslateTransform(-horizontalTranslate, -verticalTranslate)
                    End If

                    If isLineToLabelEnabled Then
                        Me.DrawConnectingLine(radGraphics, labelElement, dataPointElement, isSmartLabelsEnabled AndAlso labelElement.SmartRectangle <> Rectangle.Empty)
                    End If

                    If labelElement.BackgroundShape IsNot Nothing Then
                        labelElement.BackgroundShape.Paint(CType(radGraphics.UnderlayGraphics, Graphics), rect)
                    End If

                    Dim fill As Telerik.WinControls.Primitives.FillPrimitiveImpl = New Telerik.WinControls.Primitives.FillPrimitiveImpl(labelElement, Nothing)
                    fill.PaintFill(radGraphics, 0, System.Drawing.Size.Empty, rect)
                    Dim border As Telerik.WinControls.Primitives.BorderPrimitiveImpl = New Telerik.WinControls.Primitives.BorderPrimitiveImpl(labelElement, Nothing)
                    border.PaintBorder(radGraphics, 0, System.Drawing.Size.Empty, rect)

                    Using brush As Brush = New SolidBrush(labelElement.ForeColor)
                        Dim drawRectangle As RectangleF = New RectangleF()
                        drawRectangle.X = rect.X + labelElement.Padding.Left
                        drawRectangle.Y = rect.Y + labelElement.Padding.Top
                        drawRectangle.Width = rect.Width - labelElement.Padding.Right
                        drawRectangle.Height = rect.Height - labelElement.Padding.Bottom
                        Dim format As StringFormat = New StringFormat()
                        format.Alignment = ContentAlignmentToHorizontalStringAlignment(labelElement.TextAlignment)
                        format.LineAlignment = Me.ContentAlignmentToVerticalStringAlignment(labelElement.TextAlignment)
                        graphics.DrawString(labelElement.Text, labelElement.Font, brush, drawRectangle, format)
                    End Using

                    If angle <> 0 Then
                        radGraphics.ResetTransform()
                        radGraphics.RestoreState(state)
                    End If

                    graphics.ResetClip()
                Next
            Next
        End Sub

        Public Function ContentAlignmentToVerticalStringAlignment(ByVal contentAlignment As ContentAlignment) As StringAlignment
            Dim result As StringAlignment

            Select Case contentAlignment
                Case contentAlignment.BottomCenter, contentAlignment.BottomLeft, contentAlignment.BottomRight
                    result = StringAlignment.Far
                Case contentAlignment.TopCenter, contentAlignment.TopLeft, contentAlignment.TopRight
                    result = StringAlignment.Near
                Case Else
                    result = StringAlignment.Center
            End Select

            Return result
        End Function

        Private Function ContentAlignmentToHorizontalStringAlignment(ByVal contentAlignment As ContentAlignment) As StringAlignment
            Dim result As StringAlignment

            Select Case contentAlignment
                Case contentAlignment.BottomLeft, contentAlignment.MiddleLeft, contentAlignment.TopLeft
                    result = StringAlignment.Near
                Case contentAlignment.BottomRight, contentAlignment.MiddleRight, contentAlignment.TopRight
                    result = StringAlignment.Far
                Case Else
                    result = StringAlignment.Center
            End Select

            Return result
        End Function

    End Class

    Private Sub RadChartView1_CreateRenderer(sender As Object, e As ChartViewCreateRendererEventArgs)
        e.Renderer = New CustomCartesianRenderer(e.Area)
    End Sub

    Public Class CustomCartesianRenderer
        Inherits CartesianRenderer
        Public Sub New(area As CartesianArea)
            MyBase.New(area)
        End Sub
        Protected Overrides Sub Initialize()
            MyBase.Initialize()
            For i As Integer = 0 To Me.DrawParts.Count - 1

                Dim label As BarLabelElementDrawPart = TryCast(Me.DrawParts(i), BarLabelElementDrawPart)
                If (label IsNot Nothing) Then
                    Me.DrawParts(i) = New CustomLabelElementDrawPart(label.Element, Me)
                End If
            Next
        End Sub
    End Class
1 comment
ADMIN
Hristo
Posted on: 06 Nov 2018 09:28
The issue duplicates an already existing one, please refer to the following item: https://feedback.telerik.com/Project/154/Feedback/Details/206758-fix-radchartview-cartesianseries-data-points-labels-are-not-clipped-if-they-ar. We will try to increase its priority.