LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Creating a new Excel chart type

Hi Heath,

Your Add method should work with chart events being triggered in the Class
module. However your code only creates an empty Chart sheet albeit linked to
the already instantiated (up-&-running) Class.

Assuming you want to Add a Worksheet.ChartObject.Chart to the already
initialized Class try something like this.

As before, code in "Class1" with a bit more than last time -

Public WithEvents cht As Excel.Chart

Private Sub cht_Select(ByVal ElementID As Long, _
ByVal Arg1 As Long, ByVal Arg2 As Long)
'' look up "GetChartElement" in help
MsgBox "ElementID " & ElementID & vbCr & _
" Arg1 " & Arg1 & vbCr & " Arg2 " & Arg2
End Sub

Private Sub cht_Calculate()
' change some series value
MsgBox "Chart calculate"
End Sub

Private Sub Class_Initialize()
' get rid of this
MsgBox "Class initialized"
End Sub
'''''''''''''

In a normal module

Public clCht As Class1 ' at top of module

Sub SetUpCht2()
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set clCht = New Class1

' add a ChartObject to Sheet1 and assign the Chart of the
' ChartObject to the class
Set clCht.cht = ws.ChartObjects.Add(50, 40, 200, 100).Chart

' adapted from VBA help, design and set source to the chart
clCht.cht.ChartWizard Source:=ws.Range("A1:B2"), _
Gallery:=xlColumn, Format:=6, PlotBy:=xlColumns, _
CategoryLabels:=1, SeriesLabels:=0, HasLegend:=1

End Sub

Sub CleanUp()
Set clCht = Nothing
End Sub

Instead of Adding' to the Class, in SetUpCht()

Dim myCht as Chart
Set myCht = ws.ChartObjects.Add(50, 40, 200, 100).Chart
design the chart to myCht & instantiate the class as above then

Set clCht.cht = myCht

But for the main object of your exersize, tracking & resizing chart
elements, you still have quite a task. The class calculate event should warn
of the possibility of resize due to value changes but resize can occur with
all sorts of other user changes.

Good luck,
Peter T

"Heath" wrote in message
...
Hi Peter,

thanks again - you rock. I've tried your code and that works fine, but I
still have one more problem. I think I might know the cause, but not the
solution.

Your suggested code in the normal module links the chart object from the
class module to an existing embedded chart in the active sheet. That is,

In a normal module

Public clCht As Class1 'at top of module

Sub SetUpCht()
Set clCht = New Class1
Set clCht.cht = ActiveSheet.ChartObjects(1).Chart
End Sub


What I REALLY want to do is create the chart programatically and override
its drawing behaviour (which will be another can of worms altogether and

may
require some nasty tinkering with its Calculate method since, as your
suggestion reveals, it doesn't seem to expose a Paint method the way .NET
controls do). But that's a whole other issue. If I can trap any events

at
all, then this problem will eventually yeidl to brute force :)

Now, as for creating the chart from code, I have found a way to create a
chart programatically; modifying the code snippet above to

Sub SetUpCht()
Set clCht = New Class1
Set clCht.cht = Charts.Add
End Sub

does the trick, and pops up a nice new chart when it executes. I can
programatically set chart type, labels, data ranges etc. However,

although
the chart appears, the event subs no longer seem to be triggered. I

suspect
that the object returned by the Charts.Add method is not linking properly

to
the clCht.cht withevents object.

I'm wondering if Excel is creating a chart object, embedding it in the
sheet, but not returning a valid reference to its instance.

The problem definitely lies in the "Charts.Add" call not returning an
instance that has events, whereas the "ActiveSheet.ChartObjects(1).Chart"
does (except the chart has to be added manually before the call to the

code
is made, and I want to add the chart via code).

Have you any further pearls of wisdom to cast before me? I really
appreciate your help - you've been generous with your experience and time.

Thanks,
Heath.



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW DO YOU TYPE - OR + IN EXCEL WITHOUT CREATING A FORMULA? jools Excel Discussion (Misc queries) 5 September 19th 06 03:25 PM
Excel x,y chart type x range must be re-entered each series Thomas R. Glass Charts and Charting in Excel 1 February 8th 06 11:23 PM
How to prevent Excel 2003 from automatically change chart type Oscar II Charts and Charting in Excel 2 October 1st 05 02:57 PM
Excel chart type that looks like a car speedometer Club Accountant Charts and Charting in Excel 1 September 23rd 05 01:57 AM
Adding a line Chart Type to a stacked-clustered Chart Type Debbie Charts and Charting in Excel 2 January 5th 05 11:25 PM


All times are GMT +1. The time now is 07:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"