Completed
Last Updated: 02 Jun 2017 13:35 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 27 Jan 2017 09:10
Category: ChartView
Type: Feature Request
1
ADD. RadChartView - add an option to control the TextAlignment of the label elements (e.g. for the BarSeries)
Workaround:

 Sub New()

     InitializeComponent()

     AddHandler Me.RadChartView1.CreateRenderer, AddressOf CreateRenderer
     
     AddHandler Me.RadChartView1.LabelFormatting, AddressOf LabelFormatting 
     Dim barSeries As New Telerik.WinControls.UI.BarSeries("Performance", "RepresentativeName")
     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.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)

 End Sub

 Private Sub LabelFormatting(sender As Object, e As Telerik.WinControls.UI.ChartViewLabelFormattingEventArgs)
     Dim dataPoint As CategoricalDataPoint = TryCast(e.LabelElement.DataPoint, CategoricalDataPoint)
     e.LabelElement.Text = "Value: " & Environment.NewLine & dataPoint.Value
     e.LabelElement.BackColor = Color.Yellow 
 End Sub

 Private Sub CreateRenderer(sender As Object, e As ChartViewCreateRendererEventArgs)
     e.Renderer = New CustomCartesianRenderer(TryCast(e.Area, CartesianArea))
 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 labelPart As BarLabelElementDrawPart = TryCast(Me.DrawParts(i), BarLabelElementDrawPart)
             If labelPart IsNot Nothing Then
                 Me.DrawParts(i) = New CustomLabelElementDrawPart(DirectCast(labelPart.Element, BarSeries), Me)
             End If
         Next
     End Sub
 End Class

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

     Public Overrides Sub Draw()
         For Each dataPointElement As DataPointElement In Me.Element.Children
             For Each label As LabelElement In dataPointElement.Children
                 label.ForeColor = Color.Transparent
             Next
         Next

         MyBase.Draw()
         Dim graphics As Graphics = TryCast(Me.Renderer.Surface, Graphics)
         Dim radGraphics As New RadGdiGraphics(graphics)
         Dim slot As RadRect
         Dim rect As Rectangle
         Dim stringFormat As New StringFormat()
         stringFormat.Alignment = StringAlignment.Center
         stringFormat.LineAlignment = StringAlignment.Center

         For Each dataPointElement As DataPointElement In Me.Element.Children
             For Each label As LabelElement In dataPointElement.Children
                 label.ForeColor = Color.Transparent
                 slot = label.GetLayoutSlot()
                 slot = AdjustLayoutSlot(slot, label.DataPointElement)
                 rect = ChartRenderer.ToRectangle(slot)
                 Using brush As Brush = New SolidBrush(Color.Red)
                     Dim drawLocation As New PointF()
                     drawLocation.X = rect.X + label.Padding.Left
                     drawLocation.Y = rect.Y + label.Padding.Top

                     graphics.DrawString(label.Text, label.Font, brush, rect, stringFormat)
                 End Using
             Next
         Next
     End Sub
 End Class
Attached Files:
(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
1 comment
ADMIN
Ralitsa
Posted on: 02 Jun 2017 13:33
In order to change the alignment of the label, you need to handle the LabelFormatting event and set the TextAlignment property to desired value. Here is the code snippet how can achieve it: 
void radChartView1_LabelFormatting(object sender, Telerik.WinControls.UI.ChartViewLabelFormattingEventArgs e)
{
    e.LabelElement.BackColor = Color.Red;
    e.LabelElement.TextAlignment = ContentAlignment.BottomRight;
}