Completed
Last Updated: 19 Jul 2016 11:49 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 30 Jun 2016 08:22
Category: ChartView
Type: Bug Report
0
FIX. RadChartView - wrapped multiline title is not exported wrapped
To reproduce:

Public Sub New()
    InitializeComponent()

    Me.RadChartView1.ShowTitle = True
    Me.RadChartView1.ChartElement.TitleElement.TextWrap = True
    Me.RadChartView1.Title = "This is a very long chart title that can't fit in a single line"

    Dim barSeries As New BarSeries("Performance", "RepresentativeName")
    barSeries.Name = "Q1"
    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)
End Sub

Private Sub radButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click 
    Dim filePath As String = "..\..\..\exprtedChart.png"
    Me.RadChartView1.ExportToImage(filePath, Me.RadChartView1.Size, System.Drawing.Imaging.ImageFormat.Png)
    Process.Start(filePath)
End Sub


Workaround: titleSize must consider the chart's width

Public Sub New()
    InitializeComponent()
    AddHandler Me.RadChartView1.CreateRenderer, AddressOf CreateRenderer
    Me.RadChartView1.ShowTitle = True
    Me.RadChartView1.ChartElement.TitleElement.TextWrap = True
    Me.RadChartView1.Title = "This is a very long chart title that can't fit in a single line"

    Dim barSeries As New BarSeries("Performance", "RepresentativeName")
    barSeries.Name = "Q1"
    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)
End Sub

Private Sub radButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
    Dim filePath As String = "..\..\..\exprtedChart.png"
    Using fs As New FileStream(filePath, FileMode.Create, FileAccess.Write)
        ExportToImage(Me.RadChartView1, fs, Me.RadChartView1.Size, System.Drawing.Imaging.ImageFormat.Png)
    End Using
    Process.Start(filePath)
End Sub

Public Sub ExportToImage(chart As RadChartView, stream As Stream, size As Size, imageFormat As ImageFormat)
    If Not chart.IsLoaded Then
        chart.LoadElementTree()
    End If

    Dim bmp As New Bitmap(size.Width, size.Height)
    Dim graphics__1 As Graphics = Graphics.FromImage(bmp)
    graphics__1.Clear(Color.White)

    Dim titleSize As SizeF = graphics__1.MeasureString(chart.Title, chart.ChartElement.TitleElement.Font, chart.Width)

    If chart.ChartElement.TitleElement.TextOrientation = Orientation.Vertical Then
        Dim swap As Single = titleSize.Height
        titleSize.Height = titleSize.Width
        titleSize.Width = swap
    End If

    Dim titleRect As New RadRect(0, 0, titleSize.Width, titleSize.Height)
    Dim legendRect As New RadRect(0, 0, size.Width, size.Height)
    Dim chartRect As RadRect = legendRect

    Select Case chart.ChartElement.TitlePosition
        Case TitlePosition.Top, TitlePosition.Bottom
            titleRect.Width = size.Width
            Exit Select
        Case TitlePosition.Right, TitlePosition.Left
            titleRect.Height = size.Height
            Exit Select
    End Select

    chartRect.X += chart.View.Margin.Left
    chartRect.Y += chart.View.Margin.Top
    chartRect.Width -= chart.View.Margin.Horizontal
    chartRect.Height -= chart.View.Margin.Vertical

    If chart.ShowTitle Then
        Select Case chart.ChartElement.TitlePosition
            Case TitlePosition.Top
                legendRect.Y += titleRect.Height
                chartRect.Y += titleRect.Height
                legendRect.Height -= titleRect.Height
                chartRect.Height -= titleRect.Height
                Exit Select
            Case TitlePosition.Right
                titleRect.X = size.Width - chart.ChartElement.TitleElement.Size.Width
                titleRect.Height = size.Height
                legendRect.Width -= titleRect.Width
                chartRect.Width -= titleRect.Width
                Exit Select
            Case TitlePosition.Bottom
                titleRect.Y = size.Height - chart.ChartElement.TitleElement.Size.Height
                titleRect.Width = size.Width
                legendRect.Height -= titleRect.Height
                chartRect.Height -= titleRect.Height
                Exit Select
            Case TitlePosition.Left
                titleRect.Height = size.Height
                legendRect.X += titleRect.Width
                chartRect.X += titleRect.Width
                legendRect.Width -= titleRect.Width
                chartRect.Width -= titleRect.Width
                Exit Select
        End Select
    End If

    If chart.ShowLegend Then
        Select Case chart.ChartElement.LegendPosition
            Case LegendPosition.Right
                If chart.ChartElement.TitlePosition = TitlePosition.Right Then
                    legendRect.X = titleRect.X - chart.ChartElement.LegendElement.Size.Width
                Else
                    legendRect.X = size.Width - chart.ChartElement.LegendElement.Size.Width
                End If

                legendRect.Width = chart.ChartElement.LegendElement.Size.Width
                chartRect.Width -= legendRect.Width + chart.View.Margin.Right
                Exit Select
            Case LegendPosition.Bottom
                If chart.ChartElement.TitlePosition = TitlePosition.Bottom Then
                    legendRect.Y = titleRect.Y - chart.ChartElement.LegendElement.Size.Height
                Else
                    legendRect.Y = size.Height - chart.ChartElement.LegendElement.Size.Height
                End If

                legendRect.Height = chart.ChartElement.LegendElement.Size.Height
                chartRect.Height -= legendRect.Height
                Exit Select
            Case LegendPosition.Left
                legendRect.Width = chart.ChartElement.LegendElement.Size.Width
                chartRect.X += legendRect.Width + chart.View.Margin.Left
                chartRect.Width -= legendRect.Width + chart.View.Margin.Left
                Exit Select
            Case LegendPosition.Top
                legendRect.Height = chart.ChartElement.LegendElement.Size.Height
                chartRect.Y += legendRect.Height
                chartRect.Height -= legendRect.Height
                Exit Select
            Case LegendPosition.Float
                legendRect.Width = chart.ChartElement.LegendElement.Size.Width
                legendRect.Height = chart.ChartElement.LegendElement.Size.Height
                Dim xRatio As Double = size.Width / Me.Size.Width
                Dim yRatio As Double = size.Height / Me.Size.Height
                legendRect.X = (chart.ChartElement.LegendOffset.X * xRatio) + (If((chart.ChartElement.TitlePosition = TitlePosition.Left), titleRect.Right, 0.0))
                legendRect.Y = (chart.ChartElement.LegendOffset.Y * yRatio) + (If((chart.ChartElement.TitlePosition = TitlePosition.Top), titleRect.Bottom, 0.0F))
                Exit Select
        End Select
    End If

    If chart.ShowLegend Then
        Dim xTransform As Single = CSng(legendRect.X) - chart.ChartElement.LegendElement.ControlBoundingRectangle.X + _
        (CSng(legendRect.Width) - chart.ChartElement.LegendElement.ControlBoundingRectangle.Width) / 2.0F
        Dim yTransform As Single = CSng(legendRect.Y) - chart.ChartElement.LegendElement.ControlBoundingRectangle.Y + _
        (CSng(legendRect.Height) - chart.ChartElement.LegendElement.ControlBoundingRectangle.Height) / 2.0F
        graphics__1.TranslateTransform(xTransform, yTransform)
        chart.ChartElement.LegendElement.Paint(New RadGdiGraphics(graphics__1), _
                                               chart.ChartElement.LegendElement.ControlBoundingRectangle, 0.0F, New SizeF(1.0F, 1.0F), True)
        graphics__1.ResetTransform()
    End If

    Dim radGraphics As New RadGdiGraphics(graphics__1)

    If chart.ShowTitle Then
        radGraphics.DrawString(chart.Title, GetTitleDrawRectangle(ChartRenderer.ToRectangleF(titleRect), _
                                                                  titleSize, chart.ChartElement.TitleElement.TextAlignment), chart.ChartElement.TitleElement.Font, _
                               chart.ChartElement.TitleElement.ForeColor, chart.ChartElement.TitleElement.TextParams.CreateStringFormat(), _
                               chart.ChartElement.TitleElement.TextOrientation, _
                               chart.ChartElement.TitleElement.FlipText)
    End If

    chart.View.Layout(chartRect)
    renderer.Draw(graphics__1)

    bmp.Save(stream, imageFormat)
    chart.View.Layout()
