Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we set the position of an Excel chart from C#?

Tags:

c#

excel

charts

I am trying to generate an Excel chart from C#. Chart is generated just find but it allways appears at the center of the screen. How can I set the position of the chart?

Thanks.

My code looks like this:

Microsoft.Office.Interop.Excel._Workbook ebook = (Microsoft.Office.Interop.Excel._Workbook)etablo.Workbooks.Add(true);

Microsoft.Office.Interop.Excel._Worksheet esheet = (Microsoft.Office.Interop.Excel._Worksheet)ebook.ActiveSheet;

_Chart grafik1 = (Chart)ebook.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

/* Add Data From Cells here */
/* Then */
grafik1.Location(XlChartLocation.xlLocationAsObject, esheet.Name);
like image 991
slhsen Avatar asked Feb 03 '10 09:02

slhsen


People also ask

How do I change the position of a chart in Excel?

Click anywhere in the chart. This displays the Chart Tools option on the ribbon. In the Format Chart Area dialog box, click the Properties tab. Under Object Positioning select the appropriate option.

How do you c reposition the chart legend to the right of the chart?

Click the chart, and then click the Chart Design tab. Click Add Chart Element > Legend. To change the position of the legend, choose Right, Top, Left, or Bottom. To change the format of the legend, click More Legend Options, and then make the format changes that you want.

How do I move a chart to an exact location?

To accomplish either task, first select the chart area. Then click the “Design” tab of the “Chart Tools” contextual tab in the Ribbon. Then click the “Move Chart” button in the “Location” button group.

How do I find the position of a chart in Excel?

You can easily switch between the two possible locations by selecting (Chart > Location). An alternative to using the Chart drop-down menu you could also use the Chart shortcut menu. This will display the Chart Location dialog box. As new sheet - Inserts a new chart sheet before the active sheet.


2 Answers

after you generate a chart, you can handle chart as a shape object:

for example:

esheet.Shapes.Item("Chart 1").Top = 100;
esheet.Shapes.Item("Chart 1").Left = 250;


// or you can handle shape by index - indexes start from 1 so esheet.Shapes.Item(1).Top

i think, you would like to set position right the relevating cells, for example:

esheet.Shapes.Item("Chart 1").Top = (float)(double)esheet.get_Range("A5","A6").Top;

so ...i hope this will help :)

Luboss

like image 192
Luboss Avatar answered Sep 22 '22 18:09

Luboss


When Adding chart You can use this code to handle it's left, top, width, height. use this link

var chartObject = charts.Add(260, 30, 300, 300);
like image 43
Saurabh Agrawal Avatar answered Sep 18 '22 18:09

Saurabh Agrawal