in reply to Prepare charts using Win32::ole
Your code doesn't run. It's far easier for me, at least, to help you if I know what it is you are doing. The sort of problem I have is with your graph data. You have ony one numeric - 22 - and a pie chart with only one value isn't going to be very interesting. If there's other data that gets transformed somehow, it's missing and I can't guess at what it might be, while if you are trying to have the second slice as "TOM" or "AGE", you are destined for disappointment. Compare my code, which you can run from scratch.
I generally, like you, use constant values rather than named Excel constants because I can be consistent about them and VBA constants. I have deliberately deviated in this code both to show you how you might, if you choose, use named Excel constants and how I believe everyone should use "magic number" constants. You will see that when I use 255, I follow it with a comment naming the constant that I am using. Where you use, for example, 70, I have no means of knowing if that might be a typo.
Don't activate sheets unless you really need to. See Excel’s Select and Activate considered harmful.
I have changed only one colour, but I'm sure you can work out from what I have done how to change the rest. However, colours come in two forms in Excel (like everything else), 24 bit colour and the palette (see Re: handling excel conditional formating > color scale). If you want to use the palette, and there are good reasons for doing so if you have users who have set the palette to handle, say, colour blindness, you will need a slightly different incantation. If you need to know how to do this, please reply saying so.
use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add; my $sht = $wb->Sheets(1); $sht->Range("A1")->{Value} = "'PMD"; $sht->Range("A2")->{Value} = "'SOPW"; $sht->Range("A3")->{Value} = "'Meds"; $sht->Range("A4")->{Value} = "'CUFP"; $sht->Range("B1")->{Value} = "3"; $sht->Range("B2")->{Value} = "40"; $sht->Range("B3")->{Value} = "2"; $sht->Range("B4")->{Value} = "1"; my $cht = $wb->Charts->Add; $cht->{ChartType} = xlPie; $cht->SetSourceData ({Source=>$sht->Range("A1:B4"), PlotBy=>xlColumns} +); $cht->PlotArea->{Width} = 350; $cht->PlotArea->{Height} = 350; $cht->SeriesCollection(1)->Points(1)->Interior->{Color} = 255; #vbRed +- NOT an Excel constant, but a VBA constant $cht->Location ({Where=>xlLocationAsObject, Name=>$sht->Name});
Regards,
John Davies
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Prepare charts using Win32::ole
by soumyapanda (Acolyte) on Aug 01, 2011 at 05:11 UTC | |
by davies (Monsignor) on Aug 01, 2011 at 08:47 UTC | |
by soumyapanda (Acolyte) on Aug 05, 2011 at 03:49 UTC |