Page 1 of 1

[Bug] Changing LegendItem text breaks redraw

Posted: Thu Mar 12, 2009 1:18 am
by 15049432
Add this code:

Code: Select all

Option Explicit

Private Sub Form_Load()
    TChart1.AddSeries scLine
    TChart1.AddSeries scLine
    TChart1.AddSeries scLine
    
    TChart1.Series(0).FillSampleValues 10
    TChart1.Series(0).Title = "Series No. 001"
    TChart1.Series(1).FillSampleValues 10
    TChart1.Series(1).Title = "Series No. 002"
    TChart1.Series(2).FillSampleValues 10
    TChart1.Series(2).Title = "Series No. 003"

End Sub

Private Sub Form_Resize()
    TChart1.Top = 0
    TChart1.Left = 0
    TChart1.Width = Me.Width
    TChart1.Height = Me.Height - 1000
End Sub

Private Sub TChart1_OnClickSeries(ByVal SeriesIndex As Long, ByVal ValueIndex As Long, ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
    Dim nLegCount As Integer, i As Integer
    Dim sItemText As String, sSeriesName As String
    Dim li As TeeChart.ILegendItem
    nLegCount = TChart1.Legend.LastValue

    sSeriesName = TChart1.Series(SeriesIndex).Title

    For i = 0 To nLegCount
         Set li = TChart1.Legend.Item(i)
         sItemText = li.Text
         If (Right(sItemText, 1) = "*") Then
            sItemText = Left(sItemText, Len(sItemText) - 1)
         End If
            
         If (sItemText = sSeriesName) Then
            li.Text = sItemText & "*"
         Else
            li.Text = sItemText
         End If
    Next i
    
End Sub
Click a series, then resize the form. The legend text is detached and floats around.

Posted: Thu Mar 12, 2009 10:08 am
by yeray
Hi LVL,

Yes, it happens because you are changing the legend text in an inappropriate method.

I changed your code and it looks simpler and resizes fine:

Code: Select all

Option Explicit

Dim seriesClicked As Integer

Private Sub Form_Load()
    TChart1.AddSeries scLine
    TChart1.AddSeries scLine
    TChart1.AddSeries scLine
   
    TChart1.Series(0).FillSampleValues 10
    TChart1.Series(0).Title = "Series No. 001"
    TChart1.Series(1).FillSampleValues 10
    TChart1.Series(1).Title = "Series No. 002"
    TChart1.Series(2).FillSampleValues 10
    TChart1.Series(2).Title = "Series No. 003"

  seriesClicked = -1
End Sub

Private Sub Form_Resize()
    TChart1.Top = 0
    TChart1.Left = 0
    TChart1.Width = Me.Width
    TChart1.Height = Me.Height - 1000
End Sub

Private Sub TChart1_OnClickSeries(ByVal SeriesIndex As Long, ByVal ValueIndex As Long, ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)

  seriesClicked = SeriesIndex
  
  TChart1.Environment.InternalRepaint
End Sub

Private Sub TChart1_OnGetLegendText(ByVal LegendStyle As Long, ByVal ValueIndex As Long, LegendText As String)
  If ValueIndex = seriesClicked Then
    LegendText = TChart1.Series(ValueIndex).Title & "*"
  End If
End Sub