Embedded chart is resizing

Hi,

I am creating Excel charts using templates and then I am embedding these charts in slide. The problem is that when this embeddec ahrt is double clicked to activate, the chart gets resized. I am not using any code for resizing the chart (like setOleSize etc). I have attached the output chart which is resizing. Please help me in identifying why the chart is resizing.

Regards,

Mahima

Hi Mahima,

I have tested your scenario but could not find if the chart is re-sized. Could you give us some more details, do you create charts using Aspose.Cells APIs? How do you embed the chart workbook in Powerpoint slide, do you embed it manually or use Aspose.Slides component. Moreover, did you check without involving the Aspose.Cells generated chart file, I mean you can create the chart manually in MS Excel and save the file, now insert the chart workbook into a Powerpoint slide if the chart is re-sized.

Also, if you are using Aspose.Cells component, kindly try the latest version of the component.

Thank you.

Were you able to detect a chart resize in the attached presentation ?

I am using Aspose.cells to generate the Charts using templates and then embedding these charts in the slides using Aspose.slides.

I am using Aspose.Cells - 4.7

Aspose.Slides - 3.0

I have tested by manual embedding the chart in the slide and the chart does not resize on activation. I am posting the code which I am using -

string slideLayoutPpt = System.IO.Path.Combine(SystemConfiguration.TemplatesFolder, Templates.SLIDE_LAYOUT);

Aspose.Slides.Presentation tempPresentation = new Aspose.Slides.Presentation(slideLayoutPpt);

Slide layoutSlide = tempPresentation.GetSlideByPosition((int)SlideLayout.TitleAndContent);

SortedList sList = new SortedList();

slide = tempPresentation.CloneSlide(layoutSlide, _presentation.Slides.LastSlidePosition + 1, _presentation, sList);

Aspose.Slides.Presentation pres = pSlide.Parent;

Workbook workbook = new Workbook();

workbook.Open(template);

//Obtaining the reference of the worksheet by name

worksheet = workbook.Worksheets[DataSheet];

worksheet.Cells.ClearRange(0, 0, 20, 20);

DataTable data = null;

data = DataSection.ChartDataTable;

worksheet.Cells.ImportDataTable(data, true, "A1");

Chart chart = workbook.Worksheets[ChartSheet].Charts[0];

//clear the chart's old values

chart.NSeries.Clear();

chart.NSeries.Add(worksheet.Name + "!B2:" + cellName, true);

chart.NSeries.CategoryData = worksheet.Name + "!A2:A" + rowcount;

string sItem = string.Empty;

for (int i = 0; i < chart.NSeries.Count; i++)

{

sItem = worksheet.Cells[0, i + 1].Value.ToString();

chart.NSeries[i].Name = sItem;

}

chartBitMap = chart.ToImage();

if (chartBitMap != null)

