' Gambas module file

Public ColorList As Integer[] = [&hff6f00,
&h800080, &h666666, &h6B4794, &h290099, &h7da647,
&he6e64c, &hff9966, &h000080, &hfff0ff, &h00ffff, &h9999ff]

Public Function GetChartMaxValue(sidChart As String) As Float
  Dim hSeries As _CSerie
  Dim f As Float
  Dim fCurMax As Float
  fCurMax = 0
  For Each hSeries In Chart._$ColChart[sidChart]
    For Each f In hSeries
      If fCurMax < f Then fCurMax = f
    Next
  Next
  Return fCurMax
  
End

Public Function GetChartMaxCumulateValue(sidChart As String) As Float
  Dim hSeries As _CSerie
  Dim f As Float
  Dim fCurMax As Float
  Dim i As Integer
  fCurMax = 0
  For i = 0 To Chart._$ColChart[sidChart].Count - 1
    f = 0
    For Each hSeries In Chart._$ColChart[sidChart]
      f += hSeries[i]
    Next
    If fCurMax < f Then fCurMax = f
  Next
  Return fCurMax
End

Public Function GetChartMaxCumulate(sidChart As String) As Float
  
  Dim f, fMaxValue As Float
  Dim hSeries As _CSerie
  Dim i As Integer
  
  For Each hSeries In Chart._$ColChart[sidChart]
    f = 0
    For i = 0 To Chart._$ColChart[sidChart].Count - 1
      f += hSeries[i]
    Next
    If fMaxValue < f Then fMaxValue = f
  Next
  Return fMaxValue
End



Public Function DrawChartAxes(sidChart As String, hChartRect As CRect, fMinValue As Float, fMaxValue As Float, Optional iAlign As Integer) As CPoint
  Dim fTabPos As Float
  Dim i, iStaticSpace As Integer
  Dim fXUnit, fYUnit As Float
  Dim hPoint As CPoint
  Dim f As Float
  Dim fStep As Float
  Dim fMultiple As Float
  Dim oSerie As _CSerie
  Dim iXStep, cnt As Integer
  Dim bBascule As Boolean
    'Draw the Y axe
  iStaticSpace = 5 * Chart._$ColChart[sidChart]._fProportionnal

  

  

'mise en place du pas

  fMultiple = Round(fMaxValue / 100)
  If fMultiple = 0 Then fMultiple = 1
  If Chart._$ColChart[sidChart].YAxe.AutoScale Then
    If fMaxValue > 1 Then 
      
      If fMaxValue / fMultiple < 10 Then 
        fStep = 0.5 * fMultiple
      Else If fMaxValue / fMultiple < 25 Then 
        fStep = 1 * fMultiple
      Else If fMaxValue / fMultiple < 50 Then 
        fStep = 2 * fMultiple
      Else If fMaxValue / fMultiple < 75 Then 
        fStep = 3 * fMultiple
      'ELSE IF fMaxValue / fMultiple < 90 THEN 
        'fStep = 4 * fMultiple
      Else If fMaxValue / fMultiple <= 100 Then 
        fStep = 5 * fMultiple
      Endif
     Endif
    Else
      fStep = Chart._$ColChart[sidChart].YAxe.Step
    Endif
    
'Endif

  'Redefinition de la position basse
  If Chart._$ColChart[sidChart].XAxe.Visible Then
    If Chart._$ColChart[sidChart].XAxe.Arrange = Chart._$ColChart[sidChart].XAxe.Steps Then 
      hChartRect.Bottom = hChartRect.Bottom - (iStaticSpace * 2 + Draw.Font.Height)
      hChartRect.Top = hChartRect.Top '+ (iStaticSpace * 2 + Draw.Font.Height)
    Else
      hChartRect.Bottom = hChartRect.Bottom - (Draw.Font.Height)
      hChartRect.Top = hChartRect.Top '+ (Draw.Font.Height)
    Endif
  Endif
  draw.Font = Chart._$ColChart[sidChart].XAxe.Font
  draw.Font.Size = Chart._$ColChart[sidChart].XAxe.Font.Size * Chart._$ColChart[sidChart]._fProportionnal
  
  'To manage the last Xaxe label length
  If iAlign = Align.Center Then

    hChartRect.Right -= draw.TextWidth(Chart._$ColChart[sidChart][Chart._$ColChart[sidChart].CountDataSets - 1].Text) / 2
  Else
    hChartRect.Right -= draw.TextWidth(Chart._$ColChart[sidChart].Headers.Values[Chart._$ColChart[sidChart].Headers.Values.Max]) / 2
  Endif
  
  
  If Chart._$ColChart[sidChart].YAxe.Visible Then
    cnt = draw.TextWidth(fMaxValue & "0")
    
    hChartRect.Left = hChartRect.Left + iStaticSpace * Chart._$ColChart[sidChart]._fProportionnal + cnt
  Endif
  
  'Draw the background
  
  Draw.FillColor = Chart._$ColChart[sidChart].BackGround
  Draw.FillStyle = Fill.Solid
  Draw.Rect(hChartRect.Left, hChartRect.Top, hChartRect.Right - hChartRect.Left, hChartRect.Bottom - hChartRect.Top + 1)
  Draw.ForeGround = Color.Black
  
  fYUnit = 1
  Try fYUnit = (hChartRect.Bottom - hChartRect.Top) / (fMaxValue - fMinValue)

  Draw.Line(hChartRect.Left, hChartRect.Top, hChartRect.Left, hChartRect.Bottom)
