Page 1 of 1

Problem with Contour Series

Posted: Tue Jan 08, 2008 6:43 pm
by 15047334
The following code display nothing. What's wrong?

Code: Select all

TChart1.ClearChart
TChart1.AddSeries (scContour)

'Read the data 
Set oFs = New FileSystemObject
Dim oTs As TextStream
Set oTs = oFs.OpenTextFile(App.Path & "\xz.csv", ForReading)
oTs.ReadLine
Dim x() As Double
Dim y() As Double
Dim z() As Double
Dim aSplit
Dim sBuf As String
Dim nRec as long
nRec = -1
Do While Not oTs.AtEndOfStream
    nRec = nRec + 1
    sBuf = Trim(oTs.ReadLine)
    If Len(sBuf) = 0 Then
        Exit Do
    End If
    aSplit = Split(sBuf, ",")
    ReDim Preserve x(nRec)
    ReDim Preserve y(nRec)
    ReDim Preserve z(nRec)
    x(nRec) = aSplit(0)
    y(nRec) = aSplit(1)
    z(nRec) = aSplit(2)
Loop

TChart1.Aspect.View3D = False
TChart1.Series(0).asContour.AddArrayXYZ x, y, z

xz.csv:

Code: Select all


X,Y,Z
683077.286,0,248002.373
683099.6342,-0.0011999999987893,248011.7774
683103.8526,-0.0019000000029337,248023.5668
683132.132,0,248033.965
683136.6297,0.0015999999886844,248040.5467
683138.0343,0.0023000000219326,248031.9636
683138.1388,0.0019000000029337,248032.3171
683142.4509,0.0027000000118278,248029.6065
683144.2054,0.0033999999868684,248029.1596
683145.8092,0.0022999999928288,248028.3265
683149.6297,-0.00039999998989515,248072.251
683150.6615,0.0021000000124332,248027.1327
683150.7757,0.0035999999963678,248027.4776
683154.8875,0.0025999999779742,248025.5205
683154.9848,0.0033999999868684,248025.8672
683159.2712,0.0017999999981839,248023.1736
683161.0311,0.0025000000023283,248022.7175
683162.6013,0.00090000001364388,248021.8975
683165.1154,0,248065.881
683165.7058,-0.00020000000949949,248065.6626
683167.4731,0.0010999999940395,248020.7049
683167.5882,0.0015999999886844,248021.0252
683171.6762,0.0007999999797903,248019.0994
683171.7962,0.0016999999934342,248019.4108
683176.0729,0.00089999998454005,248016.7552
683177.8114,0.0010999999940395,248016.2874
683179.4245,-0.001300000003539,248015.4726
683184.2872,0.0014000000082888,248014.2801
683184.3901,0.00070000000414439,248014.5887
683186.1029,0,248057.8044
683186.7446,9.9999975645915E-5,248057.5566
683188.487,0.00070000000414439,248012.6722
683188.5867,0.0016000000177883,248012.9852
683192.8981,0.00039999998989515,248010.3216
683194.6241,0.00090000001364388,248009.8533
683196.247,0.0014000000082888,248009.0429
683201.1585,0.00090000001364388,248008.044
683201.1941,0.00039999998989515,248008.1503
683205.308,0.00059999999939464,248006.2445
683205.4041,0.0022000000171829,248006.5365
683209.7049,0.0010999999940395,248003.8976
683211.4429,0.0026000000070781,248003.4093
683213.0697,0.0010999999940395,248002.6157
683215.5662,0,248046.5687
683216.1651,0,248046.3413
683217.9189,0.00059999999939464,248001.4235
683218.0206,0.0023999999975786,248001.7411
683222.124,0.0010999999940395,247999.8119
683222.2492,0.0026000000070781,248000.1227
683226.5309,0.0014000000082888,247997.4624
683228.2729,0.0022000000171829,247997.0066
683229.8863,0.0016999999934342,247996.1792
683234.7205,0.0013999999791849,247994.9923
683234.8442,0.0025000000023283,247995.3094
683238.9383,0.0011000000231434,247993.3821
683239.0606,0.0023999999975786,247993.692
683243.3324,0.001300000003539,247991.0295
683245.0971,0.0022999999928288,247990.5352
683246.6727,0.0018999999738298,247989.757
683251.579,0.001700000022538,247988.5899
683251.6928,0.0028999999922235,247988.8676
683255.8469,0.0019000000029337,247993.5207
683266.0892,0,248027.2417
683266.5974,-0.00019999998039566,248027.0483

Still having problem with Contour series

Posted: Thu Jan 10, 2008 11:17 am
by 15047334
I receive no reply for my previous message http://www.teechart.net/support/viewtopic.php?t=7152. To make things simple I prepare another sampler. This code does not display any chart. But if you change y = k + Rnd * 0.1 to y = k, it makes a chart. It seems that TeeChart Contour series (as weel as other 3-d plots like Surfeca and Color Grid) very sensitive to data. Guys from Steema, am I wrong?
=======VB6 code==========
Dim i As Long
Dim k As Long
Dim x As Double
Dim y As Double
TChart1.ClearChart
TChart1.AddSeries (scContour)
TChart1.Aspect.View3D = False
TChart1.Series(0).asContour.IrregularGrid = True
Randomize
For i = 1 To 10
x = i
For k = 1 To 10
y = k + Rnd * 0.1
TChart1.Series(0).asContour.AddXYZ x, Sin(x) * Sin(y), y, "", RGB(255, 0, 0)
Next
Next