{

//Save it to a stream

MemoryStream wbStream = workbook.SaveToStream();

wbStream.Seek(0, SeekOrigin.Begin);

byte[] b = new byte[wbStream.Length];

wbStream.Read(b, 0, (int)wbStream.Length);

wbStream.Close();

//Add Chart to Slide

Aspose.Slides.Shape shp = pSlide.Shapes[1];

int x = shp.X;

int y = shp.Y;

int width = shp.Width;

int height = shp.Height;

pSlide.Shapes.RemoveAt(1);

OleObjectFrame oof = pSlide.Shapes.AddOleObjectFrame(x, y, width, height, "Excel.Sheet.12", b); //msOut.ToArray());

//cover the object message with the image of the chart

Aspose.Slides.Picture pic = new Aspose.Slides.Picture(pSlide.Parent, chartBitMap);

int picID = pres.Pictures.Add(pic);

oof.PictureId = picID;

pres.Write(presLocation);

The application is quite big and has code to create different types of charts. So, I have copied the base code for creating the charts. The slideLayout template and the charts template is attached.

Hi,

Yes your provided slide has the chart which is re-sized a bit, the labels are also changed and their rotation angles are modified when activating and deactivating it. We are not sure if this is the issue with Aspose.Cells for .NET or Aspose.Slides for .NET. We will check it and let you know soon.

By the way, could you create this chart workbook manually in MS Excel (without using Aspose.Cells for .NET API) and then insert it as an ole object using Aspose.Slides APIs to check if the chart gets re-sized or not.

Thank you.

I have already tested manually inserting Ole Object in powerpoint. The embedded chart does not resize on activation.

Hi,

We have further investigated your issue and we think that the issue is regarding Aspose.Slides for .NET. I will move this thread to Aspose.Slides forum, so that our developers of Aspose.Slides team will check your issue soon.

Thank you.

I have tried setting the ole size using the following code -

workbook.Worksheets.SetOleSize(0, 32, 0, 15);

After using this code, and activating the datatsheet explicitly does not resize the chart.

But I need to make the chart sheet active when the we double click the embedded object. And when I do this, the chart resizes.

Is there any update on this issue ?

Hello,

It is SetOleSize() issue.

As you can see, if you directly add a chart without using Aspose.Cells, it does not resize.

See the code below and the output ppt.

//Read the chart from excel file
FileStream fin = new FileStream(@"C:\Test\ChartsTemplate.xls", FileMode.Open, FileAccess.Read);
byte[] chartBytes = new byte[fin.Length];
fin.Read(chartBytes, 0, chartBytes.Length);
fin.Close();

Presentation pres = new Presentation();
Slide sld = pres.GetSlideByPosition(1);

int x = 100;
int y = 100;
int xWidth = pres.SlideSize.Width - 1000;
int yHeight = pres.SlideSize.Height - 1000;

OleObjectFrame oof = sld.Shapes.AddOleObjectFrame(x, y, xWidth, yHeight, "Excel.Sheet.12", chartBytes);

pres.Write("c:\\test\\chartOutput.ppt");

I have tried using the setolesize(). And it does not resolve the issue. The setolesize() sets the number of rows in the excel. I have used the below code -

workbook.Worksheets.SetOleSize(0, 32, 0, 15);

In the embedded workbook, if the active sheet is the data sheet, then on activation the chart does not resize. But if we set the chart sheet as the active sheet in the embedded workbook, then on activation the chart resizes. I am not able to resolve this issue. I have attached the presentations with the same code with only the active sheet different. You can make out that in one presentation the chart resizes and in the other it does not.

Hello,

Your chart width is smaller than its height.

I have written a separate code to find the ratio of chart width and height. And your chart width turns out to be 98% of chart height

The code below reads your presentation and set the height and width according to that ratio and you can see now the PowerPoint does not adjust the size of the chart.

See the output.ppt

Presentation pres = new Presentation(@"C:\Test\Scatter+-+ChartSheetActive.ppt");
Slide sld = pres.GetSlideByPosition(1);
Aspose.Slides.Shape shp = sld.Shapes[1];

float ratio = 0.98f;
shp.Height = 4000; //You can choose any height
shp.Width = (int)(shp.Height * ratio); //But width will be the ratio of height

pres.Write("C:\\test\\output.ppt");

I will see if I can find any workaround, may be template oleobjectframe instead of adding a new one could work.

This is the workaround, I suggested above.

I created a templatePres.ppt with an OleObject of my own desired height and width, then I extracted the data from your Scatter±+ChartSheetActive.ppt and updated the OleObject of template with your data.

Now, if you double click the chart in output presenation, you will see your chart instead of template chart and it will also not be resized.

Also, read the comments

I am attaching the templatePres.ppt and the output presentation

C#

Presentation templatePres = new Presentation(@"C:\Test\templatePres.ppt");
Slide templateSld = templatePres.GetSlideByPosition(1);
OleObjectFrame templateOleObjectFrame = templateSld.Shapes[0] as OleObjectFrame;

//Extracting the bytes of your chart
//In real scenario, these bytes will come from Aspose.Cells Workbook
Presentation pres = new Presentation(@"C:\Test\Scatter+-+ChartSheetActive.ppt");
Slide sld = pres.GetSlideByPosition(1);
OleObjectFrame oof = sld.Shapes[1] as OleObjectFrame;

//Now update the template oleobject with your data
templateOleObjectFrame.ObjectClassName = oof.ObjectClassName;
templateOleObjectFrame.ObjectData = oof.ObjectData;

//Write template
templatePres.Write("c:\\test\\outTemplate.ppt");

I don't understand how do we get the oof in the above code? We donot have an optput presentation. Alll we have is a template with an Oleobjectframe and a slide layout. We clone the slide from teh template, using charts wrokbook template, modify the data and get the required chart and then how do we set this new chart to the existing oleframe of the cloned slide?

In my understanding we do not get the oleobjectframe without adding it to the slide?

Below is the code which I am using -

slideLayoutPpt is the template file with the chart.

Aspose.Slides.Presentation tempPresentation = new Aspose.Slides.Presentation(slideLayoutPpt);

Slide layoutSlide = tempPresentation.GetSlideByPosition((int)SlideLayout.TitleAndContent);

SortedList sList = new SortedList();

slide = tempPresentation.CloneSlide(layoutSlide, _presentation.Slides.LastSlidePosition + 1, _presentation, sList);

TitleCreator tc = new TitleCreator(officeSlide);

tc.SaveTitleOnSlide(slide);

ChartBuilder chrtBldr = new ChartBuilder();

chrtBldr.OfficeChartObject = officeSlide.chart;

Workbook workbook = new Workbook();

//create the chart

Chart chart = chrtBldr.CreateChart(ref workbook, appInfo);

if (chart != null)

{

//Save the workbook to a stream

byte[] b = GetStream(workbook);

//Add Chart to Slide

Aspose.Slides.Shape shp = slide.Shapes[1];

int x = shp.X;

int y = shp.Y;

int width = shp.Width;

int height = shp.Height;

slide.Shapes.RemoveAt(1);

OleObjectFrame oof = slide.Shapes.AddOleObjectFrame(x, y, width, height, "Excel.Sheet.12", b);

//cover the object message with the image of the chart

try

{

//Get the picture of the chart so we don't have the Object Changed image

Bitmap chartBitMap = chart.ToImage();

Aspose.Slides.Picture pic = new Aspose.Slides.Picture(slide.Parent, chartBitMap);

int picID = slide.Parent.Pictures.Add(pic);

oof.PictureId = picID;

//success = true;

}

catch (Exception ex)

{

}

These are real scenarios.

1- Create a presentation in MS-PowerPoint with a Chart (synonymous to Ole Object) with desired dimensions (height / width) and save it and call it a Template.ppt

2- Then create another presentation using Aspose.Slides and clone the chart slide from Template.ppt into it and call it Target.ppt

3- Then create workbook/chart using Aspose.Cells with your data and get its byte data

4- Then access the OleObjectFrame from Target.ppt and change its object class name as “Excel.Sheet.12” and object data with the data obtained in step 3

5- Then get the image of your chart created in step 3 and update the image of OleObjectFrame of Target.ppt with it.