'Dessin de l'axe Y
  Draw.Font = Chart._$ColChart[sidChart].YAxe.Font
  Draw.Font.Size = Chart._$ColChart[sidChart].YAxe.Font.Size * Chart._$ColChart[sidChart]._fProportionnal  
  For f = fMinValue To fMaxValue Step fStep 'Round(fMaxValue / 20)
    'Position du taquet
    fTabPos = hChartRect.Bottom - (fYUnit * f - (fYUnit * fMinValue))
    'Dessine les intervalles Verticaux
    If Chart._$ColChart[sidChart].YAxe.ShowIntervalLines Then Draw.Line(hChartRect.Left, fTabPos, hChartRect.Right, fTabPos)
    'Dessine l'étiquette
    If Chart._$ColChart[sidChart].YAxe.Visible Then 
      'Dessine le taquet

      Draw.Line(hChartRect.Left - iStaticSpace, fTabPos, hChartRect.Left, fTabPos)
      Draw.Text(f, hChartRect.Left - iStaticSpace * Chart._$ColChart[sidChart]._fProportionnal - Draw.TextWidth(f & " "), fTabPos - Draw.Font.Height / 2)
    Endif
  Next
  
  'IF $iType = ChartType.AreaPercent THEN fMaxVal = GetChartMaxCumulateValue(Chart)
  
  
  'Draw the X axe
  Draw.Line(hChartRect.Left, hChartRect.Bottom, hChartRect.Right, hChartRect.Bottom)
  
  If iAlign = Align.Center Then 
    fXUnit = ((hChartRect.Right - hChartRect.Left) / (Chart._$ColChart[sidChart].Count + 1)) / Chart._$ColChart[sidChart].CountDataSets
  Else 
    fXUnit = ((hChartRect.Right - hChartRect.Left) / (Chart._$ColChart[sidChart].Count - 1))
  Endif
  fTabPos = hChartRect.Left 
  
  Draw.Font = Chart._$ColChart[sidChart].XAxe.Font
  Draw.Font.Size = Chart._$ColChart[sidChart].XAxe.Font.Size * Chart._$ColChart[sidChart]._fProportionnal
  

  
  If iAlign = Align.Center Then 

  
  For Each oSerie In Chart._$ColChart[sidChart]
      fTabPos += fXUnit * (Chart._$ColChart[sidChart].Count + 1)
        Draw.Line(fTabPos, hChartRect.Bottom, fTabPos, hChartRect.Bottom + iStaticSpace)
      If Chart._$ColChart[sidChart].XAxe.Visible Then 
        
        If bBascule Then iXStep = Draw.Font.Height / 2
        
        Draw.Text(oSerie.Text, fTabPos - (fXUnit * (Chart._$ColChart[sidChart].Count + 1)) / 2 - Draw.TextWidth(oSerie.Text) / 2, hChartRect.Bottom + iStaticSpace + iXStep, Align.Center)
        
        If Chart._$ColChart[sidChart].XAxe.Arrange = Chart._$ColChart[sidChart].XAxe.Steps Then
          bBascule = Not bBascule
        Endif
        
        iXStep = 0
      Endif
  Next
  Else
  For i = 0 To Chart._$ColChart[sidChart].Count - 1
  
     '* '(Chart.Count + 1)
   
    

    Draw.Line(fTabPos, hChartRect.Bottom, fTabPos, hChartRect.Bottom + 2 * Chart._$ColChart[sidChart]._fProportionnal)
    If Chart._$ColChart[sidChart].XAxe.Visible Then 
      If bBascule Then iXStep = Draw.Font.Height / 2
      Draw.Text(Chart._$ColChart[sidChart].Headers[i], fTabPos - Draw.TextWidth(Chart._$ColChart[sidChart].Headers[i]) / 2, hChartRect.Bottom + iStaticSpace + iXStep, Align.Center)
      If Chart._$ColChart[sidChart].XAxe.Arrange = Chart._$ColChart[sidChart].XAxe.Steps Then
          bBascule = Not bBascule
      Endif
        
      iXStep = 0
    Endif
    fTabPos += fXUnit

  Next
