C
Chris Hind
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;
chart.Application.DataSheet.Cells[2,2+i] = piSegments;
}
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();
}
}
}
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;
chart.Application.DataSheet.Cells[2,2+i] = piSegments;
}
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();
}
}
}