https://foro.gambas-es.org/viewtopic.php?f=1&t=6698&p=42650#p42650
-----------------------------------
Grandamakulo
Martes, 31 Julio 2018, 20:48

Re: Graficas En Gambas
-----------------------------------
En mi ansia viva por reinventar la pólvora, he pasado de «chart» —lo que creo que es un error, pero servidor es así— y he hecho un intento bastante chapucero, aunque funcional para uno de mis programas. El resultado es éste:

 [img]https://foro.gambas-es.org/files/images/2876/captura_de_pantalla_de_2018_07_31_21_36_25.png[/img]

El código ni siquiera está completo, aunque confío en ir añadiéndole las propiedades y métodos poco a poco. Es éste —está sin comentar y tiene muchos valores directos que deberían ser parámetros a pasar—:

[code linenumbers=false syntax=gambas]
' Gambas module file

Public TAMANO_LETRA_TITULO As Short = 11

Private ORI_X_REAL As Float
Private ORI_Y_REAL As Float
Private FIN_X_REAL As Float
Private FIN_Y_REAL As Float

Private ORI_X_DIBU As Short
Private ORI_Y_DIBU As Short
Private FIN_X_DIBU As Short
Private FIN_Y_DIBU As Short

Public ColorSeries As New Long[20]

Public COLOR_TITULO As Long = &H603020
Public COLOR_LINEA_PRAL As Long = &HF0D080
Public COLOR_LINEA_SECU As Long = &HFFF0A0
Public COLOR_LINEA_CERO As Long = &HFF8000
Public COLOR_FONDO As Long = &HFFFFC8

' Public COLOR_TITULO As Long = &H00FF00
' Public COLOR_LINEA_PRAL As Long = &H008000
' Public COLOR_LINEA_SECU As Long = &H004000
' Public COLOR_LINEA_CERO As Long = &H00A000
' Public COLOR_FONDO As Long = &H000000