Posted: Thu Jan 10, 2008 11:50 am
by narcis
Hi AntonXZ,

Thanks for the information. This is most likely because such series styles need to be populated as described here.

Posted: Thu Jan 10, 2008 12:40 pm
by 15047334
Thanks NarcĂ­s, for reply

I checked out the post you mentioned above. It suggests basically the following:
1) set IrregularGrid to true
I already tried it. It doesn't help.
OR/ AND
2) Try to populate series by inverted Y and Z values.
Hm..., it will produce (if produce...) different chart that I need. Anyway,
I've just tried it, but not successfully. Changing population from

Code: Select all

TChart1.Series(0).asContour.AddXYZ x, Sin(x) * Sin(y), y, "", RGB(255, 0,0)
to

Code: Select all

TChart1.Series(0).asContour.AddXYZ x, y,Sin(x) * Sin(y) "", RGB(255, 0, 0) 

Finally, I've resolved the problem.

Posted: Thu Jan 10, 2008 1:45 pm
by 15047334
Finally, I've resolved the problem. I followed suggestion from Marjan Slatinek http://www.teechart.net/support/viewtop ... 4465#24465
In order to get contour chart, you need a regular grid. See demo code in XYZGriddingForm.frm from TeeChartv8FeatureDemo.

My suggestions:
1) Do not use contour series for your data as is. You need to define grid by yourself first.
2) setting IrregularGrid to true does not help in case of irregular grid (Bug?)

Final code:

Code: Select all

Private Sub Form_Load()
Dim i As Long
Dim k As Long
Dim X As Double
Dim y As Double
TChart1.ClearChart
TChart1.AddSeries (scContour)
TChart1.AddSeries (scContour)
TChart1.Aspect.View3D = False
TChart1.Series(0).asContour.IrregularGrid = True
Randomize
For i = 1 To 10
    X = i
    For k = 1 To 10
        y = k + Rnd * 0.00000001
        TChart1.Series(0).asContour.AddXYZ X, Sin(X) * Sin(y), y, "", RGB(255, 0, 0)
    Next
Next
TChart1.Series(0).Active = False
Call Gridding(10, 10)

End Sub

Private Sub Gridding(ByVal GridSizeX As Integer, ByVal GridSizeZ As Integer)
Dim X As Integer, z As Integer
Dim tmpX As Double, tmpZ As Double, tmpMinX As Double, tmpMinZ As Double
Dim tmpXFactor As Double, tmpZFactor As Double
  tmpMinX = TChart1.Series(0).XValues.Minimum
  tmpMinZ = TChart1.Series(0).asContour.ZValues.Minimum

  tmpXFactor = (TChart1.Series(0).XValues.Maximum - tmpMinX) / GridSizeX
  tmpZFactor = (TChart1.Series(0).asContour.ZValues.Maximum - tmpMinZ) / GridSizeZ

TChart1.Axis.Bottom.Labels.Style = talText
'TChart1.Axis.Bottom.Labels.Separation = 10

  With TChart1.Series(1).asContour
    TChart1.Series(1).Clear
    .NumXValues = GridSizeX
    .NumZValues = GridSizeZ

    ' loop all grid cells
    For X = 1 To GridSizeX + 1
      tmpX = tmpMinX + ((X - 1) * tmpXFactor)

      For z = 1 To GridSizeZ + 1
        tmpZ = tmpMinZ + ((z - 1) * tmpZFactor)

        .AddXYZ X, ClosestValue(tmpX, tmpZ), z, "", clTeeColor
         Call TChart1.Axis.Bottom.Labels.Add(X, CStr(tmpX))
         Call TChart1.Axis.Left.Labels.Add(z, CStr(tmpZ))

      Next z
    Next X
  End With
  
End Sub

Private Function ClosestValue(ByVal X As Double, z As Double) As Double
Dim Closer As Long, t As Long
Dim tmpX As Double, tmpZ As Double, dist As Double
Dim MaxDist As Double, result As Double
Dim MaxDistance As Double
  MaxDistance = 1E+300

  MaxDist = MaxDistance
  Closer = -1
  result = 0

  With TChart1.Series(0)
  For t = 0 To .Count - 1
    tmpX = .XValues.Value(t) - X
    tmpZ = .asContour.ZValues.Value(t) - z
    dist = Sqr(tmpX ^ 2 + tmpZ ^ 2)
    If dist < MaxDist Then
      MaxDist = dist
      Closer = t
    End If
  Next t
  End With

  If Closer <> -1 Then result = TChart1.Series(0).YValues.Value(Closer)
  ClosestValue = result
End Function
[/url]