Endif
  hPoint = New CPoint(fXUnit, fYUnit)
  hPoint.Rect = hChartRect
  Return hPoint
End


Public Sub DrawSymbol(sidChart As String, iType As Integer, X As Integer, Y As Integer)
  Dim ariSymbol As Integer[]
  Dim iStaticSpace As Integer = 10 * Chart._$ColChart[sidChart]._fProportionnal
  X -= iStaticSpace / 2
  Y -= iStaticSpace / 2
  Select Case iType
    Case 0 'carré
     ariSymbol = [X, Y, X + iStaticSpace, Y, X + iStaticSpace, Y + iStaticSpace, X, Y + iStaticSpace]
     Draw.Polygon(ariSymbol)
    Case 1 'cercle
      Draw.Circle(X + iStaticSpace / 2, Y + iStaticSpace / 2, iStaticSpace / 2)
    Case 2 'Triangle
      ariSymbol = [X + CInt(iStaticSpace / 2), Y, X + iStaticSpace, Y + iStaticSpace, X, Y + iStaticSpace]
      Draw.Polygon(ariSymbol)
    Case 3 'TriangleInverse
      ariSymbol = [X + CInt(iStaticSpace / 2), Y + iStaticSpace, X + iStaticSpace, Y, X, Y]
      Draw.Polygon(ariSymbol)
    Case 4 'sablier
      ariSymbol = [X, Y, X + iStaticSpace, Y, X, Y + iStaticSpace, X + iStaticSpace, Y + iStaticSpace]
      Draw.Polygon(ariSymbol)
    Case 5 'sablierH
      
  End Select
  
End

Public Function DrawVertChartAxes(sidChart As String, hChartRect As CRect, fMinValue As Float, fMaxValue As Float, Optional iAlign As Integer) As CPoint
  Dim fTabPos As Float
  Dim i, iStaticSpace As Integer
  Dim fXUnit, fYUnit As Float
  Dim hPoint As CPoint
  Dim f As Float
  Dim fStep As Float
  Dim fMultiple As Float
  Dim oSerie As _CSerie
  Dim iXStep As Integer
  Dim bBascule As Boolean
    'Draw the Y axe
  iStaticSpace = 5 * Chart._$ColChart[sidChart]._fProportionnal

  
  fMultiple = Round(fMaxValue / 100)
  If fMultiple = 0 Then fMultiple = 1
  If fMaxValue > 1 Then 
    
    If fMaxValue / fMultiple < 10 Then 
      fStep = 0.5 * fMultiple
    Else If fMaxValue / fMultiple < 25 Then 
      fStep = 1 * fMultiple
    Else If fMaxValue / fMultiple < 50 Then 
      fStep = 2 * fMultiple
    Else If fMaxValue / fMultiple < 75 Then 
      fStep = 3 * fMultiple
    'ELSE IF fMaxValue / fMultiple < 90 THEN 
      'fStep = 4 * fMultiple
    Else If fMaxValue / fMultiple <= 100 Then 
      fStep = 5 * fMultiple
    Endif
    
Endif