End Sub

Private Function GetTitleDrawRectangle(drawArea As RectangleF, textRect As SizeF, textAlignment As ContentAlignment) As RectangleF
    Select Case textAlignment
        Case ContentAlignment.BottomCenter
            Return New RectangleF(New PointF(drawArea.X + (drawArea.Width - textRect.Width) / 2.0F, drawArea.Bottom - textRect.Height), textRect)
        Case ContentAlignment.BottomLeft
            Return New RectangleF(New PointF(drawArea.X, drawArea.Bottom - textRect.Height), textRect)
        Case ContentAlignment.BottomRight
            Return New RectangleF(New PointF(drawArea.Right - textRect.Width, drawArea.Bottom - textRect.Height), textRect)
        Case ContentAlignment.MiddleCenter
            Return New RectangleF(New PointF(drawArea.X + (drawArea.Width - textRect.Width) / 2.0F, drawArea.Y + (drawArea.Height - textRect.Height) / 2.0F), textRect)
        Case ContentAlignment.MiddleLeft
            Return New RectangleF(New PointF(drawArea.X, drawArea.Y + (drawArea.Height - textRect.Height) / 2.0F), textRect)
        Case ContentAlignment.MiddleRight
            Return New RectangleF(New PointF(drawArea.Right - textRect.Width, drawArea.Y + (drawArea.Height - textRect.Height) / 2.0F), textRect)
        Case ContentAlignment.TopCenter
            Return New RectangleF(New PointF(drawArea.X + (drawArea.Width - textRect.Width) / 2, drawArea.Y), textRect)
        Case ContentAlignment.TopLeft
            Return New RectangleF(drawArea.Location, textRect)
        Case ContentAlignment.TopRight
            Return New RectangleF(New PointF(drawArea.Right - textRect.Width, drawArea.Y), textRect)
        Case Else
            Return New RectangleF(drawArea.Location, textRect)
    End Select
End Function

Dim renderer As CartesianRenderer
Private Sub CreateRenderer(sender As Object, e As ChartViewCreateRendererEventArgs)
    e.Renderer = New CartesianRenderer(DirectCast(e.Area, CartesianArea))
    renderer = e.Renderer
End Sub
Attached Files:
0 comments