PC Review


Reply
Thread Tools Rate Thread

Changing pie chart colours

 
 
Chris Hind
Guest
Posts: n/a
 
      8th Aug 2003
I'm trying to use the Primary Interop Assemblies for Office XP to
create a pie chart in Powerpoint in C#. I've managed to create the
chart and set up the data, but I cannot find a way to set the colours
of the various pie slices, nor can I tell it that I want the slices
drawn in solid colour (ie without a border). Can anyone help me? I
don't necessarily need C#: some (non-.NET) VB to do this would be an
invaluable aid!

I've read all sorts of FAQs and object models and so on, and the only
thing I've found which will change a colour is

Graph.Point point = (Graph.Point)s.Points(1);
point.Interior.ColorIndex = 1;

after where I set up the Series, s. This lets me choose a colour from
a predefined palette - but how do I set the colours in that palette?
Attempting to use the "Color" property of point.Interior throws an
exception (COMException, Unable to set the Color property of the
Interior class).

With regards to the border, the obvious try

Graph.Point point = (Graph.Point)s.Points(1);
point.Border.Weight = 0;

gives "COMException: Unable to set the Weight property of the Border
class".

Here's my code so far:

using System;
using System.Diagnostics;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using Graph = Microsoft.Office.Interop.Graph;
using System.Reflection;

namespace PowerpointWriter
{
class Class1
{
static int[] piSegments = {34,60,2,3,1};
static string[] labels = {"USA", "Europe", "Australia",
"Japan","Other"};
static string[] colors =
{"#3366FF","#000000","#000000","#000000","#000000"};

[STAThread]
static void Main(string[] args)
{
PowerPoint.Application powerpoint =
new PowerPoint.ApplicationClass();
powerpoint.Visible = MsoTriState.msoTrue;

PowerPoint.Presentation presentation =
powerpoint.Presentations.Add(MsoTriState.msoTrue);
PowerPoint.Slide slide =
presentation.Slides.Add(1,PowerPoint.PpSlideLayout.ppLayoutBlank);

Graph.Chart chart =
(Graph.Chart)slide.Shapes.AddOLEObject(150,150,480,320,
"MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",
MsoTriState.msoFalse).OLEFormat.Object;

chart.ChartType = Graph.XlChartType.xlPie;
Graph.DataSheet data = chart.Application.DataSheet;
chart.HasLegend = false;
chart.HasTitle = true;
chart.ChartTitle.Text = "Geographical concentration";

Graph.DataSheet ds = chart.Application.DataSheet;
ds.Cells.Delete(Missing.Value);
for(int i=0; i<piSegments.Length; i++)
{
chart.Application.DataSheet.Cells[1,2+i] = labels[i];
chart.Application.DataSheet.Cells[2,2+i] = piSegments[i];
}

chart.PlotArea.ClearFormats();
Graph.ChartGroup chartgroup =(Graph.ChartGroup)chart.PieGroups(1);
chartgroup.FirstSliceAngle = 80;

Graph.Series s = (Graph.Series)chart.SeriesCollection(1);
s.ClearFormats();

s.ApplyDataLabels(Graph.XlDataLabelsType.xlDataLabelsShowLabel,
false, false, true, false, true, false, true, true, "\n");
for(int i=1; i<=labels.Length; i++)
{
Graph.DataLabel dl = (Graph.DataLabel)s.DataLabels(i);
dl.NumberFormat = "0.0%";
dl.Font.Name = "Arial";
dl.Font.Size = 10;
dl.Font.Bold = true;
}
chart.Application.Update();

presentation.SaveAs(@"C:\Documents and
Settings\hindc\Desktop\helloworld.ppt",
PowerPoint.PpSaveAsFileType.ppSaveAsDefault,
MsoTriState.msoFalse);
presentation.Close();
powerpoint.Quit();
}
}
}
 
Reply With Quote
 
 
 
 
Colleen
Guest
Posts: n/a
 
      9th Aug 2003
If you just want to change the colors of the chart elements, rather than
changing the whole palette, you can. (I do not know how to do it using VB.)
Just right click on the pie slice and choose to edit the data point, then
you can apply fill colors, borders, labels and angles, etc. You can even
fill the slice with an image by choosing fill effects, then clicking the
picture tab.

Colleen

