Serving the Quantitative Finance Community

 
User avatar
WaaghBakri
Topic Author
Posts: 1
Joined: March 21st, 2002, 4:07 am

XL: Chart from an Array

June 7th, 2002, 8:25 pm

Hi Folks, This seems to be an embarassingly simple problem but I can't seem to get the damn thing right. I have a 2d array in VBA, say, Dim dataArray(1 To 100, 1 To 100) as Doubleand I want to plot, in Excel, the data contained in the above array. Say, first column versus the 10th column. How do I do it? I find that the "Chart" & "ChartObject" objects have, through the "SeriesCollection" object, methods such as "NewSeries" & "Add" as means to introduce data to the chart. But the above-mentioned methods only accept "Range" objects. Of course, a not so elegant way might be to dump the data onto a hidden sheet and then re-read it through "Range"....but surely there must be a straight forward solution to this fundamental problem? Any help on the matter will be much appreciated...Thanks.Regards, WB.PS. I have 2 Excel VBA books, and neither addresses my dilemma.
 
User avatar
spursfan
Posts: 2
Joined: October 7th, 2001, 3:43 pm

XL: Chart from an Array

June 11th, 2002, 8:59 am

WBi haven't been able to solve it either (that's why i stick to named ranges and macros to automate charting) and it might be that microsoft just didn't really expect people to use functions for everythingmike
 
User avatar
lolillo
Posts: 1
Joined: May 29th, 2002, 8:32 am

XL: Chart from an Array

June 11th, 2002, 4:57 pm

This should work:Charts.Add ActiveChart.ChartType = xlXYScatterActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(1).XValues = "={1,2,3}" ActiveChart.SeriesCollection(1).Values = "={1,4,5}"Of course, you need to write a function that, given an array, returns the string. Anyway that isn´t difficult at all.But there is a problem:The code is OK if you only want to plot a small number of points, but there is a limit on the size of the string.Incredible but true.
 
User avatar
WaaghBakri
Topic Author
Posts: 1
Joined: March 21st, 2002, 4:07 am

XL: Chart from an Array

June 11th, 2002, 6:13 pm

Thanks for your responses. Troublesome issue, complications galore for a simple need!! Lollilo, thats a neat trick I must make a note of. As you point out the string length would limit the approach. Regards - WB.
 
User avatar
Onuk

XL: Chart from an Array

June 12th, 2002, 6:59 am

I use something more or less the same as Lolillo's:Sub woofer()Dim i As Integer, n As Integer, Xs, Ysn = 50ReDim Xs(1 To n) As Double, Ys(1 To n) As DoubleFor i = 1 To n Xs(i) = i Ys(i) = i * iNext iCharts.AddActiveChart.ChartType = xlXYScatterActiveChart.SeriesCollection.NewSeriesActiveChart.SeriesCollection(1).XValues = XsActiveChart.SeriesCollection(1).Values = YsEnd SubIt is a dumb problem. The Values property is a Variant, but the Chart stores it in a string, hence the length restriction. The only functional alternative is to create your own control.
 
User avatar
WaaghBakri
Topic Author
Posts: 1
Joined: March 21st, 2002, 4:07 am

XL: Chart from an Array

June 12th, 2002, 6:40 pm

Thanks Onuk. As I read your example, I realized that I had missed a stating a key point in stating my problem - I wanted to embed the chart in a worksheet, ie. I wanted to use a "ChartObject" My apologies for not having stated that earlier. As I experimented last week, I recall trying something along the lines you've suggested, ie. plotting on a chartsheet. And it worked fine. But when I experimented with plotting an array in a chartObject, I failed. Theoretically, it seems it should work since "chart" is a member of "chartObject."Well, many thanks for your example. I'll expt. with it through a chartobject. If you do know how to plot a data array on an embedded chart please do let me know. I'd be much obliged. Thanks.