' Demo of Excel charting from J
'
' J writes data in xlfmt into temp\xl.txt
' 961 values are required i.e. 31 x 31 matrix giving 30 x 30 tiles
'
' see script ~system\examples\ole\excel\xldemo.ijs

Function showchart() As String
Charts("Chart1").ChartArea.ClearContents

' *** ensure correct J pathname is used in line below
'     i.e. replace <c:\j5> with the path you are using
jreadfile "c:\j5\temp\xl.txt", "jdemo.xls", "sheet1", 1, 1, 31, 31
' ***

Charts("Chart1").Activate
ActiveChart.Type = xl3DSurface
With ActiveChart
    .Elevation = 20
    .Perspective = 30
    .Rotation = 30
    .RightAngleAxes = False
    .HeightPercent = 50
    .AutoScaling = True
End With
ActiveChart.SeriesCollection.Add Source:="Sheet1!$A$1:$AE$31", Rowcol _
    :=xlRows, SeriesLabels:=False, CategoryLabels:=False, Replace _
    :=False
showchart = "1"
End Function

' jreadfile filename book sheet row cell rowsize colsize
' read file, store values into given range
Function jreadfile(f As String, b, s, _
    r As Integer, c As Integer, w As Integer, h As Integer) As String
Dim d As String
Dim x As Integer, y As Integer, n As Integer
Dim ra As Range, rc As Range
n = FileLen(f)
Open f For Input As #1 Len = 32767
d = Input(n, #1)
Close #1
x = 1
With Workbooks(b).Worksheets(s)
    Set ra = .Range(.Cells(r, c), .Cells(r + w - 1, c + h - 1))
    For Each rc In ra.Cells
        y = InStr(x, d, Chr(10))
        If y = 0 Then y = n + 1
        rc.Value = Mid(d, x, y - x)
        x = y + 1
        If x > n Then x = n + 1
    Next rc
End With
jreadfile = "1"
End Function