"Chris Hind" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm trying to use the Primary Interop Assemblies for Office XP to
> create a pie chart in Powerpoint in C#. I've managed to create the
> chart and set up the data, but I cannot find a way to set the colours
> of the various pie slices, nor can I tell it that I want the slices
> drawn in solid colour (ie without a border). Can anyone help me? I
> don't necessarily need C#: some (non-.NET) VB to do this would be an
> invaluable aid!
>
> I've read all sorts of FAQs and object models and so on, and the only
> thing I've found which will change a colour is
>
> Graph.Point point = (Graph.Point)s.Points(1);
> point.Interior.ColorIndex = 1;
>
> after where I set up the Series, s. This lets me choose a colour from
> a predefined palette - but how do I set the colours in that palette?
> Attempting to use the "Color" property of point.Interior throws an
> exception (COMException, Unable to set the Color property of the
> Interior class).
>
> With regards to the border, the obvious try
>
> Graph.Point point = (Graph.Point)s.Points(1);
> point.Border.Weight = 0;
>
> gives "COMException: Unable to set the Weight property of the Border
> class".
>
> Here's my code so far:
>
> using System;
> using System.Diagnostics;
> using PowerPoint = Microsoft.Office.Interop.PowerPoint;
> using Microsoft.Office.Core;
> using Graph = Microsoft.Office.Interop.Graph;
> using System.Reflection;
>
> namespace PowerpointWriter
> {
> class Class1
> {
> static int[] piSegments = {34,60,2,3,1};
> static string[] labels = {"USA", "Europe", "Australia",
> "Japan","Other"};
> static string[] colors =
> {"#3366FF","#000000","#000000","#000000","#000000"};
>
> [STAThread]
> static void Main(string[] args)
> {
> PowerPoint.Application powerpoint =
> new PowerPoint.ApplicationClass();
> powerpoint.Visible = MsoTriState.msoTrue;
>
> PowerPoint.Presentation presentation =
> powerpoint.Presentations.Add(MsoTriState.msoTrue);
> PowerPoint.Slide slide =
> presentation.Slides.Add(1,PowerPoint.PpSlideLayout.ppLayoutBlank);
>
> Graph.Chart chart =
> (Graph.Chart)slide.Shapes.AddOLEObject(150,150,480,320,
> "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",
> MsoTriState.msoFalse).OLEFormat.Object;
>
> chart.ChartType = Graph.XlChartType.xlPie;
> Graph.DataSheet data = chart.Application.DataSheet;
> chart.HasLegend = false;
> chart.HasTitle = true;
> chart.ChartTitle.Text = "Geographical concentration";
>
> Graph.DataSheet ds = chart.Application.DataSheet;
> ds.Cells.Delete(Missing.Value);
> for(int i=0; i<piSegments.Length; i++)
> {
> chart.Application.DataSheet.Cells[1,2+i] = labels[i];
> chart.Application.DataSheet.Cells[2,2+i] = piSegments[i];
> }
>
> chart.PlotArea.ClearFormats();
> Graph.ChartGroup chartgroup =(Graph.ChartGroup)chart.PieGroups(1);
> chartgroup.FirstSliceAngle = 80;
>
> Graph.Series s = (Graph.Series)chart.SeriesCollection(1);
> s.ClearFormats();
>
> s.ApplyDataLabels(Graph.XlDataLabelsType.xlDataLabelsShowLabel,
> false, false, true, false, true, false, true, true, "\n");
> for(int i=1; i<=labels.Length; i++)
> {
> Graph.DataLabel dl = (Graph.DataLabel)s.DataLabels(i);
> dl.NumberFormat = "0.0%";
> dl.Font.Name = "Arial";
> dl.Font.Size = 10;
> dl.Font.Bold = true;
> }
> chart.Application.Update();
>
> presentation.SaveAs(@"C:\Documents and
> Settings\hindc\Desktop\helloworld.ppt",
> PowerPoint.PpSaveAsFileType.ppSaveAsDefault,
> MsoTriState.msoFalse);
> presentation.Close();
> powerpoint.Quit();
> }
> }
> }



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing cell colours on a pie chart acprc Microsoft Excel Worksheet Functions 0 5th May 2010 11:00 AM
Changing bar chart colours depending on values SammiH Microsoft Access Reports 0 10th Feb 2009 02:13 PM
changing colours on chart series JillC Microsoft Excel Programming 0 25th Sep 2008 01:02 PM
Changing chart colours using VBA =?Utf-8?B?VG9tbW9VSw==?= Microsoft Excel Programming 2 8th Feb 2006 06:59 PM
changing colours on stacked bar chart sphenisc Microsoft Excel Programming 1 10th Jun 2004 03:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:09 AM.