Search code examples
activexteechart

Teechart + Word Wrap for legend


I am drawing a Trend chart(line) but in my case the legend Text is really big so it there any way that I can word wrap the text.

enter image description here


Solution

  • I think you can use the string functions that you can find in this link to manipulate the titles of series and try to reduce their length. I have made a suggestion code that I think can help you achieve as you want:

    Private Sub Form_Load()
        TChart1.Aspect.View3D = False
        TChart1.AddSeries scLine
        TChart1.AddSeries scLine
        Dim i As Integer
        TChart1.Series(0).AddXY 0, 30, "", clTeeColor
        TChart1.Series(0).AddXY 10, 100, "", clTeeColor
        TChart1.Series(1).AddXY 0, 50, "", clTeeColor
        TChart1.Series(1).AddXY 50, 120, "", clTeeColor
        TChart1.Series(0).Title = "DDDDAAAAFFFFLLLLRRRRSSSS"
        TChart1.Series(1).Title = "AAAALLLLSSSSTTTTEEEERRRR"
        SeriesTitleWarp TChart1.SeriesCount
    End Sub
    
    Private Sub SeriesTitleWarp(ByVal count As Long)
    'Replace some chars of string title to ...
        Dim i As Integer
            For i = 0 To count - 1
                'Calculate the size of string
                Dim LenString As Integer
                LenString = Len(TChart1.Series(i).Title)
                'First replace the Left chars for ...
                Dim TitleString As String
                TitleString = TChart1.Series(i).Title
                Mid$(TitleString, 10, 3) = "..."
                'After cut the string
                TitleString = Left(TitleString, 12)
                'Assign new title to series.
                TChart1.Series(i).Title = TitleString
        Next i
    End Sub
    

    Revising your requirements, I suggest you an other alternative that reduces the Legend Text, but the title of series remains intact. Could you please check if next code works as you want?

    Private Sub Form_Load()
        TChart1.Aspect.View3D = False
        TChart1.AddSeries scLine
        TChart1.AddSeries scLine
        Dim i As Integer
        TChart1.Series(0).AddXY 0, 30, "", clTeeColor
        TChart1.Series(0).AddXY 10, 100, "", clTeeColor
        TChart1.Series(1).AddXY 0, 50, "", clTeeColor
        TChart1.Series(1).AddXY 50, 120, "", clTeeColor
        TChart1.Series(0).Title = "DDDDAAAAFFFFLLLLRRRRSSSS"
        TChart1.Series(1).Title = "AAAALLLLSSSSTTTTEEEERRRR"
      '  TChart1.Legend.ShapeBounds.Right = 100
        TChart1.Legend.Left = 100
        TChart1.Legend.CustomPosition = True
        TChart1.Legend.Width = 100
    End Sub
    
    Private Sub TChart1_OnGetLegendText(ByVal LegendStyle As Long, ByVal ValueIndex As Long, LegendText As String)
            If ValueIndex <> -1 Then
                'Calculate the size of string
                Dim LenString As Integer
                LenString = Len(TChart1.Series(ValueIndex).Title)
                'First replace the Left chars for ...
                Dim TitleString As String
                TitleString = TChart1.Series(ValueIndex).Title
                Mid$(TitleString, 10, 3) = "..."
                'After cut the string
                TitleString = Left(TitleString, 12)
                'Assign new text to LegendText
                LegendText = TitleString
            End If
    End Sub
    

    I hope will helps.

    Thanks.