Using Variable array in Excel VBA charting
I doubt getting data from cells is what's slowing things down. Following
makes a chart with 5 x 1000xy series (ie 10k data cells) pretty quickly,
even in Excel 2007 it took less than 0.5 sec (in an old system), and most of
that time was running the last line to reset screenupdating.
Option Explicit
Sub SpiralTest()
Dim xA As Double, yA As Double
Dim xK As Double, yK As Double
Dim pts As Long
Dim rad As Double, rdn As Double
Dim nS As Long
Dim arr() As Double
Dim rng As Range
Dim cht As Chart
On Error Resume Next
' << just for testing
ActiveSheet.ChartObjects.Delete
ActiveSheet.UsedRange.ClearContents
On Error GoTo 0
Application.ScreenUpdating = False
Set cht = ActiveSheet.ChartObjects.Add(10, 10, 300, 600).Chart
cht.ChartType = xlXYScatterSmoothNoMarkers
cht.HasLegend = False
rdn = Application.WorksheetFunction.Pi / 180
pts = 1080
rad = 1
ReDim arr(1 To pts, 1 To 2)
For nS = 1 To 5
If nS = 1 Then
xA = 0: yA = -40: xK = 3: yK = 6
ElseIf nS = 2 Then
xA = 0: yA = 0: xK = 6: yK = 6
ElseIf nS = 3 Then
xA = 0: yA = -20: xK = 6: yK = 4
ElseIf nS = 4 Then
xA = 0: yA = 20: xK = 6: yK = 18
ElseIf nS = 5 Then
xA = 0: yA = 40: xK = 6: yK = 30
End If
MakeSpiral rdn, rad, pts, xA, yA, xK, yK, arr
Set rng = ActiveSheet.Cells(1, nS * 2 - 1).Resize(pts, 2)
rng.Value = arr
With cht.SeriesCollection.NewSeries
If Val(Application.Version) = 12 Then
.ChartType = xlXYScatterSmoothNoMarkers
.Border.Weight = xlThin
End If
.XValues = "=" & Application.ConvertFormula( _
rng.Columns(1).Address(external:=True), xlA1, xlR1C1)
.Values = "=" & Application.ConvertFormula( _
rng.Columns(2).Address(external:=True), xlA1, xlR1C1)
End With
Next
Application.ScreenUpdating = True
End Sub
Function MakeSpiral(rdn As Double, rad As Double, pts As Long, _
xA As Double, yA As Double, _
xK As Double, yK As Double, _
arr() As Double)
Dim i As Long
For i = 1 To pts
arr(i, 1) = xA + (Cos(i * xK * rdn) * rad * i * 0.01)
arr(i, 2) = yA + (Sin(i * yK * rdn) * rad * i * 0.01)
Next
End Function
Jon - so what's up with the the 2007 chart macro recorder. How to do a
simple little thing like format the line weight (or rather points width).
Regards,
Peter T
"hsPipe" wrote in message
...
Thanks for the response. Transferring original input to variable arrays,
process them and writing them back to the spread sheet using the method
you
suggested is what I had done. It took about 10 seconds to process a
tremendous amount of data.
However, when I tred to plot the data in a series of graphs in an active
sheet, the process slows down to 70 seconds. I was wondering whether it
it
the time consumed in getting the data from the spread sheet that slows
down
the plotting. And hence the idea of plotting directly from the variable
arrays.
May be I have to accept the delay from 30 seconds to 70 seconds for the
graphs?
--
Ho-Shu
"Peter T" wrote:
Indeed you can make charts with no source data in cells, but with your
1000
x-y points it's not straightforward and would require quite a lot of work
(it entails writing the data to named "vertical" arrays). If the only
reason
is to save time, writing data to 1000x2 cells is barely an eye-blink if
you
do it in one go -
ReDim arr(1 To 1000, 1 To 2) As Double
populate the array with x-y values
Range("a1").Resize(UBound(arr), UBound(arr, 2)).Value = arr
Regards,
Peter T
"hsPipe" wrote in message
...
I am trying to plot the generated data (over 1000 x-y scatter points)
from
the variable arrays directly rather than depositing the array to the
spread
sheet and than plotting it. This is in the hope that it will reduce
the
time
required to plot the chart.
I need help on how to specify the series using arrays rather than cell
ranges.
Than you.
--
Ho-Shu
|