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


In reply to Re: Prepare charts using Win32::ole by davies
in thread Prepare charts using Win32::ole by soumyapanda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.