'Redefinition de la position basse
  If Chart._$ColChart[sidChart].XAxe.Visible Then
    If Chart._$ColChart[sidChart].XAxe.Arrange = Chart._$ColChart[sidChart].XAxe.Steps Then 
      hChartRect.Bottom = hChartRect.Bottom - (iStaticSpace * 2 + Draw.Font.Height)
      hChartRect.Top = hChartRect.Top + (iStaticSpace * 2 + Draw.Font.Height)
    Else
      hChartRect.Bottom = hChartRect.Bottom - (Draw.Font.Height)
      hChartRect.Top = hChartRect.Top + (Draw.Font.Height)
    Endif
  Endif
  
  Draw.Line(hChartRect.Left, hChartRect.Top, hChartRect.Left, hChartRect.Bottom)
  
  fYUnit = (hChartRect.Right - hChartRect.Left) / (fMaxValue)
  Draw.Font = Chart._$ColChart[sidChart].XAxe.Font
  Draw.Font.Size = Chart._$ColChart[sidChart].XAxe.Font.Size * Chart._$ColChart[sidChart]._fProportionnal
  For f = 0 To fMaxValue Step fStep 'Round(fMaxValue / 20)
    'Position du taquet
    fTabPos = hChartRect.Left + (fYUnit * f)
    
    'Draw.Line(hChartRect.Left - iStaticSpace, fTabPos, hChartRect.Left, fTabPos)
    'Dessine les intervalles Verticaux
    If Chart._$ColChart[sidChart].YAxe.ShowIntervalLines Then Draw.Line(fTabPos, hChartRect.Bottom, fTabPos, hChartRect.Top)
    'Dessine l'étiquette
    'Draw.Text(f, hChartRect.Left - iStaticSpace * oChart._fProportionnal - Draw.TextWidth(f & "  "), fTabPos - Draw.Font.Height / 2)

        

        

    If Chart._$ColChart[sidChart].XAxe.Visible Then 
      If bBascule Then iXStep = Draw.Font.Height 
      Draw.Text(f, fTabPos - Draw.Font.TextWidth(f) / 2, hChartRect.Bottom + iStaticSpace + iXStep)
      If Chart._$ColChart[sidChart].XAxe.Arrange = Chart._$ColChart[sidChart].XAxe.Steps Then
          bBascule = Not bBascule
      Endif
      iXStep = 0
      'Dessine le taquet
      Draw.Line(fTabPos, hChartRect.Bottom - iStaticSpace, fTabPos, hChartRect.Bottom)
    Endif
  Next
  
  'If $iType = ChartType.AreaPercent Then fMaxVal = GetChartMaxCumulateValue(Chart)
  
  
 'Draw the X axe
 Draw.Line(hChartRect.Left, hChartRect.Bottom, hChartRect.Right, hChartRect.Bottom)
 
 If iAlign = Align.Center Then 
   fXUnit = ((hChartRect.Bottom - hChartRect.Top) / (Chart._$ColChart[sidChart].Count + 1)) / Chart._$ColChart[sidChart].CountDataSets
 Else 
   fXUnit = ((hChartRect.Bottom - hChartRect.Top) / (Chart._$ColChart[sidChart].Count - 1))
 Endif
 fTabPos = hChartRect.Bottom
 If iAlign = Align.Center Then 
 Draw.Font = Chart._$ColChart[sidChart].YAxe.Font
 Draw.Font.Size = Chart._$ColChart[sidChart].YAxe.Font.Size * Chart._$ColChart[sidChart]._fProportionnal
 For Each oSerie In Chart._$ColChart[sidChart]
      
     fTabPos -= fXUnit * (Chart._$ColChart[sidChart].Count + 1)
       Draw.Line(hChartRect.Left, fTabPos, hChartRect.Left - iStaticSpace, fTabPos)
     If Chart._$ColChart[sidChart].YAxe.Visible Then Draw.Text(oSerie.Text, hChartRect.Left - Draw.TextWidth(oSerie.Text) - iStaticSpace, fTabPos + (fXUnit * (Chart._$ColChart[sidChart].Count + 1)) / 2 + Draw.TextHeight(oSerie.Text) / 2)
 Next
 Else
 For i = 0 To Chart._$ColChart[sidChart].Count - 1
 
    '* '(Chart.Count + 1)
  
   
 
   Draw.Line(fTabPos, hChartRect.Bottom, fTabPos, hChartRect.Bottom + 2 * Chart._$ColChart[sidChart]._fProportionnal)
   If Chart._$ColChart[sidChart].YAxe.Visible Then Draw.Text(Chart._$ColChart[sidChart].Headers[i], fTabPos - Draw.TextWidth(Chart._$ColChart[sidChart].Headers[i]) / 2, hChartRect.Bottom + iStaticSpace, Align.Center)
   fTabPos += fXUnit
 
 Next
Endif
  hPoint = New CPoint(fXUnit, fYUnit)
  Return hPoint
End


Public Function GetChartMaxLenLabel(sidChart As String) As String
  
  Dim hSeries As _CSerie
  Dim s As String
  Dim sCurMax As String
  
  sCurMax = 0
  For Each hSeries In Chart._$ColChart[sidChart]
    If Len(hSeries.Text) > Len(sCurMax) Then sCurMax = hSeries.Text
  Next
  Return sCurMax
  
End

