Calendar

<<  août 2008  >>
lumamejevesadi
28293031123
45678910
111214151617
181920222324
25262728293031
1234567

View posts in large calendar
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

(mars 26, 2008 09:00)

Second article of the serie. Now I need to control a chart and to create some series.

Note that in my case, I have added a graph (empty) on a page, to be sure of the position and size (even if we can also control that by code). As a consequence, I will control the graph Globals.MySheet.MyGraph and I will store it in a variable chart.

Microsoft.Office.Tools.Excel.Chart chart = Globals.MySheet.MyChart;

How to add the source data of a graph ?

There is many way to add the source data of a graph. The simplest ? to use the SetSourceData method.

chart.SetSourceData(sheet.Range["B1", "B4"], XlRowCol.xlColumns);

chart.SetSourceData(sheet.Range["B1", "C4"], XlRowCol.xlColumns);

Here it's a little bit tricky to understand. In the first case, Excel will construct one serie to the graph. It will also try to extract the title from the data. How ? well it will probably check the type of the data to see, but it's a bit "magic". In the second case, it will construct two different series.

How to manipulate the series ?

The "simplest" and the most powerful way is to work directly with the series. You will note here the use of "System.Type.Missing". Indeed this is an optional parameter. When giving a string or an int, it will return a single Series. To get the wole collection, we have to omit this parameter.

SeriesCollection series = chart.SeriesCollection(System.Type.Missing) as SeriesCollection;

 

//Here "B2;B4" will be the range containing the data

Series serie = series.Add(sheet.Range["B2", "B4"], XlRowCol.xlColumns, false, false, false);

//and "A2;A4" will be the range containing the labels

serie.XValues = sheet.Range["A2", "A4"];

serie.Name = "Name of my first serie";

 

serie = series.Add(sheet.Range["C2", "C4"], XlRowCol.xlColumns, false, false, false);

serie.XValues = sheet.Range["A2", "A4"];

//Here we specify a secondary axe

serie.AxisGroup = XlAxisGroup.xlSecondary;

serie.Name = "Name of my second serie";

How to manipulate the axis ? 

//Let's take the left axis

Excel.Axis axis = (Excel.Axis)chart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);

axis.HasTitle = true;

axis.AxisTitle.Text = "My left axis title";

axis.AxisTitle.Top = chart.PlotArea.Top;

 

//And the right axis

axis = (Excel.Axis)chart.Axes(XlAxisType.xlValue, XlAxisGroup.xlSecondary);

axis.HasTitle = true;

axis.AxisTitle.Text = "My right axis title";

axis.AxisTitle.Top = chart.PlotArea.Top;

Billets liés

Ajouter un commentaire


 

  Country flag





Live preview

août 28. 2008 17:29

Powered by BlogEngine.NET 1.2.0.0 | Theme by Pierre-Emmanuel Dautreppe