Public Sub Dibujar(Titulo As String, TitX As String, TitY As String, jX As Float[], jY As Float[], Lienzo As Object, Optional Leyenda As String[])
  
  Dim NumSeries As Byte
  Dim LonSeries As Short
  Dim i As Byte
  Dim j As Short
  Dim k As Float
  Dim MinX As Float = jX[0, 0]
  Dim MaxX As Float = jX[0, 0]
  Dim MinY As Float = jY[0, 0]
  Dim MaxY As Float = jY[0, 0]
  Dim Ints As New Float[4]
  Dim TextoLeyenda As String
  
  ColorSeries[0] = &H000000
  ColorSeries[1] = &H0000FF
  ColorSeries[2] = &H00FF00
  ColorSeries[3] = &H00FFFF
  ColorSeries[4] = &HFF0000
  ColorSeries[5] = &HFF00FF
  ColorSeries[6] = &HFFFF00
  ColorSeries[7] = &0000080
  ColorSeries[8] = &H008000
  ColorSeries[9] = &H008080
  ColorSeries[10] = &H800000
  
  NumSeries = jX.Bounds[0]
  LonSeries = jX.Bounds[1]
  
  'Bordes del dibujo
  ORI_X_DIBU = TAMANO_LETRA_TITULO * (Len(TitY) + 6) * 2 / 3
  ORI_Y_DIBU = Lienzo.H - TAMANO_LETRA_TITULO * 4
  FIN_X_DIBU = Lienzo.W - TAMANO_LETRA_TITULO
  FIN_Y_DIBU = TAMANO_LETRA_TITULO * 2
  'Extremos reales
  For i = 0 To NumSeries - 1
    For j = 0 To LonSeries - 1
      If jX[i, j] <MinX> MaxX Then MaxX = jX[i, j]
      If jY[i, j] < MinY And jY[i, j] <15> MaxY Then MaxY = jY[i, j]
    Next
  Next
  
  ' For j = 0 To LonSeries - 1
  '   Debug jX[0, j], jY[0, j]
  ' Next
  
  'TODO: Rehacer los márgenes para que sean aproximados
  ORI_X_REAL = MinX
  FIN_X_REAL = MaxX
  ORI_Y_REAL = MinY
  FIN_Y_REAL = MaxY

  '' Dibujo
  'TODO: Separar la parte de dibujo del resto
  Draw.Begin(Lienzo)
  

  ' Títulos
  Draw.Foreground = COLOR_TITULO
  Draw.Font.Name = "Liberation Mono"
  Draw.Font.Size = TAMANO_LETRA_TITULO
  Draw.Text(Titulo, Lienzo.W / 2 - Len(Titulo) / 2 * TAMANO_LETRA_TITULO * 2 / 3, FIN_Y_DIBU - 2 * TAMANO_LETRA_TITULO)
  Draw.Text(TitX, (FIN_X_DIBU - ORI_X_DIBU) / 2 + ORI_X_DIBU - Len(TitX) / 2 * TAMANO_LETRA_TITULO * 2 / 3, ORI_Y_DIBU + TAMANO_LETRA_TITULO)
  Draw.Text(TitY, 0, (FIN_Y_DIBU - ORI_Y_DIBU) / 2 + ORI_Y_DIBU - Len(TitY) / 2 * TAMANO_LETRA_TITULO)
  
  ' Etiquetas ejes
  Ints = LineasAux(MaxX, MinX)
  Draw.Foreground = COLOR_TITULO
  For k = Ints[1] To MaxX Step Ints[0]
    Draw.Text(Format(k, "0"), KKX(k) - TAMANO_LETRA_TITULO * 2 / 3 * Len(Format(k, "0")) / 2, ORI_Y_DIBU)
  Next
  Ints = LineasAux(MaxY, MinY)
  Draw.Foreground = COLOR_TITULO
  For k = Ints[1] To MaxY Step Ints[0]
    Draw.Text(Format(k, "0.0"), ORI_X_DIBU - Len(Format(k, "0.0")) * TAMANO_LETRA_TITULO * 0.8, KKY(k) - TAMANO_LETRA_TITULO / 2,,, 3)
  Next
  
  ' Leyenda
  For i = 0 To Leyenda.Max
    TextoLeyenda = TextoLeyenda & "<font>" & Leyenda[i] & " </font>"
  Next
  
  'Debug TextoLeyenda
  Draw.RichText(TextoLeyenda, 0, ORI_Y_DIBU + 2 * TAMANO_LETRA_TITULO, Lienzo.Width)
  
  ' Líneas de división
  Ints = LineasAux(MaxX, MinX)
  Draw.Foreground = COLOR_LINEA_SECU
  For k = Ints[3] To MaxX Step Ints[2]
    Draw.Line(KKX(k), ORI_Y_DIBU, KKX(k), FIN_Y_DIBU)    
  Next
  Draw.Foreground = COLOR_LINEA_PRAL
  For k = Ints[1] To MaxX Step Ints[0]
    Draw.Line(KKX(k), ORI_Y_DIBU, KKX(k), FIN_Y_DIBU)    
  Next
  Ints = LineasAux(MaxY, MinY)
  Draw.Foreground = COLOR_LINEA_SECU
  For k = Ints[3] To MaxY Step Ints[2]
    Draw.Line(ORI_X_DIBU, KKY(k), FIN_X_DIBU, KKY(k))    
  Next
  Draw.Foreground = COLOR_LINEA_PRAL
  For k = Ints[1] To MaxY Step Ints[0]
    Draw.Line(ORI_X_DIBU, KKY(k), FIN_X_DIBU, KKY(k))    
  Next
  Draw.Foreground = COLOR_LINEA_PRAL
  Draw.Line(ORI_X_DIBU, ORI_Y_DIBU, FIN_X_DIBU, ORI_Y_DIBU)
  Draw.Line(ORI_X_DIBU, ORI_Y_DIBU, ORI_X_DIBU, FIN_Y_DIBU)
  Draw.Foreground = COLOR_LINEA_CERO
  Draw.Line(ORI_X_DIBU, KKY(0), FIN_X_DIBU, KKY(0))
  
  ' Gráficas
  Draw.LineWidth = 2
  For i = 0 To NumSeries - 1
    Draw.Foreground = ColorSeries[i]
    For j = 1 To LonSeries - 1
      If jY[i, j] <> -15 And jY[i, j - 1] <> -15 Then
        'Draw.Point(KKX(jX[i, j]), KKY(jY[i, j]))
        Draw.Line(KKX(jX[i, j - 1]), KKY(jY[i, j - 1]), KKX(jX[i, j]), KKY(jY[i, j]))
      Endif
    Next
  Next
  Draw.End
  
  
End


Private Function KKX(jX As Float) As Short

  Return (jX - ORI_X_REAL) / (FIN_X_REAL - ORI_X_REAL) * (FIN_X_DIBU - ORI_X_DIBU) + ORI_X_DIBU

End

Private Function KKY(jY As Float) As Short

  Return (jY - ORI_Y_REAL) / (FIN_Y_REAL - ORI_Y_REAL) * (FIN_Y_DIBU - ORI_Y_DIBU) + ORI_Y_DIBU

End
 Public Sub ContG_Draw()


 End
Private Function LineasAux(Xmax As Float, XMin As Float) As Float[]

  Dim A As Float
  Dim B As Float
  Dim C As Integer
  Dim D As Float
  Dim F As Float
  Dim Res As New Float[4]
  
  Dim Pral As Float
  
  A = Xmax - XMin
  If A <0>= 5 And F <10>= 5 And F / 0.5 <10>= 5 And F / 0.2 <10>= 20 And F / 0.5 <50>= 20 And F / 0.2 <50>= 20 And F / 0.1 <= 50 Then
            Pral = 0.1
            Else
              Pral = 0.05
          Endif
      Endif
  Endif
  Res[2] = Pral * 10 ^ C
  Res[3] = Int(Xmin / Res[2] + 1) * Res[2]
  'Debug ("División principal=" & Str(Res[0]) & gb.lf & "Origen principal=" & Str(Res[1]) & gb.lf & "División secundaria=" & Str(Res[2]) & gb.lf & "Origen secundario=" & Str(Res[3]))
  
  Return Res
  
End

[/code]


