Page 1 of 1

Positioning Custom Vertical Axes

Posted: Tue Mar 31, 2015 8:37 pm
by 9532498
I'm using TChart ActiveX version 7.

Trying to write code to position several vertical custom axes to the right of the chart. The number of these custom axes that will be added will be determined at runtime.

Following the general guidelines outlined in the link below:
http://stackoverflow.com/questions/1469 ... s-creation

However, using the code above, my axes are placed too close together and often an axis is superimposed on top of the title of the previous custom axes.

How do I find the size of the spacing in pixels to properly place a Custom Axis so that the following have been taken into account for the previous custom axis?

1) Length of tickmarks
2) Length of Labels
3) Separation between Labels and Title
4) Length of Title (angled at 90 degrees) to Axis so as to run along axis.

Re: Positioning Custom Vertical Axes

Posted: Wed Apr 01, 2015 10:49 am
by yeray
Hello,

Here it is an example in VB6 considering TickLength, Labels.MaxWidth, the space between Labels and Title (extraPos) and Title length (both horizontal and vertical):

Code: Select all

Private Sub Form_Load() 
  TChart1.Aspect.View3D = False
  TChart1.Legend.CheckBoxes = True
  TChart1.Header.Visible = False
  TChart1.Legend.Alignment = laBottom
  TChart1.Panel.Color = RGB(192, 192, 192)
  
  Dim nSeries As Integer
  Dim i As Integer
  Dim vAxis As Integer
  
  nSeries = 5
  
  For i = 0 To nSeries - 1
    TChart1.AddSeries scLine
    TChart1.Series(i).FillSampleValues 25
    vAxis = TChart1.Axis.AddCustom(False)
    
    With TChart1.Axis.Custom(vAxis)
      .AxisPen.Color = TChart1.Series(i).Color
      .GridPen.Visible = False
      If i > 2 Then
        .Otherside = True
      End If
      .Title.Caption = "Axis Title " + Str$(i)
      .Title.Angle = 90
      .Title.Font.Color = TChart1.Series(i).Color
    End With
    
    TChart1.Series(i).VerticalAxisCustom = vAxis
    TChart1.Axis.Custom(vAxis).PositionUnits = puPixels
  Next i
  
  TChart1.Panel.MarginUnits = muPixels
  TChart1.Environment.InternalRepaint
End Sub

Private Sub PlaceAxis(ByVal nSeries As Integer, ByVal NextXLeft As Integer, ByVal NextXRight As Integer, ByVal MarginLeft As Integer, ByVal MarginRight As Integer)
  Const extraPos As Integer = 5
  'Variable
  Dim MaxLabelsWidth As Integer
  Dim lenghtTicks As Integer
  Dim titleMargin As Integer
  Dim margin As Integer
  
  If TChart1.Series(nSeries).Active Then
    MaxLabelsWidth = TChart1.Axis.Custom(nSeries).Labels.MaxWidth
    lenghtTicks = TChart1.Axis.Custom(nSeries).TickLength
    
    If TChart1.Axis.Custom(nSeries).Title.Angle = 90 Then
      titleMargin = TChart1.Canvas.TextHeight(TChart1.Axis.Custom(nSeries).Title.Caption)
    Else
      titleMargin = TChart1.Canvas.TextWidth(TChart1.Axis.Custom(nSeries).Title.Caption)
    End If
    
    margin = MaxLabelsWidth + lenghtTicks + titleMargin + extraPos
    
    If TChart1.Axis.Custom(nSeries).Otherside Then
      TChart1.Axis.Custom(nSeries).PositionPercent = NextXRight
      NextXRight = NextXRight - margin
      MarginRight = MarginRight + margin
    Else
      TChart1.Axis.Custom(nSeries).PositionPercent = NextXLeft
      NextXLeft = NextXLeft - margin
      MarginLeft = MarginLeft + margin
    End If

    TChart1.Panel.MarginLeft = MarginLeft
    TChart1.Panel.MarginRight = MarginRight
  End If
  
  nSeries = nSeries + 1

  If (nSeries <= TChart1.SeriesCount - 1) Then
    PlaceAxis nSeries, NextXLeft, NextXRight, MarginLeft, MarginRight
  End If
End Sub

Private Sub TChart1_OnAfterDraw()
  PlaceAxis 0, 0, 0, 5, 5
End Sub

Private Sub TChart1_OnClickLegend(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
  TChart1.Environment.InternalRepaint
End Sub

Re: Positioning Custom Vertical Axes

Posted: Wed Apr 01, 2015 3:55 pm
by 9532498
Thanks. That worked for the most part.

I am using TChart ActiveX Pro Version 7. How do I get the width in pixels of the largest label for the axis? Labels.MaxWidth gives me the same number for each of my axes even though the labels are different in width. i.e. some have more decimal places in them than others. When the labels are long (more decimal places) the title for the custom axis seems to overlap the labels and or the next custom axis. So something somewhere in the calculation is not quite adding up.

Re: Positioning Custom Vertical Axes

Posted: Thu Apr 02, 2015 11:40 am
by yeray
Hello,

I've modified the code in the post above with a couple of small fixes.
This is what I get with TeeChart ActiveX v7.0.1.4:
default.png
default.png (27.16 KiB) Viewed 17438 times
If I scroll the chart a bit to see a longer label for the yellow axis, I see the margin on the left gets bigger to fit the new:
scrolled.png
scrolled.png (22.86 KiB) Viewed 17429 times
Debugging I could also see the difference. MaxLabelsWidth for nSeries=2 is 21 without scrolling and 30 when the "1.000" label is shown.

If you still find problems with it, please try to arrange a simple example project we can run as-is to reproduce the problem here.

Re: Positioning Custom Vertical Axes

Posted: Fri Jul 24, 2015 11:27 pm
by 16673658
Is there a reason to call PlaceAxis from the OnAfterDraw() method?

Re: Positioning Custom Vertical Axes

Posted: Mon Jul 27, 2015 7:21 am
by narcis
Hi nbp,

That's to allow the chart to draw all elements so properties used in that method have been initialized and have valid values.