mars 26

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;

Tags: |


Posted on mercredi, 21 août 2013 00:58

Good answer back in return of this difficulty with genuine arguments and telling everything regarding that.

Review my homepage: sintomas del autismo ( - )


Posted on jeudi, 22 août 2013 05:10

Thank you for the good writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how could we communicate?

Also visit my web page esterilidad -  Melvin - ,

Posted on vendredi, 7 février 2014 06:22

Author marketing experts will answer 'yes' to this question every time. To get a customized blog with Word - Press website development you need to hire professional developers who have expertise in this business.

My webpage - line ( - )

Ajouter un commentaire

  • Commentaire
  • Aperçu immédiat